Mode explanation The responsibility chain pattern is an object's behavior pattern, which links those objects that handle client requests into a chain and passes the request along the chain until an object has processed it. Usually used in the following situations 1 There are multiple objects can handle a request, which object processing the request is not known in advance, but is automatically determined at run time; 2 want to submit a request to one of multiple objects without explicitly formulating a receiver; 3 when a collection of objects for processing a request needs to be specified dynamically; 16.2 Structure and Usage 1. Abstract Processor (Thandler)-Defines an interface for processing requests. If necessary, declare a method that implements the link to the subsequent object; 2. The specific processor (Tconcretehandler)-is responsible for handling the request; Links subsequent objects, if it can handle the requests, then processing, if not, pass to its linked object processing; 3. Client (TClient)-sends a request to a specific processing object in the chain of responsibility; code example: Unit Chain;interfacetype Thandler = class private Fsuccessor:thandler; Procedure Setsuccessor (const value:thandler); Public procedure HandleRequest; Virtual Abstract Property Successor:thandler read Fsuccessor write setsuccessor; End TConcreteHandler1 = Class (Thandler) public procedure HandleRequest; Override End TConcreteHandler2 = Class (Thandler) public procedure HandleRequest; Override End;implementationuses Dialogs; {Thandler}procedure thandler.setsuccessor (const value:thandler); begin fsuccessor: = Value;end; {TConcreteHandler2}procedure tconcretehandler2.handlerequest;begin inherited; If SuCcessor <> Nil THEN begin showmessage (self. ClassName + ' Pass the request to ' + Successor.classname '); Successor.handlerequest; End ELSE begin showmessage (self. ClassName + ' processed the request! ‘); End;end; {TConcreteHandler1}procedure tconcretehandler1.handlerequest;begin inherited; If successor <> Nil then begin showmessage (self. ClassName + ' Pass the request to ' + Successor.classname '); Successor.handlerequest; End ELSE begin showmessage (self. ClassName + ' processed the request! ‘); End;end;end. Client code unit main;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dial OGs, Stdctrls;type TForm1 = Class (Tform) Otherdone:tbutton; Selfdone:tbutton; Procedure Otherdoneclick (Sender:tobject); Procedure Selfdoneclick (Sender:tobject); Private {Private declarations} public {public declarations} End;var form1:tform1;implementationuses Chain; {$R *.dfm}procedure Tform1.otherdoneclick (sender:tobject); var Handler1, Handler2:thandler;beginHandler1: = tconcretehandler1.create; Handler2: = tconcretehandler1.create; Try handler1.successor: = Handler2; Handler1.handlerequest; Finally Handler1.free; Handler2.free; End;end;procedure Tform1.selfdoneclick (sender:tobject); var Handler1, handler2:thandler;begin Handler1: = Tconcretehandler1.create; Handler2: = tconcretehandler1.create; Try Handler1.handlerequest; Finally Handler1.free; Handler2.free; End;end;end.
Delphi design mode: "Headfirst design mode"---behavior mode of responsibility chain mode