-
Notifications
You must be signed in to change notification settings - Fork 77
/
Creational.FactoryMethod.Pattern.pas
63 lines (46 loc) · 1.09 KB
/
Creational.FactoryMethod.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
unit Creational.FactoryMethod.Pattern;
interface
type
IProduct = interface
['{2E0DD8B3-6BA2-4922-93F1-81F521B55AA9}']
function ShipFrom: string;
End;
TProductA = class(TInterfacedObject, IProduct)
function ShipFrom: string;
end;
TProductB = class(TInterfacedObject, IProduct)
function ShipFrom: string;
end;
TDefaultProduct = class(TInterfacedObject, IProduct)
function ShipFrom: string;
end;
TCreator = class
function FactoryMethod(month: integer): IProduct;
end;
implementation
{ TProductA }
function TProductA.ShipFrom: string;
begin
Result := 'from South Africa';
end;
{ TProductB }
function TProductB.ShipFrom: string;
begin
Result := 'from Spain';
end;
{ TDefaultProduct }
function TDefaultProduct.ShipFrom: string;
begin
Result := 'not available';
end;
{ TCreator }
function TCreator.FactoryMethod(month: integer): IProduct;
begin
if (month > 4) and (month <= 11) then begin
Result := TProductA.Create;
end else if (month in [1,2,12]) then begin
Result := TProductB.Create;
end else
Result := TDefaultProduct.Create;
end;
end.