代码成就万世基积沙镇海,梦想永在凌云意意气风发。

Python设计模式-中介者模式

Python设计模式-中介者模式

代码基于3.5.2,代码如下;

 1 #coding:utf-8
 2 #中介者模式
 3 
 4 class colleague():
 5     mediator = None
 6     def __init__(self,mediator):
 7         self.mediator = mediator
 8 
 9 class purchaseColleague(colleague):
10     def buyStuff(self,num):
11         print("PURCHASE:Bought {0}".format(num))
12         self.mediator.execute("buy",num)
13     def getNotice(self,content):
14         print("PURCHASE:Get Notice -- {0}".format(content))
15 
16 class warehoustColleague(colleague):
17     total = 0
18     threshold = 100
19     def setThreshold(self,threshold):
20         self.threshold = threshold
21     def isEnough(self):
22         if self.total < self.threshold:
23             print("WARNING:Warning ... Stock is low ..")
24             self.mediator.execute("warning",self.total)
25             return False
26         else:
27             return True
28     def inc(self,num):
29         self.total += num
30         print("WAREHOUSE:Increase {0}".format(num))
31         self.mediator.execute("increase",num)
32     def dec(self,num):
33         if num > self.total:
34             print("WAREHOUSE:Error ... Stock is not enough")
35         else:
36             self.total -= num
37             print("WAREHOUSE:Decrease {0}".format(num))
38             self.mediator.execute("decrease",num)
39         self.isEnough()
40 
41 class salesColleague(colleague):
42     def sellStuff(self,num):
43         print("SALES:Sell {0}".format(num))
44         self.mediator.execute("sell",num)
45     def getNotice(self,content):
46         print("SALES:Get Notice -- {0}".format(content))
47 
48 class abstractMediator():
49     purchase = None
50     sales = None
51     warehouse = None
52     def setPurchase(self,purchase):
53         self.purchase = purchase
54     def setWarehouse(self,warehouse):
55         self.warehouse = warehouse
56     def setSales(self,sales):
57         self.sales = sales
58     def execute(self,content,num):
59         pass
60 
61 class stockMediator(abstractMediator):
62     def execute(self,content,num):
63         print("MEDIATOR:Get Info -- {0}".format(content))
64         if content == "buy":
65             self.warehouse.inc(num)
66             self.sales.getNotice("Bought {0}".format(num))
67         elif content == "increase":
68             self.sales.getNotice("Inc {0}".format(num))
69             self.purchase.getNotice("Inc {0}".format(num))
70         elif content == "decrease":
71             self.sales.getNotice("Dec {0}".format(num))
72             self.purchase.getNotice("Dec {0}".format(num))
73         elif content == "warning":
74             self.sales.getNotice("Stock is low {0} left".format(num))
75             self.purchase.getNotice("Stock is low. Please Buy More {0}".format(num))
76         elif content == "sell":
77             self.warehouse.dec(num)
78             self.purchase.getNotice("Sold {0}".format(num))
79         else:
80             pass
81 
82 if __name__ == "__main__":
83     mobile_mediator = stockMediator()
84     mobile_purchase = purchaseColleague(mobile_mediator)
85     moblie_warehouse = warehoustColleague(mobile_mediator)
86     moblie_sales = salesColleague(mobile_mediator)
87     mobile_mediator.setPurchase(mobile_purchase)
88     mobile_mediator.setWarehouse(moblie_warehouse)
89     mobile_mediator.setSales(moblie_sales)
90 
91     moblie_warehouse.setThreshold(200)
92     mobile_purchase.buyStuff(300)
93     moblie_sales.sellStuff(120)

中介者模式分析与解读

中介者模式

中介者模式,用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

代码解读

该例子基于的需求:销售一旦达成订单,销售人员会通过系统的销售子系统部分通知仓储子系统,仓储子系统会将可出仓手机数量减少,同时通知采购管理子系统当前销售订单;仓储子系统的库存到达阈值以下,会通知销售子系统和采购子系统,并督促采购子系统采购;采购完成后,采购人员会把采购信息填入采购子系统,采购子系统会通知销售子系统采购完成,并通知仓库子系统增加库存。
1、定义了colleague类,子系统都是通过继承该子类来实现,在该类初始化时,传入mediator者;
2、分别定义了purchaseColleague、warehoustColleague和salesColleague三个类,分别表示采购子系统,仓库子系统和销售子系统,在purchaseColleague中的buyStuff、salesColleague的sellStuff方法,都是在接收到请求处理时调用了mediator的方法处理;在warehoustColleague仓储子系统类中,每次调用inc,dec方法,都是先判断保存的total、threshold值是否在要求范围内,当total数量小于threshold时,会通过调用mediator来通知采购子系统,当total数量增加时,通知销售子系统仓储数量增加;
3、通过定义abstractMediator来保存三个子系统的实例,stockMediator通过继承abstractMediator类,实现execute方法,来实现三个子系统在调用过程中的逻辑处理,以此来完成通信。

代码运行结果如下:
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
WARNING: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
PURCHASE:Get Notice -- Sold 120

中介者模式应用场景:

1、设计类图时,出现了网状结构时,可以考虑将类图设计成星型结构,这样就可以实现中介模式;
2、适用于一组对象以定义良好但是复杂的方式进行通信的场合;
3、想定制一个分布在多个类中的行为,而又不想生成太多的子类的场合。

优缺点分析

优点

1、减少类与类的依赖,降低了类和类之间的耦合;
2、容易扩展规模。

缺点

1、当处理逻辑较多时,会造成中介者本身的复杂性较大。

  

posted @ 2017-11-19 20:11  Tomorrow1  阅读(300)  评论(0)    收藏  举报