Responsibility Chain Mode lua implementation

Source: Internet
Author: User

-- [Responsibility chain mode: Enables multiple objects to process requests to avoid coupling between request senders and receivers. Connect the object to a chain and pass the request along the chain until an object processes it. Application: 1. There are multiple objects to process the request. How can this problem be solved. 2. You want to submit a request to one of multiple objects without specifying the receiver explicitly. 3. The object set that can process a request should be dynamically specified.] -- Handler = {} function Handler: new (o) o = o or {} setretriable (o, self) self. _ index = selfreturn o; endfunction Handler: SetSuccessor (s) self. successor = sendConcreteHandler1 = Handler: new () function ConcreteHandler1: HandleRequest (r) if r <= 10 thenprint ("Handler1 processes requests") elseif self. successor thenself. successor: HandleRequest (r) endendConcreteHandler2 = Handler: new () function ConcreteHandler2: HandleRequest (r) I F r> 10 and r <= 20 thenprint ("Handler2 processes the request") elseif self. successor thenself. successor: HandleRequest (r) endconcretehandler3 = Handler: new () function ConcreteHandler3: HandleRequest (r) if r> 20 thenprint ("Handler3 processes the request") elseif self. successor thenself. successor: HandleRequest (r) endendh1 = ConcreteHandler1: new () h2 = ConcreteHandler2: new () h3 = ConcreteHandler3: new () h1: SetSuccessor (h2) h2: SetSuccessor 3) print ("********************* instance 1 ************** *********") h1: HandleRequest (25) h2: HandleRequest (5) h3: HandleRequest (15) print ("********************* instance 2 ************** *********") h1: HandleRequest (25) h1: HandleRequest (5) h1: HandleRequest (15) -- [[PS: "I understood" the responsibility chain mode, so I couldn't help thinking: Nima, is this the status mode ?! Later, I found out the difference: State mode: The processing object of the next state has already been set in the class. Responsibility Chain Mode: The processing object of the next request is set by the client.] --
Status mode lua implements the output: * ******************* instance 1 ***************** ******* * *************** Handler3 processes the request Handler1 and the request Handler2 processes the request

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.