Big talk design pattern python implementation-responsibility chain mode

Source: Internet
Author: User

Responsibility chain mode (Chain of Responsibility): Enables multiple objects to have the opportunity to process requests, thus avoiding coupling between sender and receiver. Chains the object and passes the request along the chain until it is processed

Here is a demo of the design pattern:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 __author__='Andy'5 """6 Liar design Mode7 design pattern--responsibility chain mode8 Responsibility Chain mode (Chain of Responsibility): Enables multiple objects to have the opportunity to process requests, thus avoiding coupling between sender and receiver. Chains the object and passes the request along the chain until it is processed9 (at the time of invocation, define which instance is the duty-on level of which instance) the request is passed along the defined chain to the object that can process the requestTen """ One #abstract a processing class A classHandle (object): -  -     def __init__(self): theSelf.successor ="' -  -     defSetsuccessor (self, successor): -Self.successor =successor +  -     defhandle_request (self,request): +         Pass A  at #specific Processor Class 1 - classConcreteHandle1 (Handle): -  -     defhandle_request (self,request): -         ifRequest>0 andrequest<=10: -             Print "ConcreteHandle1 Processing Requests", Request in         Else: - self.successor.handle_request (Request) to  + #specific Processor Class 2 - classConcreteHandle2 (Handle): the  *     defhandle_request (self,request): $         ifRequest>10 andRequest<=20:Panax Notoginseng             Print "ConcreteHandle2 Processing Requests", Request -         Else: the self.successor.handle_request (Request) +  A if __name__=="__main__": theC1 =ConcreteHandle1 () +C2 =ConcreteHandle2 () - c1.setsuccessor (C2) $  $      forIinchRange (6,15,2): -C1.handle_request (i)

The design of the above class is as follows:

Neither the receiver nor the sender has clear information about the other, and the object in the chain does not know the structure of the chain, and the chain of responsibility simplifies the connection of the objects, they only need to maintain a reference to the successor, without the need to maintain a reference to all of his candidate recipients, greatly reducing the coupling degree, The structure that handles a request can be added or modified at any time

Be careful, however, that a request is reached at the end of the chain of responsibility and is not properly configured to be processed.


Andy
Source: http://www.cnblogs.com/onepiece-andy/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the author's consent must be in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Big talk design pattern python implementation-responsibility chain mode

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.