-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathCreational.AbstractFactory.Pattern.pas
152 lines (123 loc) · 2.62 KB
/
Creational.AbstractFactory.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
unit Creational.AbstractFactory.Pattern;
interface
type
// the brands definition
IBrand = interface
['{3D8E5363-08E7-4B8F-ABF1-8953E13C7A9A}']
function Price: integer;
function Material: string;
end;
TGucci = class(TInterfacedObject, IBrand)
public
function Price: integer;
function Material: string;
end;
TPoochy = class(TInterfacedObject, IBrand)
public
function Price: integer;
function Material: string;
end;
// the products definition
IBag = interface
['{7EACC8B1-7963-4677-A8D7-9FA62065B880}']
function Material: string;
end;
IShoes = interface
['{1560A332-D2D1-43E1-99BB-3D2882C1501A}']
function Price: integer;
end;
TBag = class(TInterfacedObject, IBag)
private
FMyBrand: IBrand;
public
constructor Create(brand: IBrand);
function Material: string;
end;
TShoes = class(TInterfacedObject, IShoes)
private
FMyBrand: IBrand;
public
constructor Create(brand: IBrand);
function Price: integer;
end;
// factories definition
IFactory = interface
['{A9948727-DA7C-4843-83CF-92B81DD158F9}']
function CreateBag: IBag;
function CreateShoes: IShoes;
end;
TGucciFactory = class(TInterfacedObject, IFactory)
public
function CreateBag: IBag;
function CreateShoes: IShoes;
end;
TPoochyFactory = class(TInterfacedObject, IFactory)
public
function CreateBag: IBag;
function CreateShoes: IShoes;
end;
implementation
{ TGucci }
function TGucci.Material: string;
begin
Result := 'Crocodile skin';
end;
function TGucci.Price: integer;
begin
Result := 1000;
end;
{ TPoochi }
function TPoochy.Material: string;
begin
Result := 'Plastic';
end;
function TPoochy.Price: integer;
var
gucci: TGucci;
begin
gucci := TGucci.Create;
try
Result := Round(gucci.Price / 3);
finally
gucci.Free;
end;
end;
{ TBag }
constructor TBag.Create(brand: IBrand);
begin
inherited Create;
FMyBrand := brand;
end;
function TBag.Material: string;
begin
Result := FMyBrand.Material;
end;
{ TShoes }
constructor TShoes.Create(brand: IBrand);
begin
inherited Create;
FMyBrand := brand;
end;
function TShoes.Price: integer;
begin
Result := FMyBrand.Price;
end;
{ TGucciFactory }
function TGucciFactory.CreateBag: IBag;
begin
Result := IBag(TBag.Create(TGucci.Create));
end;
function TGucciFactory.CreateShoes: IShoes;
begin
Result := IShoes(TShoes.Create(TGucci.Create));
end;
{ TPoochyFactory }
function TPoochyFactory.CreateBag: IBag;
begin
Result := IBag(TBag.Create(TPoochy.Create));
end;
function TPoochyFactory.CreateShoes: IShoes;
begin
Result := IShoes(TShoes.Create(TPoochy.Create));
end;
end.