Design mode, description with Delphi -> Factory Method mode

zhaozj2021-02-08  189

Factory Method mode (factory method)

origin

The Factory Method mode in Delphi is extended in the basic Factory Method mode. For more information on Factory Method mode, please refer to [GAM ]

purpose

Define an interface for creating an object, which allows the subclass to decide which class is instantiated. Factory Method mode delays the instantiation of a class to their subclasses.

motivation

Unlike Abstract Factory: Abstract Factory is used to create a specific object, while Factory Method is used to create classes. Abstract Factory mode is a collection of factory methods (Factory Methods).

This mode encapsulates the class structure and completely separated the specific class with the client.

One such example, you have an object-oriented commercial system, you may have to handle multiple target database systems. Time, the client only needs to know the relevant business class, but there is no need to understand their specific application: storage, get.

application

In the example of Abstract Factory, each virtual feature constructor is a Factory Method. A specified feature class is returned in their implementation.

TredspeedButton = Class (tspeedbutton)

public

Constructor Create (Aowner: Tcomponent); OVERRIDE;

END;

Constructor TredspeedButton.create (Aowner: Tcomponent);

Begin

Inherited Create (Aowner);

Font.color: = CLRED;

END;

Function ToredFactory.createspeedButton (Aowner: Tcomponent): TspeedButton;

Begin

Result: = TREDspeedButton.create (Aowner);

END;

Code above:

· ToredFactory defines a factory method and returns a TREDSPEEDBUTTON instance

And returning to the object is TREDSPEEDBUTTON.

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

New Post(0)