unit FizzBuzz;
interface
type
TCustomFizzBuzzState =
class;
TFizzBuzzStateClass =
class of TCustomFizzBuzzState;
TFizzBuzz =
class
private
FIndex : Integer;
FState : TCustomFizzBuzzState;
protected
procedure DoWriteIndex;
procedure DoWriteString(
const AString :
string );
procedure DoWriteEOL;
procedure DoIncIndex;
procedure DoSetState( AStateClass : TFizzBuzzStateClass );
public
procedure Execute( AIndex, ACount : Integer );
end;
TCustomFizzBuzzState =
class
private
FContext : TFizzBuzz;
protected
procedure DoWriteIndex;
procedure DoWriteString(
const AString :
string );
procedure DoWriteEOL;
procedure DoIncIndex;
procedure DoSetState( AStateClass : TFizzBuzzStateClass );
public
constructor Create( AContext : TFizzBuzz );
procedure Consume( AValue : Integer );
virtual;
abstract;
end;
implementation
uses
FizzBuzzStates;
{ TFizzBuzzStateBase }
constructor TCustomFizzBuzzState.Create( AContext : TFizzBuzz );
begin
inherited Create;
FContext := AContext;
end;
procedure TCustomFizzBuzzState.DoIncIndex;
begin
FContext.DoIncIndex;
end;
procedure TCustomFizzBuzzState.DoSetState( AStateClass : TFizzBuzzStateClass );
begin
FContext.DoSetState( AStateClass );
end;
procedure TCustomFizzBuzzState.DoWriteEOL;
begin
FContext.DoWriteEOL;
end;
procedure TCustomFizzBuzzState.DoWriteIndex;
begin
FContext.DoWriteIndex;
end;
procedure TCustomFizzBuzzState.DoWriteString(
const AString :
string );
begin
FContext.DoWriteString( AString );
end;
{ TFizzBuzz }
procedure TFizzBuzz.Execute( AIndex, ACount : Integer );
begin
DoSetState( TFizzState );
FIndex := AIndex;
while FIndex < AIndex + ACount
do
begin
FState.Consume( FIndex );
end;
end;
procedure TFizzBuzz.DoIncIndex;
begin
Inc( FIndex );
end;
procedure TFizzBuzz.DoSetState( AStateClass : TFizzBuzzStateClass );
begin
if Assigned( FState )
then
FState.Free;
FState := AStateClass.Create( Self );
end;
procedure TFizzBuzz.DoWriteEOL;
begin
Writeln;
end;
procedure TFizzBuzz.DoWriteIndex;
begin
write( FIndex );
end;
procedure TFizzBuzz.DoWriteString(
const AString :
string );
begin
write( AString );
end;
end.