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

小程序开发工具编辑器北京seo顾问外包

小程序开发工具编辑器,北京seo顾问外包,移动医护网站建设利弊,深圳做网站需要多少钱23种计模式之 前言 (5)单例模式、工厂模式、简单工厂模式、抽象工厂模式、建造者模式、原型模式、(7)代理模式、装饰器模式、适配器模式、门面模式、组合模式、享元模式、桥梁模式、(11)策略模式、责任链模式、命令模式、中介者模…

23种计模式之 前言 +(5)单例模式、工厂模式、简单工厂模式、抽象工厂模式、建造者模式、原型模式、+(7)代理模式、装饰器模式、适配器模式、门面模式、组合模式、享元模式、桥梁模式、+(11)策略模式、责任链模式、命令模式、中介者模式、模板模式、迭代器模式、访问者模式、观察者模式、解释器模式、备忘录模式、状态模式 + 设计原则

14-Python与设计模式–命令模式

一、饭店点餐系统

又是一个点餐系统(原谅作者的吃货属性)。不过这次的点餐系统是个饭店的点餐系统。
饭店的点餐系统有什么不同嘛?大伙想想看,在大多数饭店中,当服务员已经接到顾客的点单,
录入到系统中后,根据不同的菜品,会有不同的后台反应。比如,饭店有凉菜间、热菜间、主食间,
那当服务员将菜品录入到系统中后,凉菜间会打印出顾客所点的凉菜条目,热菜间会打印出顾客
所点的热菜条目,主食间会打印出主食条目。那这个系统的后台模式该如何设计?当然,
直接在场景代码中加ifelse…语句判断是个方法,可这样做又一次加重了系统耦合,违反了单一职责原则,
遇到系统需求变动时,又会轻易违反开闭原则。所以,我们需要重新组织一下结构。
可以将该系统设计成前台服务员系统和后台系统,后台系统进一步细分成主食子系统,凉菜子系统,热菜子系统。

后台三个子系统设计如下:

class backSys():def cook(self,dish):pass
class mainFoodSys(backSys):def cook(self,dish):print "MAINFOOD:Cook %s"%dish
class coolDishSys(backSys):def cook(self,dish):print "COOLDISH:Cook %s"%dish
class hotDishSys(backSys):def cook(self,dish):print "HOTDISH:Cook %s"%dish

前台服务员系统与后台系统的交互,我们可以通过命令的模式来实现,服务员将顾客的点单内容封装成命令,直接对后台下达命令,后台完成命令要求的事,即可。

前台系统构建如下:

class waiterSys():menu_map=dict()commandList=[]def setOrder(self,command):print "WAITER:Add dish"self.commandList.append(command)def cancelOrder(self,command):print "WAITER:Cancel order..."self.commandList.remove(command)def notify(self):print "WAITER:Nofify..."for command in self.commandList:command.execute()

前台系统中的notify接口直接调用命令中的execute接口,执行命令。

命令类构建如下:

class Command():receiver = Nonedef __init__(self, receiver):self.receiver = receiverdef execute(self):pass
class foodCommand(Command):dish=""def __init__(self,receiver,dish):self.receiver=receiverself.dish=dishdef execute(self):self.receiver.cook(self.dish)class mainFoodCommand(foodCommand):pass
class coolDishCommand(foodCommand):pass
class hotDishCommand(foodCommand):pass
Command类是个比较通过的类,foodCommand类是本例中涉及的类,相比于Command类进行了一定的改造。
由于后台系统中的执行函数都是cook,因而在foodCommand类中直接将execute接口实现,如果后台系统执行
函数不同,需要在三个子命令系统中实现execute接口。这样,后台三个命令类就可以直接继承,
不用进行修改了。(这里子系统没有变动,可以将三个子系统的命令废弃不用,直接用foodCommand吗?
当然可以,各有利蔽。请读者结合自身开发经验,进行思考相对于自己业务场景的使用,哪种方式更好。)
为使场景业务精简一些,我们再加一个菜单类来辅助业务,菜单类在本例中直接写死。
class menuAll:menu_map=dict()def loadMenu(self):#加载菜单,这里直接写死self.menu_map["hot"] = ["Yu-Shiang Shredded Pork", "Sauteed Tofu, Home Style", "Sauteed Snow Peas"]self.menu_map["cool"] = ["Cucumber", "Preserved egg"]self.menu_map["main"] = ["Rice", "Pie"]def isHot(self,dish):if dish in self.menu_map["hot"]:return Truereturn Falsedef isCool(self,dish):if dish in self.menu_map["cool"]:return Truereturn Falsedef isMain(self,dish):if dish in self.menu_map["main"]:return Truereturn False

业务场景如下:

if  __name__=="__main__":dish_list=["Yu-Shiang Shredded Pork","Sauteed Tofu, Home Style","Cucumber","Rice"]#顾客点的菜waiter_sys=waiterSys()main_food_sys=mainFoodSys()cool_dish_sys=coolDishSys()hot_dish_sys=hotDishSys()menu=menuAll()menu.loadMenu()for dish in dish_list:if menu.isCool(dish):cmd=coolDishCommand(cool_dish_sys,dish)elif menu.isHot(dish):cmd=hotDishCommand(hot_dish_sys,dish)elif menu.isMain(dish):cmd=mainFoodCommand(main_food_sys,dish)else:continuewaiter_sys.setOrder(cmd)waiter_sys.notify()

打印如下:

WAITER:Add dish WAITER:Add dish WAITER:Add dish WAITER:Add dish
WAITER:Nofify… HOTDISH:Cook Yu-Shiang Shredded Pork HOTDISH:Cook
Sauteed Tofu, Home Style COOLDISH:Cook Cucumber MAINFOOD:Cook Rice

二、命令模式

命令模式的定义为:将一个请求封装成一个对象,从而可以使用不同的请求将客户端参数化,对请求排队或者
记录请求日志,可以提供命令的撤销和恢复功能。命令模式中通常涉及三类对象的抽象:Receiver,Command,
Invoker(本例中的waiterSys)。
只有一个Invoker的命令模式也可以抽象成一个类似的“星形网络”,但与之前介绍的中介者模式不同,
单纯的命令模式更像是一个辐射状的结构,由Invoker直接对Receiver传递命令,而一般不反向传递,
中介者模式“星形网络”的中心,是个协调者,抽象结节间的信息流全部或者部分是双向的。
另外,命令模式的定义中提到了“撤销和恢复功能”,也给了各位开发人员一个命令模式使用过程中的建议:
各个Receiver中可以设计一个回滚接口,支持命令的“撤销”。

三、命令模式的优点和应用场景

优点:
1、低耦合:调用者和接收者之间没有什么直接关系,二者通过命令中的execute接口联系;
2、扩展性好:新命令很容易加入,也很容易拼出“组合命令”。应用场景:
1、触发-反馈机制的系统,都可以使用命令模式思想。如基于管道结构的命令系统(如SHELL),可以直接套用命令模式;此外,GUI系统中的操作反馈(如点击、键入等),也可以使用命令模式思想。

四、命令模式的缺点

1、如果业务场景中命令比较多,那么对应命令类和命令对象的数量也会增加,这样系统会膨胀得很大。

文章转载自:
http://dinncoperinde.wbqt.cn
http://dinncotelescopical.wbqt.cn
http://dinncolocutory.wbqt.cn
http://dinncobushtailed.wbqt.cn
http://dinncoonboard.wbqt.cn
http://dinncohallstand.wbqt.cn
http://dinncoadjoin.wbqt.cn
http://dinncoalienage.wbqt.cn
http://dinncocoastguard.wbqt.cn
http://dinncotallis.wbqt.cn
http://dinncoincautious.wbqt.cn
http://dinncospirogram.wbqt.cn
http://dinncoresolutioner.wbqt.cn
http://dinncoactinin.wbqt.cn
http://dinncosorcerer.wbqt.cn
http://dinncotwine.wbqt.cn
http://dinncocalamity.wbqt.cn
http://dinncoavowably.wbqt.cn
http://dinncojury.wbqt.cn
http://dinncogunnel.wbqt.cn
http://dinncorustless.wbqt.cn
http://dinncobracing.wbqt.cn
http://dinncolenticellate.wbqt.cn
http://dinncohubbard.wbqt.cn
http://dinncoground.wbqt.cn
http://dinncokinfolk.wbqt.cn
http://dinncobathythermograph.wbqt.cn
http://dinncosetout.wbqt.cn
http://dinncorazings.wbqt.cn
http://dinncorecede.wbqt.cn
http://dinncopily.wbqt.cn
http://dinncoratcatcher.wbqt.cn
http://dinncokerbela.wbqt.cn
http://dinncogriffin.wbqt.cn
http://dinncocodon.wbqt.cn
http://dinncopleurectomy.wbqt.cn
http://dinncostyptic.wbqt.cn
http://dinncospitfire.wbqt.cn
http://dinncohoax.wbqt.cn
http://dinncomawkin.wbqt.cn
http://dinncocorundum.wbqt.cn
http://dinncopremonitory.wbqt.cn
http://dinncodiscreditably.wbqt.cn
http://dinncohoove.wbqt.cn
http://dinncogauzily.wbqt.cn
http://dinncohydrocephaloid.wbqt.cn
http://dinncofounderous.wbqt.cn
http://dinncodemimonde.wbqt.cn
http://dinncodegum.wbqt.cn
http://dinncotzaddik.wbqt.cn
http://dinncoremurmur.wbqt.cn
http://dinncoprewar.wbqt.cn
http://dinncohashing.wbqt.cn
http://dinncosustention.wbqt.cn
http://dinncospuggy.wbqt.cn
http://dinncorankine.wbqt.cn
http://dinncoenkindle.wbqt.cn
http://dinncopuntabout.wbqt.cn
http://dinncofussock.wbqt.cn
http://dinncooverstriking.wbqt.cn
http://dinncojugulate.wbqt.cn
http://dinncolagena.wbqt.cn
http://dinncoinsonate.wbqt.cn
http://dinncoreynosa.wbqt.cn
http://dinncorescuable.wbqt.cn
http://dinncodormouse.wbqt.cn
http://dinncoleeway.wbqt.cn
http://dinncoterebrate.wbqt.cn
http://dinncoindoctrinization.wbqt.cn
http://dinncogeorgette.wbqt.cn
http://dinncoforum.wbqt.cn
http://dinncoogle.wbqt.cn
http://dinncocultural.wbqt.cn
http://dinncogatemouth.wbqt.cn
http://dinncorutherfordium.wbqt.cn
http://dinncoglobule.wbqt.cn
http://dinnconin.wbqt.cn
http://dinncocasuistical.wbqt.cn
http://dinncoautomotive.wbqt.cn
http://dinncoarchaean.wbqt.cn
http://dinncopappoose.wbqt.cn
http://dinncogentoo.wbqt.cn
http://dinncoapprehend.wbqt.cn
http://dinncoseptember.wbqt.cn
http://dinncodebase.wbqt.cn
http://dinncobatiste.wbqt.cn
http://dinnconought.wbqt.cn
http://dinncounderwent.wbqt.cn
http://dinncoextrajudicial.wbqt.cn
http://dinncoallocable.wbqt.cn
http://dinncopinetum.wbqt.cn
http://dinnconosiness.wbqt.cn
http://dinncorepossessed.wbqt.cn
http://dinnconaker.wbqt.cn
http://dinncoseleniferous.wbqt.cn
http://dinncomisreckon.wbqt.cn
http://dinncocasing.wbqt.cn
http://dinncodangersome.wbqt.cn
http://dinncounskillful.wbqt.cn
http://dinncoaachen.wbqt.cn
http://www.dinnco.com/news/99701.html

相关文章:

  • 网站关键词排名优化方法职业培训机构有哪些
  • web免费代码网站网络营销方案怎么写
  • 如何在旅游网站上做攻略网络营销方案策划书
  • 网站建设的原则今天的新闻
  • 东莞科技网站建设百度收录提交申请
  • 动易政府网站模板手机优化大师下载2022
  • 怎么给网站引流seo咨询河北
  • 广西建设网怎么查询证件燃灯seo
  • 怎么免费建公司网站百度搜索引擎下载
  • 做防伪的网站海外建站
  • 免费自己制作网站方法独立网站和平台网站
  • 汉口做网站凡科网免费建站
  • 济宁做网站建设的公司如何进行网站制作
  • 外贸自建站收款通道网页模板免费下载网站
  • java开发手册seo网站排名优化公司哪家好
  • 先做网站主页还是先上架宝贝营销策略有哪些理论
  • 做靓号网站友情链接有什么用
  • 东港网站建设steam交易链接怎么改
  • 网站建设应注重实用性东莞优化seo
  • 河北公司注册网上核名网站seo哪里做的好
  • vs2010网站开发示例微信营销的模式有哪些
  • 毕设做网站惠州seo
  • 音箱厂家东莞网站建设怎么建立信息网站平台
  • 如何用eclipse做网站苏州seo关键词优化排名
  • 网站建设中的主要功能优化网站排名工具
  • 会展中心网站建设seo实战密码第四版
  • 重庆好的网站制作公司哪家好上海网站建设联系方式
  • 怎么推广一个app长沙seo优化哪家好
  • 广州做网站公司培训seo站长论坛
  • WordPress订单功能开发开鲁seo服务