Pattern "chain of duties"
-
How do you put it in delphi?
-
You can read the patter in the book. http://www.ozon.ru/context/detail/id/2457392/ or that http://codelib.ru/pattern/chain_of_responsibility/ who, in fact, duplicates information from the relevant book.
From the more adequate realities of nations
Delphi
The most understandable is:unit Pattern;
interface
uses SysUtils;
type
TPurchase = class
public
Number: integer;
Amount: Double;
Purpose: string;
constructor Create(num: integer; am: Double; pur: string);
end;IApprover = interface
['{3ACA3967-FFCF-48A1-AC45-9A9B98A8DD96}']
procedure SetSuccessor(successor: IApprover);
procedure ProcessRequest(purchase: TPurchase);
end;TApprover = class(TInterfacedObject, IApprover)
protected
FSuccessor: IApprover;
public
procedure SetSuccessor(successor: IApprover);
procedure ProcessRequest(purchase: TPurchase); virtual; abstract;
end;TDirector = class(TApprover)
procedure ProcessRequest(purchase: TPurchase); override;
end;TVicePresident = class(TApprover)
procedure ProcessRequest(purchase: TPurchase); override;
end;TPresident = class(TApprover)
procedure ProcessRequest(purchase: TPurchase); override;
end;implementation
{ TApprover }
procedure TApprover.SetSuccessor(successor: IApprover);
begin
FSuccessor := successor;
end;{ TDirector }
procedure TDirector.ProcessRequest(purchase: TPurchase);
begin
if purchase.Amount < 10000.0 then
WriteLn(Format('Director approved request # %d', [purchase.Number]))
else if FSuccessor nil then
FSuccessor.ProcessRequest(purchase);
end;{ TVicePresident }
procedure TVicePresident.ProcessRequest(purchase: TPurchase);
begin
if purchase.Amount < 25000.0 then
WriteLn(Format('VicePresident approved request # %d', [purchase.Number]))
else if FSuccessor nil then
FSuccessor.ProcessRequest(purchase);
end;{ TPresident }
procedure TPresident.ProcessRequest(purchase: TPurchase);
begin
if purchase.Amount < 100000.0 then
WriteLn(Format('President approved request # %d', [purchase.Number]))
else
WriteLn(Format('Request# %d requires an executive meeting!', [purchase.Number]))
end;{ TPurchase }
constructor TPurchase.Create(num: integer; am: Double; pur: string);
begin
Number := num;
Amount := am;
Purpose := pur;
end;end.
program Behavioral.ChainOfResponsibility.Pattern;
{$APPTYPE CONSOLE}
uses
SysUtils,
Pattern in 'Pattern.pas';var
Director: IApprover;
VicePresident: IApprover;
President: IApprover;
Purchase: TPurchase;begin
ReportMemoryLeaksOnShutDown := DebugHook <> 0;
try
Director := TDirector.Create;
VicePresident := TVicePresident.Create;
President := TPresident.Create;try Director.SetSuccessor(VicePresident); VicePresident.SetSuccessor(President); Purchase := TPurchase.Create(2034, 350.00, 'Supplies'); Director.ProcessRequest(Purchase); Purchase.Free; Purchase := TPurchase.Create(2035, 32590.10, 'Project X'); Director.ProcessRequest(Purchase); Purchase.Free; Purchase := TPurchase.Create(2036, 122100.00, 'Project Y'); Director.ProcessRequest(Purchase); ReadLn; finally Purchase.Free; end;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.