刚学《设计模式》,看了板桥里人的《设计模式之State》,练练手,别笑话噢...(50分)

  • 主题发起人 主题发起人 fdwangchao
  • 开始时间 开始时间
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;
 
好,谢谢
参考!
 
我觉得stat模式不是一个简单的多态调用的实现方式,
他应该是为有限状态机实现的一个专有模式,把动作和状态逻辑分离开来。
否则他和strategy模式就分别不大了
 
后退
顶部