Template mode Delphi implementation

zhaozj2021-02-11  238

Unit TPL_METH;

{Design Pattern Example -Some Simple Classes Using The Template Method Pattern.} Interface

type TAbstractTemplateClass = class (TObject) protected function Algorithm_StepA: Integer; virtual; abstract; function Algorithm_StepB: Integer; virtual; abstract; function Algorithm_StepC: Integer; virtual; abstract; public function Algorithm: Integer; end;

TConcreteClassA = class (TAbstractTemplateClass) protected function Algorithm_StepA: Integer; override; function Algorithm_StepB: Integer; override; function Algorithm_StepC: Integer; override; end;

TConcreteClassB = class (TAbstractTemplateClass) protected function Algorithm_StepA: Integer; override; function Algorithm_StepB: Integer; override; function Algorithm_StepC: Integer; override; end;

IMPLEMentation

Function TabstractTemplateClass.Algorithm: Integer; Begin Result: = Algorithm_Stepa Algorithm_Stepb Algorithm_Stepc; End;

Function tconcreteclassa.algorithm_stepa: integer; begin result: = 1;

Function TCONCRETECLASSA.ALGORITHM_STEPB: INTEGER; Begin Result: = 2;

Function tconcreteclassa.algorithm_stepc: integer; begin result: = 3;

Function tconcreteclassb.algorithm_stepa: integer; begin result: = 4;

Function TCONCRETECLASSB.Algorithm_stepb: integer; begin result: = 5;

Function tconcreteclassb.algorithm_stepc: integer; begin result: = 6;

End.

/

Unit unit1;

Interface

Uses sysutils, Wintypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls

type TForm1 = class (TForm) Button1: TButton; Button2: TButton; procedure Button1Click (Sender: TObject); procedure Button2Click (Sender: TObject); private {Private declarations} public {Public declarations} end; var Form1: TForm1;

IMPLEMENTATION

{$ R * .dfm}

Uses TPL_METH;

procedure TForm1.Button1Click (Sender: TObject); var AnAbstractObject: TAbstractTemplateClass; begin AnAbstractObject: = TConcreteClassA.Create; try ShowMessage ( 'Algorithm Result =' Inttostr (AnAbstractObject.Algorithm)); finally AnAbstractObject.Destroy; end; end;

procedure TForm1.Button2Click (Sender: TObject); var AnAbstractObject: TAbstractTemplateClass; begin AnAbstractObject: = TConcreteClassB.Create; try ShowMessage ( 'Algorithm Result =' Inttostr (AnAbstractObject.Algorithm)); finally AnAbstractObject.Destroy; end; end;

End.

转载请注明原文地址:https://www.9cbs.com/read-5778.html

New Post(0)