DesignPattern FactoryMethod

xiaoxiao2021-03-06  18

UNIT FACTORY;

InterfaceUses Windows, Messages, Sysutils, Controls, Product

Type ifactoryinterface = interface function factory (): iProductInterface;

Tfactory1 = Class (tcontrol, ifactoryinterface) public function factory (): iProductInterface; end;

Tfactory2 = Class (TControl, ifactoryInterface) public function factory (): iproductinterface; end;

IMPLEMENTATION

Function TFactory1.Factory (): iproductinterface; var Aproduct: TPRoduct1; Begin Aproduct: = tproduct1.create (nil); result: = APRODUCT.CREATEPRODUCT ();

Function TFactory2.Factory (): iproductinterface; var Aproduct: TPRoduct2; Begin Aproduct: = tproduct2.create (nil); result: = APRODUCT.CREATEPRODUCT ();

End.

UNIT Product;

InterfaceUses Windows, Messages, Sysutils, Controls, Dialogs

Type iproductinterface = interface function createproduct (): iProductInterface; end;

TProduct1 = class (TControl, IProductInterface) public function CreateProduct (): IProductInterface; end; TProduct2 = class (TControl, IProductInterface) public function CreateProduct (): IProductInterface; end;

IMPLEMENTATION

Function tproduct1.createproduct (): iProductInterface; begin result: = Self; showMessage ('product1 created');

Function tproduct2.createproduct (): iProductInterface; begin result: = Self; showMessage ('product2 created');

End.

//test

Procedure tForm1.button1click (sender: TOBJECT); var afactory: tFactory1; Begin Afactory: = tFactory1.create (self); afactory.factory; freeandnil; end;

Procedure tForm1.Button2Click (Sender: TOBJECT); VAR AFAACTORY: TFACTORY2; Begin Afactory: = TFactory2.create (self); Afactory.Factory; FreeAndnil (Afactory);

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

New Post(0)