This article published in the Monopoly has been nearly one month, as a relatively strong content, reflecting more enthusiastic and reprinted, because my Blog has been opened, will now re-release and delete the original post on the Monopoly ,Please indicate the source. The following is the text: It took about half a year ago, "Java and Mode" inside, the factory model is basically understanding, when Delphi has rewritten several routines, now take it out, share it with you, The comment inside is my understanding of this design pattern, very sided, if there is any mistake I hope everyone criticizes the correct. Factory model is divided into simple factory models, factory method patterns and abstract factory models. The simple factory model introduced here is the simplest of them. If you support, I will continue to post out the factory method model and the abstract factory model, etc., to see everyone's reaction! Learning design mode should have some understanding of object-oriented programming, especially polymorphism, if you understand the following example, there is no problem, huh, huh!
// Fruit class, it is an abstract product TFRUIT = Class (TOBJECT) ... END; // Apple, fruit type avatar TAPPLE = Class (TFRUIT) ... End; function factory (): tfruit; var f: tfruit; begin // The essence is this statement. It is clear that the Tapple object is created, but he assigns him to the TFRuit type variable // In fact, it is very good, and it will experience f: = Tapple.create (); Result: = f; End;
I used in the routine.
Interface, don't understand it as a abstract class that is more abstract than the abstract class, saying that it is right as a class as a class. Let's start below. . . . . . .
This is a description:
// We use a small orchard to explain what is a simple factory // this orchard, all fruits have the task of growing, farming and harvesting three steps // orchards. Get the three fruit objects of grapes, apples and strawberries // we use the obtained objects to complete fruit growth, farming and harvest three steps // orchard is what we said (Factory) // and grapes, apples and strawberries These three fruits are the benefits of using simple factories in the factory in the factory (pruduce) // using simple factories: // 1, full use of polymorphism // No matter you What, the object returned by the orchard is not a specific grape, apple or strawberry // but returns an abstract object-fruit (IFRUIT) // 2, which is used to use the encapsulation // internal product Unaffected // His disadvantage is: // If you add new products, you must modify the factory (Factory)
This is the unit file source code for defining simple factories:
//Simplefactory.PAS Defines the simple factory unit file // code is as follows ==========1 SimpleFactory; interfaceuses sysutils; type // fruit class, it is an abstract product // only declares all objects The interface does not realize their ifRuit = interface (ieinterface) function growth: string; // Growth Function Harvest: String; // Harvest Function Plant: String; // Tillage End; // Grape class, Fruit Class avatar TGRAPE = class (TInterfacedObject, IFruit) function Grow: string; function Harvest: string; function Plant: string; end; // apples, fruits embodying TApple = class (TInterfacedObject, IFruit) function Grow: string; function Harvest: String; Function Plant: String; End; // Strawberry Class, Fruit Class TSTRAWBERRY = Class (TinterFaceDObject, IFRUIT) Function Grow: String; Function Harvest: String; Function Plant: String; End; // Orchard, it It is the factory class, which is responsible for the example of three fruits. TFRUITGERDENER = Class (TOBJECT) public // 1, pay attention to the class keyword, it defines the factory method Factory is a static function, you can use // 2, pay attention to return value, he Returns the most abstract product IFRUIT fruit class // 3, pay attention to him has a parameter to tell the factory which fruit Class Function Factory (WhichFRuit: String): ifRuit; End; // Declare an exception, this is not a focus Nothisfr UITEXCEPTION = Class (Exception) End; importation {********** TGRAPE **********} Function Tgrape.grow: String; Begin Result: = 'Grapes are growing ... ... '; End; Function Tgrape.harvest: String; Begin Result: =' Grapes can be harvested ... '; end; function tgrape.plant: string; begin result: =' grapes already ... '; end; {********** TAPPLE *********} Function TAPPLE.GROW: STRING; Begin Result: =' Apple is growing .. .... '; end; function payple.harvest: String; Begin Result: =' Apple can have harvested ... '; end; function is.plant: string; begin result: =' Apple is already good ... '; END;
{********** TSTRAWBERRY **********} Function Tstract Tstract: String; Begin Result: = 'Strawberry is growing ...'; end; function tstract string .Harvest: String; Begin Result: = 'Strawberry can be harvested ...'; end; function tstract =PLANT: String; Begin Result: = 'strawberry has been used ...'; end {*********** TFRUITGARDENER **********} Class Function TFRuitner.Factory (WhichFRuit: String): iFruit; Begin // Entry is this statement: = TAPPLE .Create () // Do not understand what to review and review what is polymorphism if (LowerCase (Whichfruit) = 'apple') Then Result: = Tapple.create () Else If (WHICASE (WhichFRuit) = 'grape') THEN Result: = tgrape.create () Else If (Whichfruit) = 'strawberry') Then Result: = tstractrawberry.create () Else Raise NothisFRuiteXception.create ('This fruit has not been planted!'); end; Form interface:
//MainForm.pas form file, here illustrate how simple factory unit MainForm; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, SimpleFactory, StdCtrls; type TForm1 = class (TForm) RadioButton1: TRadioButton; radioButton2: TRadioButton; RadioButton3: TRadioButton; RadioButton4: TRadioButton; procedure RadioButton1Click (Sender: TObject); procedure RadioButton2Click (Sender: TObject); procedure RadioButton3Click (Sender: TObject); procedure RadioButton4Click (Sender: TObject); public procedure Produce ( Fruitname: string; end; var form1: tform1; importation {********** TFORM1 **********} // This is the temporary variable f of the production process // imp What kind of fruit is you knowing, interesting, what you want to type, orchard! Procedure tForm1.Produce (FruitName: string); var f: iFRuit; begin try f: = tfruitner.Factory; showMessage (f.grow ()); showMessage (F.GROW ()); showMessage (f.harvest) ()); EXCEPT ON E: NothisFRuitexception do messagedlg (E.MESSAGE, MTINFORMATION, [Mbok], 0); end; end; {$ r * .dfm} procedure tform1.radiobutton1click (sender: TOBJECT); Begin Produce (' apple '); end; procedure TForm1.RadioButton2Click (Sender: TObject); begin Produce (' grape '); end; procedure TForm1.RadioButton3Click (Sender: TObject); begin Produce (' strawberry '); end; procedure TForm1.RadioButton4Click (Sender: TOBJECT); Begin Produce ('Other'); end; end;