Python设计模式-中介者模式
代码基于3.5.2,代码如下;
class colleague():
mediator = None
def __init__(self,mediator):
self.mediator = mediator
class purchaseColleague(colleague):
def buyStuff(self,num):
print("PURCHASE:Bought {0}".format(num))
self.mediator.execute("buy",num)
def getNotice(self,content):
print("PURCHASE:Get Notice -- {0}".format(content))
class warehoustColleague(colleague):
total = 0
threshold = 100
def setThreshold(self,threshold):
self.threshold = threshold
def isEnough(self):
if self.total < self.threshold:
print("WARNING:Warning ... Stock is low ..")
self.mediator.execute("warning",self.total)
return False
else:
return True
def inc(self,num):
self.total += num
print("WAREHOUSE:Increase {0}".format(num))
self.mediator.execute("increase",num)
def dec(self,num):
if num > self.total:
print("WAREHOUSE:Error ... Stock is not enough")
else:
self.total -= num
print("WAREHOUSE:Decrease {0}".format(num))
self.mediator.execute("decrease",num)
self.isEnough()
class salesColleague(colleague):
def sellStuff(self,num):
print("SALES:Sell {0}".format(num))
self.mediator.execute("sell",num)
def getNotice(self,content):
print("SALES:Get Notice -- {0}".format(content))
class abstractMediator():
purchase = None
sales = None
warehouse = None
def setPurchase(self,purchase):
self.purchase = purchase
def setWarehouse(self,warehouse):
self.warehouse = warehouse
def setSales(self,sales):
self.sales = sales
def execute(self,content,num):
pass
class stockMediator(abstractMediator):
def execute(self,content,num):
print("MEDIATOR:Get Info -- {0}".format(content))
if content == "buy":
self.warehouse.inc(num)
self.sales.getNotice("Bought {0}".format(num))
elif content == "increase":
self.sales.getNotice("Inc {0}".format(num))
self.purchase.getNotice("Inc {0}".format(num))
elif content == "decrease":
self.sales.getNotice("Dec {0}".format(num))
self.purchase.getNotice("Dec {0}".format(num))
elif content == "warning":
self.sales.getNotice("Stock is low {0} left".format(num))
self.purchase.getNotice("Stock is low. Please Buy More {0}".format(num))
elif content == "sell":
self.warehouse.dec(num)
self.purchase.getNotice("Sold {0}".format(num))
else:
pass
if __name__ == "__main__":
mobile_mediator = stockMediator()
mobile_purchase = purchaseColleague(mobile_mediator)
moblie_warehouse = warehoustColleague(mobile_mediator)
moblie_sales = salesColleague(mobile_mediator)
mobile_mediator.setPurchase(mobile_purchase)
mobile_mediator.setWarehouse(moblie_warehouse)
mobile_mediator.setSales(moblie_sales)
moblie_warehouse.setThreshold(200)
mobile_purchase.buyStuff(300)
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、当处理逻辑较多时,会造成中介者本身的复杂性较大。