-
Notifications
You must be signed in to change notification settings - Fork 79
/
Behavioral.Strategy.Pattern.pas
71 lines (54 loc) · 1.14 KB
/
Behavioral.Strategy.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
unit Behavioral.Strategy.Pattern;
interface
type
TContext = class;
IStrategy = interface
['{7F63C143-98D0-4B8C-A02B-894D145BB745}']
function Move(c: TContext): integer;
end;
TStrategy1 = class(TInterfacedObject, IStrategy)
public
function Move(c: TContext): integer;
end;
TStrategy2 = class(TInterfacedObject, IStrategy)
public
function Move(c: TContext): integer;
end;
TContext = class
private
FStrategy: IStrategy;
public
FCounter: integer;
constructor Create;
function Algorithm: integer;
procedure SetStrategy(s: IStrategy);
end;
implementation
{ TStrategy1 }
function TStrategy1.Move(c: TContext): integer;
begin
c.FCounter := c.FCounter + 1;
Result := c.FCounter;
end;
{ TStrategy2 }
function TStrategy2.Move(c: TContext): integer;
begin
c.FCounter := c.FCounter - 1;
Result := c.FCounter;
end;
{ TContext }
function TContext.Algorithm: integer;
begin
Result := FStrategy.Move(Self)
end;
constructor TContext.Create;
begin
inherited;
FCounter := 5;
FStrategy := TStrategy1.Create;
end;
procedure TContext.SetStrategy(s: IStrategy);
begin
FStrategy := s;
end;
end.