Abstract Factory mode (abstract factory)
origin
The Abstract Factory mode in Delphi is extended in the basic Abstract Factory mode. For more information on Abstract Factory mode, please refer to [Gam ]
purpose
Provides an interface to create a series of related or interdependent objects, no specific classes are required to specify their specific classes.
motivation
This mode is the best way to classify your application and specific classification, for example, you have to cover Delphi's just VCL
You can create an abstract factory to implement your own components.
application
The following example uses an abstract factory and two actual class factories to achieve different characteristic user interface components. ToabstractFactory is a single component separate class that usually only needs a factory in each product.
ToabstractFactory = Class (TOBJECT)
public
Constructor crete;
DESTRUCTOR DESTROY; OVERRIDE;
{Abstract constructor}
Function CreatespeedButton (Aowner: Tcomponent): TspeedButton; Virtual; ABSTRACT;
Function CreateEdit (Aowner: Tcomponent): Tedit; Virtual; Abstract
Function CreateLabel (Aowner: Tcomponent): TLABEL; Virtual; Abstract
END;
ToredFactory with ToblueFactory overloads abstract interfaces to support different feature interfaces.
ToredFactory = Class (ToabstractFactory)
public
{Red constructor}
Function CreatespeedButton (Aowner: Tcomponent): TspeedButton; Override;
Function CreateEdit (Aowner: Tcomponent): TEDIT; OVERRIDE;
Function CreateLabel (Aowner: Tcomponent): TLABEL; OVERRIDE
END;
ToblueFactory = Class (ToabstractFactory)
public
Blue constructor}
Function CreatespeedButton (Aowner: Tcomponent): TspeedButton; Override;
Function CreateEdit (Aowner: Tcomponent): TEDIT; OVERRIDE;
Function CreateLabel (Aowner: Tcomponent): TLABEL; OVERRIDE
END;
There is a top interface:
· Declare an interface to create an abstract product object: ToabstractFactory
¨ TOABSTRACTFACTORY has three abstract factory methods CreatespeedButton, CreateEdit, CreateLabel
· ToredFactory, TOBLUEFACTORY Method for creating specific product objects
Here is the implementation code of ToredFactory:
· Unit Redfact;
Interface
· Uses
· Classes, Sysutils, Stdctrls, Graphics, AbstFact, Buttons
· Type
· ToredFactory = Class (ToabstractFactory)
PUBLIC
· {Actual constructor}
· Function CreatespeedButton (Aowner: Tcomponent): TspeedButton; Override; Function CreateEdit (Aowner: Tcomponent): Tedit; OVERRIDE
· Function CreateLabel (Aowner: Tcomponent): TLABEL; OVERRIDE
· END;
·
· Implementation
· {Specific construct, hidden by factory methods}
· Type
· TREDSPEEDBUTTON = Class (TspeedButton)
PUBLIC
· Constructor Create (Aowner: tComponent); OVERRIDE;
· END;
·
· TREDEDIT = Class (TEDIT)
PUBLIC
· Constructor Create (Aowner: tComponent); OVERRIDE;
· END;
·
· TREDLABEL = Class (TLABEL)
PUBLIC
· Constructor Create (Aowner: tComponent); OVERRIDE;
· END;
·
{Widget Implementation}
·
{TREDBUTTON}
·
· Constructor TredspeedButton.create (Aowner: Tcomponent);
Begin
· Inherited CREATE (AOWNER);
· Font.color: = CLRED;
· END;
·
· {TREDEDIT}
·
Conntructor TrededIt (Aowner: Tcomponent);
Begin
· Inherited CREATE (AOWNER);
· Font.color: = CLRED;
· END;
·
{TREDLABEL}
·
· Constructor TREDLABEL.CREATE (Aowner: Tcomponent);
Begin
· Inherited CREATE (AOWNER);
· Font.color: = CLRED;
· END;
·
·
{THE Concrete Factory}
·
· Function ToredFactory.createspeedButton (Aowner: Tcomponent): tspeedbutton;
Begin
· Result: = TREDspeedButton.create (Aowner);
· END;
·
· Function ToredFactory.createEdit (Aowner: Tcomponent): tedit;
Begin
· Result: = TREDEDIT.CREATE (AOWNER);
· END;
·
· Function ToredFactory.createLabel (Aowner: Tcomponent): TLABEL;
Begin
· Result: = TREDLABEL.CREATE (AOWNER); · end;
End.
When running, our client program is created by the class of the class. And the client does not need to know the specific subclass of the factory.
Delphi instance
Be organized