-
Notifications
You must be signed in to change notification settings - Fork 77
/
Behavioral.Memento.Pattern.pas
92 lines (74 loc) · 1.77 KB
/
Behavioral.Memento.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
unit Behavioral.Memento.Pattern;
interface
uses SysUtils;
type
TMemento = class
public
FName: string;
FPhone: string;
FBudget: Double;
constructor Create(name: string; phone: string; budget: Double);
end;
TSalesProspect = class
private
FName: string;
FPhone: string;
FBudget: Double;
procedure SetName(value: string);
procedure SetPhone(value: string);
procedure SetBudget(value: Double);
public
property Name: string read FName write SetName;
property Phone: string read FPhone write SetPhone;
property Budget: Double read FBudget write SetBudget;
function SaveMemento: TMemento;
procedure RestoreMemento(memento: TMemento);
end;
TProspectMemory = class
public
FMemento: TMemento;
destructor Destroy; override;
end;
implementation
{ TMemento }
constructor TMemento.Create(name, phone: string; budget: Double);
begin
FName := name;
FPhone := phone;
FBudget := budget;
end;
{ TSalesProspect }
procedure TSalesProspect.RestoreMemento(memento: TMemento);
begin
WriteLn(#10 + ' Restoring State ---' + #10);
Name := memento.FName;
Phone := memento.FPhone;
Budget := memento.FBudget;
end;
function TSalesProspect.SaveMemento: TMemento;
begin
WriteLn(#10 + ' Saving State ---' + #10);
Result := TMemento.Create(FName, FPhone, FBudget);
end;
procedure TSalesProspect.SetBudget(value: Double);
begin
FBudget := value;
WriteLn('Budget = ' + FloatToStr(FBudget));
end;
procedure TSalesProspect.SetName(value: string);
begin
FName := value;
WriteLn('Name = ' + FName);
end;
procedure TSalesProspect.SetPhone(value: string);
begin
FPhone := value;
WriteLn('Phone = ' + FPhone);
end;
{ TProspectMemory }
destructor TProspectMemory.Destroy;
begin
FMemento.Free;
inherited;
end;
end.