Chinaunix首页 | 论坛 | 博客
  • 博客访问: 395927
  • 博文数量: 272
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 2706
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
  • 认证徽章:
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(272)

文章存档

2018年(42)

2017年(80)

2016年(80)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2018-02-24 15:12:39

看了个韩国人做的Python Design Pattern视频,决定把以前的代码拿出来重新复习下.


点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-

  3. from abc import ABCMeta,abstractmethod

  4. class Order(metaclass=ABCMeta):
  5.     @abstractmethod
  6.     def execute(self):
  7.         pass

  8. class BuyStockOrder(Order):
  9.     def __init__(self,stock):
  10.         self.stock=stock

  11.     def execute(self):
  12.         self.stock.buy()

  13. class SellStockOrder(Order):
  14.     def __init__(self,stock):
  15.         self.stock=stock

  16.     def execute(self):
  17.         self.stock.sell()

  18. class StockTrade:
  19.     def buy(self):
  20.         print("You will buy stocks")
  21.     
  22.     def sell(self):
  23.         print("You will sell stocks")


  24. class Agent:
  25.     def __init__(self):
  26.         self.__orderQueue=[]

  27.     def placeOrder(self,other):
  28.         self.__orderQueue.append(other)
  29.         other.execute()

  30. def test_agent():
  31.     #client
  32.     stock=StockTrade()
  33.     buyStock=BuyStockOrder(stock)
  34.     sellStock=SellStockOrder(stock)

  35.     #Inovker
  36.     agent=Agent()
  37.     agent.placeOrder(buyStock)
  38.     agent.placeOrder(sellStock)

  39. test_agent()

点击(此处)折叠或打开

  1. root@kali:/usr/local/src/py/design_pattern# ./command_pattern.py
  2. You will buy stocks
  3. You will sell stocks
2. Mediator pattern

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Author: hezhb
  4. # Created Time: Sat 24 Feb 2018 03:21:20 PM CST

  5. from abc import ABCMeta,abstractmethod
  6. import sys

  7. class Colleague(metaclass=ABCMeta):
  8.     
  9.     def __init__(self, mediator, id):
  10.         self._mediator = mediator
  11.         self._id = id
  12.     
  13.     def getId(self):
  14.         return self._id
  15.     
  16.     @abstractmethod
  17.     def send(self, msg):
  18.         pass
  19.     
  20.     @abstractmethod
  21.     def receive(self, msg):
  22.         pass
  23.         

  24. class ConcreteColleague(Colleague):
  25.     def __init__(self, mediator, id):
  26.         super().__init__(mediator, id)
  27.     
  28.     def send(self, msg):
  29.         print("Message {} sent by Colleague {}".format(msg, self._id))
  30.         self._mediator.distribute(self, msg)
  31.     
  32.     def receive(self, msg):
  33.         print("Message {} received by Colleague {}".format(msg, self._id))
  34.     

  35. class Mediator(metaclass=ABCMeta):
  36.     
  37.     @abstractmethod
  38.     def add(self, colleague):
  39.         pass
  40.     
  41.     @abstractmethod
  42.     def distribute(self, sender, msg):
  43.         pass
  44.     

  45. class ConcreteMediator(Mediator):
  46.     def __init__(self):
  47.         Mediator.__init__(self)
  48.         self._colleagues = []
  49.     
  50.     def add(self, colleague):
  51.         self._colleagues.append(colleague)
  52.         
  53.     def distribute(self, sender, msg):
  54.         for coll in self._colleagues:
  55.             if coll.getId() != sender.getId():
  56.                 coll.receive(msg)

  57. def main():
  58.     mediator = ConcreteMediator()
  59.     
  60.     c1 = ConcreteColleague(mediator, 1)
  61.     c2 = ConcreteColleague(mediator, 2)
  62.     c3 = ConcreteColleague(mediator, 3)
  63.     
  64.     mediator.add(c1)
  65.     mediator.add(c2)
  66.     mediator.add(c3)
  67.     
  68.     c1.send("Good morning")
  69.     
  70.     
  71. if __name__=="__main__":
  72.     main()

  73. #pyzo里面执行
  74. In [12]: %run -i /usr/local/src/py/design_pattern/mediator_pattern.py
  75. Message Good morning sent by Colleague 1
  76. Message Good morning received by Colleague 2
  77. Message Good morning received by Colleague 3



阅读(101) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册