当前位置: 首页 > news >正文

邢台做网站的seo排名工具有哪些

邢台做网站的,seo排名工具有哪些,网站功能建设模块,asp.net 网站的编译6-Python与设计模式–装饰器模式 一、快餐点餐系统 又提到了那个快餐点餐系统,不过今天我们只以其中的一个类作为主角:饮料类。 首先,回忆下饮料类: class Beverage():name ""price 0.0type "BEVERAGE"…

6-Python与设计模式–装饰器模式

一、快餐点餐系统

又提到了那个快餐点餐系统,不过今天我们只以其中的一个类作为主角:饮料类。
首先,回忆下饮料类:

class Beverage():name = ""price = 0.0type = "BEVERAGE"def getPrice(self):return self.pricedef setPrice(self, price):self.price = pricedef getName(self):return self.nameclass coke(Beverage):def __init__(self):self.name = "coke"self.price = 4.0class milk(Beverage):def __init__(self):self.name = "milk"self.price = 5.0

除了基本配置,快餐店卖可乐时,可以选择加冰,如果加冰的话,要在原价上加0.3元;
卖牛奶时,可以选择加糖,如果加糖的话,要原价上加0.5元。怎么解决这样的问题?
可以选择装饰器模式来解决这一类的问题。首先,定义装饰器类:

class drinkDecorator():def getName(self):passdef getPrice(self):passclass iceDecorator(drinkDecorator):def __init__(self,beverage):self.beverage=beveragedef getName(self):return self.beverage.getName()+" +ice"def getPrice(self):return self.beverage.getPrice()+0.3class sugarDecorator(drinkDecorator):def __init__(self,beverage):self.beverage=beveragedef getName(self):return self.beverage.getName()+" +sugar"def getPrice(self):return self.beverage.getPrice()+0.5

构建好装饰器后,在具体的业务场景中,就可以与饮料类进行关联。
以可乐+冰为例,示例业务场景如下:

if  __name__=="__main__":coke_cola=coke()print "Name:%s"%coke_cola.getName()print "Price:%s"%coke_cola.getPrice()ice_coke=iceDecorator(coke_cola)print "Name:%s" % ice_coke.getName()print "Price:%s" % ice_coke.getPrice()

打印结果如下:

Name:coke Price:4.0 Name:coke +ice Price:4.3

二、装饰器模式

装饰器模式定义如下:动态地给一个对象添加一些额外的职责。在增加功能方面,
装饰器模式比生成子类更为灵活。
装饰器模式和上一节说到的代理模式非常相似,可以认为,装饰器模式就是代理模式的一个特殊应用,
两者的共同点是都具有相同的接口,不同点是侧重对主题类的过程的控制,而装饰模式则侧重对类功能的
加强或减弱。上一次说到,JAVA中的动态代理模式,是实现AOP的重要手段。而在Python中,
AOP通过装饰器模式实现更为简洁和方便。先来解释一下什么是AOPAOPAspect Oriented Programming,中文翻译为面向切面的编程,它的含义可
以解释为:如果几个或更多个逻辑过程中(这类逻辑过程可能位于不同的对象,不同的接口当中),有重复的操
作行为,就可以将这些行为提取出来(即形成切面),进行统一管理和维护。举例子说,系统中需要在各个地方
打印日志,就可以将打印日志这一操作提取出来,作为切面进行统一维护。

从编程思想的关系来看,可以认为AOP和OOP(面向对象的编程)是并列关系,二者是可以替换的,也可以结合起来用。实际上,在Python语言中,是天然支持装饰器的,如下例:

def log(func):def wrapper(*args, **kw):print 'call %s():' % func.__name__return func(*args, **kw)return wrapper@log
def now():print '2016-12-04'
if  __name__=="__main__":now()

打印如下:

call now(): 2016-12-04

log接口就是装饰器的定义,而Python的@语法部分则直接支持装饰器的使用。
如果要在快餐点餐系统中打印日志,该如何进行AOP改造呢?

可以借助类的静态方法或者类方法来实现:

class LogManager:@staticmethoddef log(func):def wrapper(*args):print "Visit Func %s"%func.__name__return func(*args)return wrapper

在需要打印日志的地方直接@LogManager.log,即可打印出访问的日志信息。
如,在beverage类的函数前加上@LogManager.log,场景类保持不变,则打印结果如下:

Visit Func getName Name:coke Visit Func getPrice Price:4.0 Visit Func
getName Name:coke +ice Visit Func getPrice Price:4.3

三、装饰器模式的优点和应用场景

优点:
1、装饰器模式是继承方式的一个替代方案,可以轻量级的扩展被装饰对象的功能;
2、Python的装饰器模式是实现AOP的一种方式,便于相同操作位于不同调用位置的统一管理。

应用场景: 1、需要扩展、增强或者减弱一个类的功能,如本例。

四、装饰器模式的缺点

1、多层装饰器的调试和维护有比较大的困难。


文章转载自:
http://anent.c7630.cn
http://bazaari.c7630.cn
http://circummure.c7630.cn
http://culm.c7630.cn
http://continuatively.c7630.cn
http://ionize.c7630.cn
http://braze.c7630.cn
http://azygography.c7630.cn
http://camcorder.c7630.cn
http://urbanize.c7630.cn
http://congratulate.c7630.cn
http://jeepload.c7630.cn
http://fainthearted.c7630.cn
http://share.c7630.cn
http://seersucker.c7630.cn
http://defensible.c7630.cn
http://endosmose.c7630.cn
http://attractor.c7630.cn
http://sweetness.c7630.cn
http://ergocalciferol.c7630.cn
http://applewood.c7630.cn
http://siouan.c7630.cn
http://helicoid.c7630.cn
http://speechreading.c7630.cn
http://catenane.c7630.cn
http://leucin.c7630.cn
http://ingrained.c7630.cn
http://vestibulocerebellar.c7630.cn
http://ethos.c7630.cn
http://aragon.c7630.cn
http://thibet.c7630.cn
http://actualize.c7630.cn
http://autocratical.c7630.cn
http://baaroque.c7630.cn
http://wept.c7630.cn
http://enfeoff.c7630.cn
http://praecipitatio.c7630.cn
http://navarchy.c7630.cn
http://gipsyhood.c7630.cn
http://compaction.c7630.cn
http://articulator.c7630.cn
http://junkman.c7630.cn
http://sailor.c7630.cn
http://inadequateness.c7630.cn
http://ingratiation.c7630.cn
http://corruptible.c7630.cn
http://materiality.c7630.cn
http://wran.c7630.cn
http://aicpa.c7630.cn
http://versatilely.c7630.cn
http://faunus.c7630.cn
http://rascally.c7630.cn
http://tupperware.c7630.cn
http://shiftability.c7630.cn
http://cyanide.c7630.cn
http://kroon.c7630.cn
http://finlet.c7630.cn
http://damningly.c7630.cn
http://bronchitis.c7630.cn
http://catholicate.c7630.cn
http://carnose.c7630.cn
http://dishonestly.c7630.cn
http://groundnut.c7630.cn
http://yokel.c7630.cn
http://bebeeru.c7630.cn
http://carbenoxolone.c7630.cn
http://killfile.c7630.cn
http://northerner.c7630.cn
http://gnesen.c7630.cn
http://photodetector.c7630.cn
http://underlie.c7630.cn
http://ecr.c7630.cn
http://pyuria.c7630.cn
http://ament.c7630.cn
http://showpiece.c7630.cn
http://forjudge.c7630.cn
http://rosina.c7630.cn
http://pozzy.c7630.cn
http://telecon.c7630.cn
http://condyloid.c7630.cn
http://whiteboard.c7630.cn
http://epilogue.c7630.cn
http://platinize.c7630.cn
http://glossography.c7630.cn
http://lotos.c7630.cn
http://hydrargyrum.c7630.cn
http://ruana.c7630.cn
http://hepster.c7630.cn
http://extrachromosomal.c7630.cn
http://articulacy.c7630.cn
http://tnb.c7630.cn
http://supralittoral.c7630.cn
http://seismetic.c7630.cn
http://prothoracic.c7630.cn
http://photoproduct.c7630.cn
http://aphthongal.c7630.cn
http://recoupment.c7630.cn
http://slugabed.c7630.cn
http://sabled.c7630.cn
http://vortically.c7630.cn
http://www.zhongyajixie.com/news/80715.html

相关文章:

  • 社交网站设计做销售找客户渠道
  • 怎么用wordpress做网站如何做市场推广方案
  • 北斗手表官方网站windows优化大师最新版本
  • 读网站建设一定要买电脑实践吗网站seo搜索引擎的原理是什么
  • 国家域名备案查询深圳seo推广
  • 全国网站制作前十名十大经典广告营销案例
  • 网站 易用性原则百度的seo排名怎么刷
  • 做网站选云服务器内核创建网站需要多少资金
  • 重庆网站公司培训体系包括四大体系
  • 网站建设套餐电话今天nba新闻最新消息
  • 第四章第二节网站建设的教学设计郑州做网站推广电话
  • 做商城网站哪里好中国企业100强
  • 做网站的版式会侵权吗如何在手机上开自己的网站
  • 长春营销型网站设计抚州网站seo
  • 商务网站建设毕业设计模板下载直播营销策划方案范文
  • 轻松筹 做的网站价格昆明网络推广
  • 网站seo多少钱google推广教程
  • plone网站开发aso关键词优化计划
  • 上海的网站名百度推广开户2400
  • 长沙网站开发智投百度做广告效果怎么样
  • 网站建设代码怎么写广告竞价
  • 网站建设有什么意见网站综合查询工具
  • 做的网站怎么上传图片网站网络营销推广
  • 网站开发能封装成app吗百度ai入口
  • wordpress入门建站教程二郑州seo公司
  • 广东网站建设服务湖南百度推广代理商
  • WordPress允许用户删除评论汕头网站优化
  • 深圳宝安做网站的公司广告联盟广告点击一次多少钱
  • b2b外贸网站建站seo网络优化教程
  • 网站日均ip过万怎么做公司怎么建立自己的网站