-
Notifications
You must be signed in to change notification settings - Fork 77
/
Creational.Builder.Pattern.pas
138 lines (111 loc) · 2.07 KB
/
Creational.Builder.Pattern.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
unit Creational.Builder.Pattern;
interface
uses
Classes;
type
TProduct = class
private
FParts: TStringList;
public
constructor Create;
destructor Destroy; override;
procedure Add(part: string);
procedure Display;
end;
IBuilder = interface
['{9769DCD1-A2F5-4105-B28D-8A34DA6B0C12}']
procedure BuildPartA;
procedure BuildPartB;
function GetResult: TProduct;
end;
TBuilder1 = class(TInterfacedObject, IBuilder)
private
FProduct: TProduct;
public
constructor Create;
procedure BuildPartA;
procedure BuildPartB;
function GetResult: TProduct;
end;
TBuilder2 = class(TInterfacedObject, IBuilder)
private
FProduct: TProduct;
public
constructor Create;
procedure BuildPartA;
procedure BuildPartB;
function GetResult: TProduct;
end;
TDirector = class
public
procedure Construct(builder: IBuilder);
end;
implementation
{ TProduct }
procedure TProduct.Add(part: string);
begin
FParts.Add(part);
end;
constructor TProduct.Create;
begin
inherited;
FParts := TStringList.Create;
end;
destructor TProduct.Destroy;
begin
FParts.Free;
inherited;
end;
procedure TProduct.Display;
var
I: integer;
begin
WriteLn('Product Patrs -----------');
for I := 0 to FParts.Count - 1 do begin
WriteLn(FParts[I]);
end;
end;
{ TBuilder1 }
procedure TBuilder1.BuildPartA;
begin
FProduct.Add('Part A ');
end;
procedure TBuilder1.BuildPartB;
begin
FProduct.Add('Part B ');
end;
constructor TBuilder1.Create;
begin
inherited;
FProduct := TProduct.Create;
end;
function TBuilder1.GetResult: TProduct;
begin
Result := FProduct;
end;
{ TBuilder2 }
procedure TBuilder2.BuildPartA;
begin
FProduct.Add('Part X ');
end;
procedure TBuilder2.BuildPartB;
begin
FProduct.Add('Part Y ');
end;
constructor TBuilder2.Create;
begin
inherited;
FProduct := TProduct.Create;
end;
function TBuilder2.GetResult: TProduct;
begin
Result := FProduct;
end;
{ TDirector }
procedure TDirector.Construct(builder: IBuilder);
begin
builder.BuildPartA;
builder.BuildPartB;
builder.BuildPartB;
end;
end.