Unit Unit2
Factory Method Review: Up to the first large class (abstract factory) in the design mode (object creation mode) in the design mode. Say and understand, it is actually Use one: mytest.msg; to create different methods of different subclasses, create different subclasses of different classes. That is to say that the subclasses can be: tsubclasstene.create; tsubclasstwo.create; .. .......... Call the statement mytest.test; don't change anything, you don't have to know what is one, two, or three, you can also be Four, FIVE ....... ... From a sense, it is a polymorph of instance creation. (I don't know how to describe it, please point out, thank you). Under normal circumstances, I make an example as simple as possible. / / --------------------------------------------------- ------------------------------------------ Well, review is this. Original I want to write down in order, just the example of Builder, I can't think of simple point, so I will skip it first (I want to make a simple point to make up), this time I will talk about Factory Method (factory method) Unit Unit2; Interfaceuses Windows, Dialogs, Sysutils, Classes; Type // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------- TPRoductId = (One, Two, Three); tproduct = Class // products defined parent class public constructor Create; virtual; end; TProductOne = class (TProduct) // define a product constructor Create; override; end; TProductTwo = class (TProduct) // define two product constructor Create; override; end; TProductThree = Class (tproduct) // Define products Three constructor create; override; end; // is Of course. Of course, FOUR, FIVE ................... ------------- -------------------------------------------------- ------ TCREATOR = Class // Define the creation of the creation, implementation of the default definition public constructor create; Virtual; function createproduct (ProductID: tproductID): TPRODUCT; Virtual; end; TMYCREATOR = Class (Tcreator) // Definition My creator, realize my definition constructor create; override; function createproduct (ProductID: tproductid): tproduct; override; end; // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- implementationconstructor TCreator.Create; begin // end; function TCreator.CreateProduct (ProductID: TProductID): TProduct; var aProduct: TProduct; begin aProduct: = nil; if ProductID =
ONE THEN APRODUCT: = TPRODUCTONE.CREATE; if ProductId = Two Then Aproduct: = tproductTwo.create; Result: = APRODUCT; END; / / ---------------------------------------------------------------------------- -------------------------------------------------- --constructor TMyCreator.Create; begin // inherited; end; function TMyCreator.CreateProduct (ProductID: TProductID): TProduct; var aProduct: TProduct; aCreator: TCreator; begin aProduct: = nil; // swap One of my definitions and Two; if ProductID = One then aProduct: = TProductTwo.Create; if ProductID = Two then aProduct: = TProductOne.Create; if ProductID = Three then aProduct: = TProductThree.Create; if aProduct = nil then // if the above object is created Failure, then call the Factory method of the parent class Begin Acreator: = tcreator.create; Aproduct: = AcReator.createProduct (ProductID); end; = APRODUCT; END; // -----------; // ----------- -------------------------------------------------- --------------- {tproductone} Constructor TPRoductone.create; Begin ShowMessage ('this is tproductone'); EN d; {TProductThree} constructor TProductThree.Create; begin showmessage ( 'This is TproductThree'); end; {TProductTwo} constructor TProductTwo.Create; begin showmessage ( 'This is TproductTwo'); end; {TProduct} constructor TProduct.Create; BEGIN / / END; END. In general, the factory method is to deliver different parameters to the Factory object to return an object with the same base class or implement the same interface.