-- [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