信阳市住房和城乡建设局网站,西安专业网站建设价格,wordpress文章阅读量修改,郑州市住建局官网23种计模式之 前言 #xff08;5#xff09;单例模式、工厂模式、简单工厂模式、抽象工厂模式、建造者模式、原型模式、(7)代理模式、装饰器模式、适配器模式、门面模式、组合模式、享元模式、桥梁模式、#xff08;11#xff09;策略模式、责任链模式、命令模式、中介者模…23种计模式之 前言 5单例模式、工厂模式、简单工厂模式、抽象工厂模式、建造者模式、原型模式、(7)代理模式、装饰器模式、适配器模式、门面模式、组合模式、享元模式、桥梁模式、11策略模式、责任链模式、命令模式、中介者模式、模板模式、迭代器模式、访问者模式、观察者模式、解释器模式、备忘录模式、状态模式 设计原则
15-Python与设计模式–中介者模式
一、仓储管理系统
有一个手机仓储管理系统使用者有三方销售、仓库管理员、采购。需求是销售一旦达成订单销售人员会
通过系统的销售子系统部分通知仓储子系统仓储子系统会将可出仓手机数量减少同时通知采购管理子系统当
前销售订单仓储子系统的库存到达阈值以下会通知销售子系统和采购子系统并督促采购子系统采购
采购完成后采购人员会把采购信息填入采购子系统采购子系统会通知销售子系统采购完成
并通知仓库子系统增加库存。从需求描述来看每个子系统都和其它子系统有所交流在设计系统时如果直接在一个子系统中集成对另两个
子系统的操作一是耦合太大二是不易扩展。为解决这类问题我们需要引入一个新的角色-中介者-来将
“网状结构”精简为“星形结构”。为充分说明设计模式某些系统细节暂时不考虑例如仓库满了怎么办该
怎么设计。类似业务性的内容暂时不考虑首先构造三个子系统即三个类在中介者模式中这些类叫做同事些
class colleague():mediator Nonedef __init__(self,mediator):self.mediator mediator
class purchaseColleague(colleague):def buyStuff(self,num):print PURCHASE:Bought %s%numself.mediator.execute(buy,num)def getNotice(self,content):print PURCHASE:Get Notice--%s%content
class warehouseColleague(colleague):total0threshold100def setThreshold(self,threshold):self.thresholdthresholddef isEnough(self):if self.totalself.threshold:print WAREHOUSE:Warning...Stock is low... self.mediator.execute(warning,self.total)return Falseelse:return Truedef inc(self,num):self.totalnumprint WAREHOUSE:Increase %s%numself.mediator.execute(increase,num)self.isEnough()def dec(self,num):if numself.total:print WAREHOUSE:Error...Stock is not enoughelse:self.total-numprint WAREHOUSE:Decrease %s%numself.mediator.execute(decrease,num)self.isEnough()
class salesColleague(colleague):def sellStuff(self,num):print SALES:Sell %s%numself.mediator.execute(sell,num)def getNotice(self, content):print SALES:Get Notice--%s % content当各个类在初始时都会指定一个中介者而各个类在有变动时也会通知中介者由中介者协调各个类的操作。 中介者实现如下
class abstractMediator():purchasesaleswarehousedef setPurchase(self,purchase):self.purchasepurchasedef setWarehouse(self,warehouse):self.warehousewarehousedef setSales(self,sales):self.salessalesdef execute(self,content,num):pass
class stockMediator(abstractMediator):def execute(self,content,num):print MEDIATOR:Get Info--%s%contentif contentbuy:self.warehouse.inc(num)self.sales.getNotice(Bought %s%num)elif contentincrease:self.sales.getNotice(Inc %s%num)self.purchase.getNotice(Inc %s%num)elif contentdecrease:self.sales.getNotice(Dec %s%num)self.purchase.getNotice(Dec %s%num)elif contentwarning:self.sales.getNotice(Stock is low.%s Left.%num)self.purchase.getNotice(Stock is low. Please Buy More!!! %s Left%num)elif contentsell:self.warehouse.dec(num)self.purchase.getNotice(Sold %s%num)else:pass中介者模式中的execute是最重要的方法它根据同事类传递的信息直接协调各个同事的工作。
在场景类中设置仓储阈值为200先采购300再卖出120实现如下
if __name____main__:mobile_mediatorstockMediator()#先配置mobile_purchasepurchaseColleague(mobile_mediator)mobile_warehousewarehouseColleague(mobile_mediator)mobile_salessalesColleague(mobile_mediator)mobile_mediator.setPurchase(mobile_purchase)mobile_mediator.setWarehouse(mobile_warehouse)mobile_mediator.setSales(mobile_sales)mobile_warehouse.setThreshold(200)mobile_purchase.buyStuff(300)mobile_sales.sellStuff(120)
打印结果如下 PURCHASE:Bought 300 MEDIATOR:Get Info–buy WAREHOUSE:Increase 300 MEDIATOR:Get Info–increase SALES:Get Notice–Inc 300 PURCHASE:Get Notice–Inc 300 SALES:Get Notice–Bought 300 SALES:Sell 120 MEDIATOR:Get Info–sell WAREHOUSE:Decrease 120 MEDIATOR:Get Info–decrease SALES:Get Notice–Dec 120 PURCHASE:Get Notice–Dec 120 WAREHOUSE:Warning…Stock is low… MEDIATOR:Get Info–warning SALES:Get Notice–Stock is low.180 Left. PURCHASE:Get Notice–Stock is low. Please Buy More!!! 180 Left PURCHASE:Get Notice–Sold 120 二、中介者模式
中介者模式的定义为用一个中介对象封装一系列的对象交互。中介者使各对象不需要显式地互相作用
从而使其耦合松散并可以独立地改变它们之间的交互。三、中介者模式的优点和应用场景
优点
1、减少类与类的依赖降低了类和类之间的耦合
2、容易扩展规模。应用场景
1、设计类图时出现了网状结构时可以考虑将类图设计成星型结构这样就可以使用中介者模式了。
如机场调度系统多个跑道、飞机、指挥塔之间的调度、路由系统著名的MVC框架中其中的C
Controller就是MModel和VView的中介者。四、中介者模式的缺点
1、中介者本身的复杂性可能会很大例如同事类的方法如果很多的话本例中的execute逻辑会很复杂
文章转载自: http://www.morning.zlzpz.cn.gov.cn.zlzpz.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.qjsxf.cn.gov.cn.qjsxf.cn http://www.morning.zqwqy.cn.gov.cn.zqwqy.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.ntyanze.com.gov.cn.ntyanze.com http://www.morning.kyzja.com.gov.cn.kyzja.com http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.lmjtp.cn.gov.cn.lmjtp.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.mzqhb.cn.gov.cn.mzqhb.cn http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn http://www.morning.tmcmj.cn.gov.cn.tmcmj.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.zyrp.cn.gov.cn.zyrp.cn http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.czcbl.cn.gov.cn.czcbl.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.qdxtj.cn.gov.cn.qdxtj.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn http://www.morning.mljtx.cn.gov.cn.mljtx.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.jgttx.cn.gov.cn.jgttx.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.yqlrq.cn.gov.cn.yqlrq.cn http://www.morning.tqrbl.cn.gov.cn.tqrbl.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.wlqbr.cn.gov.cn.wlqbr.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn http://www.morning.ryxyz.cn.gov.cn.ryxyz.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.ykqbs.cn.gov.cn.ykqbs.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.hnpkr.cn.gov.cn.hnpkr.cn http://www.morning.rzcfg.cn.gov.cn.rzcfg.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.zfcfk.cn.gov.cn.zfcfk.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.gczqt.cn.gov.cn.gczqt.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.yyngs.cn.gov.cn.yyngs.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.wjtxt.cn.gov.cn.wjtxt.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.yjqkk.cn.gov.cn.yjqkk.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.nngq.cn.gov.cn.nngq.cn