Example of using the responsibility chain mode and iterator mode in the design mode in Python

Source: Internet
Author: User
Tags class manager
This article describes how to use the responsibility chain mode and iterator mode in the design mode in Python. Both the responsibility chain mode and the iterator mode can be viewed as the behavior-type design mode, for more information, see Responsibility Chain Model

Responsible Chain Mode: connects the objects that can process the request into a chain and transmits the request along this chain until an object processes the request, avoid coupling between request senders and receivers.

# Encoding = UTF-8 ## by panda # responsibility connection mode def printInfo (info): print unicode (info, 'utf-8 '). encode ('gbk') # Abstract responsibility class Manager (): successor = None name = ''def _ init _ (self, name): self. name = name def SetSuccessor (self, successor): self. successor = successor def HandleRequest (self, request): pass # responsibility class: Manager class CommonManager (Manager): def HandleRequest (self, request): if request. requestType = 'offset' D request. number <= 2: printInfo ('% s: % s quantity % d approved' % (self. name, request. requestContent, request. number) else: if self. successor! = None: self. successor. handleRequest (request) # specific responsibilities: director class Majordomo (Manager): def HandleRequest (self, request): if request. requestType = 'offset' and request. number <= 5: printInfo ('% s: % s quantity % d approved' % (self. name, request. requestContent, request. number) else: if self. successor! = None: self. successor. handleRequest (request) # specific responsibilities: general Manager class GeneralManager (Manager): def HandleRequest (self, request): if request. requestType = 'Leave of absence ': printInfo (' % s: % s quantity % d approved '% (self. name, request. requestContent, request. number) elif request. requestType = 'salary raise 'and request. number <= 500: printInfo ('% s: % s quantity % d approved' % (self. name, request. requestContent, request. number) elif request. requestType = 'salary raise 'and request. number> 500: printInfo ('% s: % s quantity % d.' % (self. name, request. requestContent, request. number) class Request (): RequestType = ''RequestContent ='' Number = 0 def clientUI (): jinLi = CommonManager ('kingli ') zongJian = Majordomo ('zongjian ') zhongJingLi = GeneralManager ('zhong jinlil') jinLi. setSuccessor (zongJian) zongJian. setSuccessor (zhongJingLi) request = Request () request. requestType = 'offset' request. requestContent = 'Food offset' request. number = 1 jinLi. handleRequest (request) request. requestType = 'offset' request. requestContent = 'Food offset' request. number = 5 jinLi. handleRequest (request) request. requestType = 'salary raise 'request. requestContent = 'Food requires a raise 'request. number = 500 jinLi. handleRequest (request) request. requestType = 'salary raise 'request. requestContent = 'Food requires a raise 'request. number = 1000 jinLi. handleRequest (request) return if _ name _ = '_ main _': clientUI ();

Class diagram:

Iterator Mode
Iterator mode: provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.

Python provides built-in support for this mode, so you do not need to write it yourself,

# Encoding = UTF-8 ## by panda # Iterator mode def printInfo (info): print unicode (info, 'utf-8 '). encode ('gbk') # Iterator abstract class Iterator: def First (self): pass def Next (self): pass def IsDone (self): pass def CurrentItem (self ): pass # Set abstract class Aggregate: def CreateIterator (self): pass # specific Iterator class: 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] r Eturn ret # specific collection class ConcreteAggregate (Aggregate): items = None def _ init _ (self): self. items = [] def clientUI (): a = ConcreteAggregate (). items. append ('Big Bird'). items. append ('food'). items. append ('luggage '). items. append ('foreigner '). items. append ('bus internal employee '). items. append ('thief ') printInfo (' --------- iterator mode ------------- ') I = ConcreteIterator (. items) item = I. first () while (False = I. isDone (): printInfo ("% S, please buy a ticket! "% I. currentItem (); I. next () printInfo ('\ n --------- internal python iteration -------------') for item in. items: printInfo ("% s buy a ticket! "% Item); return if _ name _ = '_ main _': clientUI ();

Class diagram:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.