The Builder mode in Delphi extends the basic Builder mode. For more information on Builder mode, please refer to [Gam+]
Separate the construction of a complex object from its representation so that the same construction process can create different representations
A builder is conceptually similar to an abstract factory. But the difference is that the generator generates the various components of a single complex class by referencing different constructors, while the abstract factory allows you to create an entire concrete class. For example: a builder can construct houses, villas, and offices. You can hire different builders to construct brick houses and wooden houses. Although you can specify the shape and size of the house. And other ribs are used to build part of the house, but not all. For example, build windows, doors, and parking lots.
The following example first introduces an abstract class TabstractFormBuilder and its two concrete subclasses TredFormBuilder and TblueFormBuilder. TabstractFormBuilder declares some class constructors
type
TAbstractFormBuilder = class
PRivate
FForm: TForm;
procedure BuilderFormClose(Sender: TObject; var Action: TCloseAction);
protected
function GetForm: TForm; virtual;
public
procedure CreateForm(AOwner: TComponent); virtual;
procedure CreateSpeedButton; virtual; abstract;
procedure CreateEdit; virtual; abstract;
procedure CreateLabel; virtual; abstract;
property Form: TForm read GetForm;
end;
type
TRedFormBuilder = class(TAbstractFormBuilder)
private
FNextLeft, FNextTop: Integer;
public
procedure CreateForm(AOwner: TComponent); override;
procedure CreateSpeedButton; override;
procedure CreateEdit; override;
procedure CreateEdit; override;
end;
type
TBlueFormBuilder = class(TAbstractFormBuilder)
private
FNextLeft, FNextTop: Integer;
public
procedure CreateForm(AOwner: TComponent); override;
procedure CreateSpeedButton; override;
procedure CreateEdit; override;
procedure CreateLabel; override;
end;
Among the above interfaces:
? Declare an interface for creating abstract product objects: TAbstractFormBuilder
¨ Class TAbstractFormBuilder has three abstract factory methods CreateForm, CreateSpeedButton, CreateEdit, CreateEdit
? TBlueFormBuilder and TRedFormBuilder are used to implement methods for creating specific product objects
When running, the client program calls a public constructor of a specific class to create some parts of it. The calling method of a specific constructor instance is as follows:
procedure TForm1.Create3ComponentFormUsingBuilder(ABuilder: TAbstractFormBuilder);
var
NewForm: TForm;
begin
with ABuilder do begin
CreateForm(application);
CreateEdit;
CreateSpeedButton;
CreateLabel;
NewForm := Form;
if NewForm <> nil then NewForm.Show;
end;
end;