Python使用設計模式中的責任鏈模式與迭代器模式的樣本

來源:互聯網
上載者:User
責任鏈模式

責任鏈模式:將能處理請求的對象連成一條鏈,並沿著這條鏈傳遞該請求,直到有一個對象處理請求為止,避免請求的寄件者和接收者之間的耦合關係。

#encoding=utf-8 # #by panda #職責連模式  def printInfo(info):   print unicode(info, 'utf-8').encode('gbk')  #抽象職責類 class Manager():   successor = None   name = ''   def __init__(self, name):     self.name = name      def SetSuccessor(self, successor):     self.successor = successor      def HandleRequest(self, request):     pass  #具體職責類:經理 class CommonManager(Manager):   def HandleRequest(self, request):     if request.RequestType == '請假' and request.Number <= 2:       printInfo('%s:%s 數量%d 被批准' % (self.name, request.RequestContent, request.Number))     else:       if self.successor != None:         self.successor.HandleRequest(request)          #具體職責類:總監 class Majordomo(Manager):   def HandleRequest(self, request):     if request.RequestType == '請假' and request.Number <= 5:       printInfo('%s:%s 數量%d 被批准' % (self.name, request.RequestContent, request.Number))     else:       if self.successor != None:         self.successor.HandleRequest(request)  #具體職責類:總經理 class GeneralManager(Manager):   def HandleRequest(self, request):     if request.RequestType == '請假':       printInfo('%s:%s 數量%d 被批准' % (self.name, request.RequestContent, request.Number))     elif request.RequestType == '加薪' and request.Number <= 500:       printInfo('%s:%s 數量%d 被批准' % (self.name, request.RequestContent, request.Number))     elif request.RequestType == '加薪' and request.Number > 500:       printInfo('%s:%s 數量%d 再說吧' % (self.name, request.RequestContent, request.Number))  class Request():   RequestType = ''   RequestContent = ''   Number = 0  def clientUI():   jinLi = CommonManager('金力')   zongJian = Majordomo('宗健')   zhongJingLi = GeneralManager('鐘金利')      jinLi.SetSuccessor(zongJian)   zongJian.SetSuccessor(zhongJingLi)      request = Request()   request.RequestType = '請假'   request.RequestContent = '小菜請假'   request.Number = 1   jinLi.HandleRequest(request)      request.RequestType = '請假'   request.RequestContent = '小菜請假'   request.Number = 5   jinLi.HandleRequest(request)      request.RequestType = '加薪'   request.RequestContent = '小菜要求加薪'   request.Number = 500   jinLi.HandleRequest(request)      request.RequestType = '加薪'   request.RequestContent = '小菜要求加薪'   request.Number = 1000   jinLi.HandleRequest(request)   return  if __name__ == '__main__':   clientUI(); 

類圖:

迭代器模式
迭代器模式:提供一種方法順序訪問一個彙總對象中的各個元素,而又不暴露該對象的內部表示。

python內建支援這種模式,所以一般來說,不用自己寫,

#encoding=utf-8 # #by panda #迭代器(Iterator)模式  def printInfo(info):   print unicode(info, 'utf-8').encode('gbk')  #迭代器抽象類別 class Iterator:   def First(self):     pass      def Next(self):     pass      def IsDone(self):     pass      def CurrentItem(self):     pass    #集合抽象類別 class Aggregate:   def CreateIterator(self):     pass    #具體迭代器類: class ConcreteIterator(Iterator):   aggregate = None   current = 0   def __init__(self, aggregate):     self.aggregate = aggregate     self.current = 0      def First(self):     return self.aggregate[0]    def Next(self):     ret = None     self.current += 1     if(self.current < len(self.aggregate)):       ret = self.aggregate[self.current]     return ret    def IsDone(self):     if(self.current < len(self.aggregate)):       return False     else:       return True    def CurrentItem(self):     ret = None     if(self.current < len(self.aggregate)):       ret = self.aggregate[self.current]     return ret    #具體集合類 class ConcreteAggregate(Aggregate):   items = None   def __init__(self):     self.items = []        def clientUI():   a = ConcreteAggregate()   a.items.append('大鳥')   a.items.append('小菜')   a.items.append('行李')   a.items.append('老外')   a.items.append('公交內部員工')   a.items.append('小偷')         printInfo('---------迭代器模式-------------')   i = ConcreteIterator(a.items)   item = i.First()   while(False == i.IsDone()):     printInfo("%s 請買車票!" % i.CurrentItem());     i.Next()        printInfo('\n---------python內部迭代-------------')   for item in a.items:     printInfo("%s 請買車票!" % item);   return  if __name__ == '__main__':   clientUI(); 

類圖:

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.