F
fdwangchao
Unregistered / Unconfirmed
GUEST, unregistred user!
unit State;
interface
uses
Graphics, stdctrls;
type
TContext = class;
TState = class (TObject)
public
function GetColor: TColor;
virtual;
abstract;
procedure HandlePull(C: TContext);
virtual;
abstract;
procedure HandlePush(C: TContext);
virtual;
abstract;
end;
TRedState = class (TState)
public
function GetColor: TColor;
override;
procedure HandlePull(C: TContext);
override;
procedure HandlePush(C: TContext);
override;
end;
TGreenState = class (TState)
public
function GetColor: TColor;
override;
procedure HandlePull(C: TContext);
override;
procedure HandlePush(C: TContext);
override;
end;
TBlueState = class (TState)
public
function GetColor: TColor;
override;
procedure HandlePull(C: TContext);
override;
procedure HandlePush(C: TContext);
override;
end;
TContext = class (TObject)
private
FLabel: TLabel;
FState: TState;
public
constructor Create(ShowLabel: TLabel);
virtual;
procedure Pull;
procedure Push;
procedure SetState(State: TState);
end;
implementation
{
********************************** TRedState ***********************************
}
function TRedState.GetColor: TColor;
begin
Result := clRed;
end;
procedure TRedState.HandlePull(C: TContext);
begin
C.SetState(TGreenState.Create);
end;
procedure TRedState.HandlePush(C: TContext);
begin
C.SetState(TBlueState.Create);
end;
{
********************************* TGreenState **********************************
}
function TGreenState.GetColor: TColor;
begin
Result := clGreen;
end;
procedure TGreenState.HandlePull(C: TContext);
begin
C.SetState(TBlueState.Create);
end;
procedure TGreenState.HandlePush(C: TContext);
begin
C.SetState(TRedState.Create);
end;
{
********************************** TBlueState **********************************
}
function TBlueState.GetColor: TColor;
begin
Result := clBlue;
end;
procedure TBlueState.HandlePull(C: TContext);
begin
C.SetState(TRedState.Create);
end;
procedure TBlueState.HandlePush(C: TContext);
begin
C.SetState(TGreenState.Create);
end;
{
*********************************** TContext ***********************************
}
constructor TContext.Create(ShowLabel: TLabel);
begin
FLabel := ShowLabel;
end;
procedure TContext.Pull;
begin
FState.HandlePull(Self);
FLabel.Color := FState.GetColor;
end;
procedure TContext.Push;
begin
FState.HandlePush(Self);
FLabel.Color := FState.GetColor;
end;
procedure TContext.SetState(State: TState);
begin
FState := State;
end;
//调用
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses State;
{$R *.DFM}
var
Temp: TContext;
procedure TForm1.Button1Click(Sender: TObject);
begin
Temp.Pull;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Green: TState;
begin
Green := TRedState.Create;
Temp := TContext.Create(Label1);
Temp.SetState(Green);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Temp.Push;
end;
interface
uses
Graphics, stdctrls;
type
TContext = class;
TState = class (TObject)
public
function GetColor: TColor;
virtual;
abstract;
procedure HandlePull(C: TContext);
virtual;
abstract;
procedure HandlePush(C: TContext);
virtual;
abstract;
end;
TRedState = class (TState)
public
function GetColor: TColor;
override;
procedure HandlePull(C: TContext);
override;
procedure HandlePush(C: TContext);
override;
end;
TGreenState = class (TState)
public
function GetColor: TColor;
override;
procedure HandlePull(C: TContext);
override;
procedure HandlePush(C: TContext);
override;
end;
TBlueState = class (TState)
public
function GetColor: TColor;
override;
procedure HandlePull(C: TContext);
override;
procedure HandlePush(C: TContext);
override;
end;
TContext = class (TObject)
private
FLabel: TLabel;
FState: TState;
public
constructor Create(ShowLabel: TLabel);
virtual;
procedure Pull;
procedure Push;
procedure SetState(State: TState);
end;
implementation
{
********************************** TRedState ***********************************
}
function TRedState.GetColor: TColor;
begin
Result := clRed;
end;
procedure TRedState.HandlePull(C: TContext);
begin
C.SetState(TGreenState.Create);
end;
procedure TRedState.HandlePush(C: TContext);
begin
C.SetState(TBlueState.Create);
end;
{
********************************* TGreenState **********************************
}
function TGreenState.GetColor: TColor;
begin
Result := clGreen;
end;
procedure TGreenState.HandlePull(C: TContext);
begin
C.SetState(TBlueState.Create);
end;
procedure TGreenState.HandlePush(C: TContext);
begin
C.SetState(TRedState.Create);
end;
{
********************************** TBlueState **********************************
}
function TBlueState.GetColor: TColor;
begin
Result := clBlue;
end;
procedure TBlueState.HandlePull(C: TContext);
begin
C.SetState(TRedState.Create);
end;
procedure TBlueState.HandlePush(C: TContext);
begin
C.SetState(TGreenState.Create);
end;
{
*********************************** TContext ***********************************
}
constructor TContext.Create(ShowLabel: TLabel);
begin
FLabel := ShowLabel;
end;
procedure TContext.Pull;
begin
FState.HandlePull(Self);
FLabel.Color := FState.GetColor;
end;
procedure TContext.Push;
begin
FState.HandlePush(Self);
FLabel.Color := FState.GetColor;
end;
procedure TContext.SetState(State: TState);
begin
FState := State;
end;
//调用
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses State;
{$R *.DFM}
var
Temp: TContext;
procedure TForm1.Button1Click(Sender: TObject);
begin
Temp.Pull;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Green: TState;
begin
Green := TRedState.Create;
Temp := TContext.Create(Label1);
Temp.SetState(Green);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Temp.Push;
end;