Created
April 15, 2014 02:45
-
-
Save drgarcia1986/10697797 to your computer and use it in GitHub Desktop.
A example of Pattern Strategy (GoF). context is IText, strategy is IFormatter and client is Application.
This file contains hidden or 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
unit Strategy.Concrete.Lower; | |
interface | |
uses | |
Strategy.Interfaces; | |
type | |
TFormatterLower = class(TInterfacedObject, IFormatter) | |
public | |
function GetFormattedText(AText: IText): string; | |
end; | |
implementation | |
uses | |
System.SysUtils; | |
{ TFormatterLower } | |
function TFormatterLower.GetFormattedText(AText: IText): string; | |
begin | |
Result := AnsiLowerCase(AText.GetText()); | |
end; | |
end. |
This file contains hidden or 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
unit Strategy.Concrete.Reverse; | |
interface | |
uses | |
Strategy.Interfaces; | |
type | |
TFormatterReverse = class(TInterfacedObject, IFormatter) | |
public | |
function GetFormattedText(AText: IText): string; | |
end; | |
implementation | |
uses | |
System.StrUtils; | |
{ TFormatterReverse } | |
function TFormatterReverse.GetFormattedText(AText: IText): string; | |
begin | |
Result := ReverseString(AText.GetText()); | |
end; | |
end. |
This file contains hidden or 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
program Strategy; | |
{$R *.res} | |
uses | |
System.SysUtils, | |
Strategy.Interfaces in 'Strategy.Interfaces.pas', | |
Strategy.Text in 'Strategy.Text.pas', | |
Strategy.Concrete.Lower in 'Strategy.Concrete.Lower.pas', | |
Strategy.Concrete.Reverse in 'Strategy.Concrete.Reverse.pas'; | |
var | |
oText : IText; | |
oFormatter : IFormatter; | |
s : string; | |
begin | |
ReportMemoryLeaksOnShutdown := True; | |
if ParamStr(1) = 'lower' then | |
oFormatter := TFormatterLower.Create() | |
else | |
oFormatter := TFormatterReverse.Create(); | |
oText := TText.Create(oFormatter); | |
try | |
Write('Enter text: '); | |
Readln(s); | |
oText.SetText(s); | |
oText.Format(); | |
Write(Format('Formatted text: %s',[oText.GetText()])); | |
Readln; | |
except | |
on E: Exception do | |
Writeln(E.ClassName, ': ', E.Message); | |
end; | |
end. |
This file contains hidden or 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
unit Strategy.Interfaces; | |
interface | |
type | |
IText = interface | |
['{79217AFB-BCA0-4095-90E2-124109EE203D}'] | |
function GetText() : String; | |
procedure SetText(const AValue : string); | |
procedure Format(); | |
end; | |
IFormatter = interface | |
['{206E22B4-626F-4163-A796-8D07C6B87FA3}'] | |
function GetFormattedText(AText : IText):String; | |
end; | |
implementation | |
end. |
This file contains hidden or 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
unit Strategy.Text; | |
interface | |
uses Strategy.Interfaces; | |
type | |
TText = class(TInterfacedObject, IText) | |
strict private | |
FFormatter : IFormatter; | |
FStr : string; | |
public | |
procedure Format; | |
function GetText: string; | |
procedure SetText(const AValue: string); | |
constructor Create(AFormatter : IFormatter); | |
end; | |
implementation | |
{ TText } | |
constructor TText.Create(AFormatter: IFormatter); | |
begin | |
Self.FFormatter := AFormatter; | |
end; | |
procedure TText.Format; | |
var | |
s : string; | |
begin | |
s := Self.FFormatter.GetFormattedText(Self); | |
Self.SetText(s); | |
end; | |
function TText.GetText: string; | |
begin | |
Result := Self.FStr; | |
end; | |
procedure TText.SetText(const AValue: string); | |
begin | |
Self.FStr := AValue; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment