forked from alepmedeiros/DelphiPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Based on https://delphipatterns.codeplex.com/
- Loading branch information
1 parent
b2ac4c2
commit 6fe750c
Showing
121 changed files
with
9,832 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
Behavioral.ChainOfResponsibility/Behavioral.ChainOfResponsibility.Pattern.dpr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
program Behavioral.ChainOfResponsibility.Pattern; | ||
|
||
{$APPTYPE CONSOLE} | ||
|
||
uses | ||
SysUtils, | ||
Pattern in 'Pattern.pas'; | ||
|
||
var | ||
Director: IApprover; | ||
VicePresident: IApprover; | ||
President: IApprover; | ||
Purchase: TPurchase; | ||
|
||
begin | ||
ReportMemoryLeaksOnShutDown := DebugHook <> 0; | ||
try | ||
Director := TDirector.Create; | ||
VicePresident := TVicePresident.Create; | ||
President := TPresident.Create; | ||
|
||
try | ||
Director.SetSuccessor(VicePresident); | ||
VicePresident.SetSuccessor(President); | ||
|
||
Purchase := TPurchase.Create(2034, 350.00, 'Supplies'); | ||
Director.ProcessRequest(Purchase); | ||
|
||
Purchase.Free; | ||
Purchase := TPurchase.Create(2035, 32590.10, 'Project X'); | ||
Director.ProcessRequest(Purchase); | ||
|
||
Purchase.Free; | ||
Purchase := TPurchase.Create(2036, 122100.00, 'Project Y'); | ||
Director.ProcessRequest(Purchase); | ||
|
||
ReadLn; | ||
finally | ||
Purchase.Free; | ||
end; | ||
except | ||
on E:Exception do | ||
Writeln(E.Classname, ': ', E.Message); | ||
end; | ||
end. |
236 changes: 236 additions & 0 deletions
236
Behavioral.ChainOfResponsibility/Behavioral.ChainOfResponsibility.Pattern.dproj
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+55.8 KB
Behavioral.ChainOfResponsibility/Behavioral.ChainOfResponsibility.Pattern.res
Binary file not shown.
10 changes: 10 additions & 0 deletions
10
Behavioral.ChainOfResponsibility/Behavioral.ChainOfResponsibility.Pattern.stat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[Stats] | ||
EditorSecs=1 | ||
DesignerSecs=1 | ||
InspectorSecs=1 | ||
CompileSecs=280 | ||
OtherSecs=6 | ||
StartTime=12/31/15 2:42:37 PM | ||
RealKeys=0 | ||
EffectiveKeys=0 | ||
DebugSecs=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
unit Pattern; | ||
|
||
interface | ||
|
||
uses SysUtils; | ||
|
||
type | ||
|
||
TPurchase = class | ||
public | ||
Number: integer; | ||
Amount: Double; | ||
Purpose: string; | ||
constructor Create(num: integer; am: Double; pur: string); | ||
end; | ||
|
||
IApprover = interface | ||
['{3ACA3967-FFCF-48A1-AC45-9A9B98A8DD96}'] | ||
procedure SetSuccessor(successor: IApprover); | ||
procedure ProcessRequest(purchase: TPurchase); | ||
end; | ||
|
||
TApprover = class(TInterfacedObject, IApprover) | ||
protected | ||
FSuccessor: IApprover; | ||
public | ||
procedure SetSuccessor(successor: IApprover); | ||
procedure ProcessRequest(purchase: TPurchase); virtual; abstract; | ||
end; | ||
|
||
TDirector = class(TApprover) | ||
procedure ProcessRequest(purchase: TPurchase); override; | ||
end; | ||
|
||
TVicePresident = class(TApprover) | ||
procedure ProcessRequest(purchase: TPurchase); override; | ||
end; | ||
|
||
TPresident = class(TApprover) | ||
procedure ProcessRequest(purchase: TPurchase); override; | ||
end; | ||
|
||
implementation | ||
|
||
{ TApprover } | ||
|
||
procedure TApprover.SetSuccessor(successor: IApprover); | ||
begin | ||
FSuccessor := successor; | ||
end; | ||
|
||
{ TDirector } | ||
|
||
procedure TDirector.ProcessRequest(purchase: TPurchase); | ||
begin | ||
if purchase.Amount < 10000.0 then | ||
WriteLn(Format('Director approved request # %d', [purchase.Number])) | ||
else if FSuccessor <> nil then | ||
FSuccessor.ProcessRequest(purchase); | ||
end; | ||
|
||
{ TVicePresident } | ||
|
||
procedure TVicePresident.ProcessRequest(purchase: TPurchase); | ||
begin | ||
if purchase.Amount < 25000.0 then | ||
WriteLn(Format('VicePresident approved request # %d', [purchase.Number])) | ||
else if FSuccessor <> nil then | ||
FSuccessor.ProcessRequest(purchase); | ||
end; | ||
|
||
{ TPresident } | ||
|
||
procedure TPresident.ProcessRequest(purchase: TPurchase); | ||
begin | ||
if purchase.Amount < 100000.0 then | ||
WriteLn(Format('President approved request # %d', [purchase.Number])) | ||
else | ||
WriteLn(Format('Request# %d requires an executive meeting!', [purchase.Number])) | ||
end; | ||
|
||
{ TPurchase } | ||
|
||
constructor TPurchase.Create(num: integer; am: Double; pur: string); | ||
begin | ||
Number := num; | ||
Amount := am; | ||
Purpose := pur; | ||
end; | ||
|
||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
program Behavioral.Command.Pattern; | ||
|
||
{$APPTYPE CONSOLE} | ||
|
||
uses | ||
SysUtils, | ||
Pattern in 'Pattern.pas'; | ||
|
||
var | ||
user: TUser; | ||
|
||
begin | ||
ReportMemoryLeaksOnShutDown := DebugHook <> 0; | ||
try | ||
user := TUser.Create; | ||
try | ||
user.Compute('+', 100); | ||
user.Compute('-', 50); | ||
user.Compute('*', 10); | ||
user.Compute('/', 2); | ||
|
||
user.Undo(4); | ||
|
||
user.Redo(3); | ||
|
||
ReadLn; | ||
finally | ||
user.Free; | ||
end; | ||
except | ||
on E:Exception do | ||
Writeln(E.Classname, ': ', E.Message); | ||
end; | ||
end. |
Oops, something went wrong.