G
gmsft
Unregistered / Unconfirmed
GUEST, unregistred user!
学习 阎宏《Java与模式》改写了其中的一个例子,
请高手指教。
逻辑单元:
unit ugmDasheng;
interface
uses
Dialogs;
type
{
大圣接口,Component角色;
}
IDasheng = interface(IInterface)
procedure Shuazi;
end;
{
大圣本尊,ConcreteComponent角色,实现大圣接口;
}
THusun = class(TInterFacedObject, IDasheng)
public
procedure Shuazi;
end;
{
大圣化身,Decorator角色
}
THuashen = class(TInterFacedObject, IDasheng)
private
FDasheng: IDasheng;
public
procedure Shuazi;
virtual;
end;
{
花儿——大圣化身之一,Concrete Decorator角色
}
THuaer = class(THuashen)
public
constructor Create(Dasheng: IDasheng);
procedure Shuazi;
override;
end;
{
鸟儿——大圣化身之二,Concrete Decorator角色
}
TNiaoer = class(THuashen)
public
constructor Create(Dasheng: IDasheng);
procedure Shuazi;
override;
end;
{
鱼儿——大圣化身之三,Concrete Decorator角色
}
TYuer = class(THuashen)
public
constructor Create(Dasheng: IDasheng);
procedure Shuazi;
override;
end;
implementation
{
************************************ THusun ************************************
}
procedure THusun.Shuazi;
begin
ShowMessage('老孙耍子去也!');
end;
{
*********************************** THuashen ***********************************
}
procedure THuashen.Shuazi;
begin
FDasheng.Shuazi;
ShowMessage('摇身一变……');
end;
{
************************************ THuaer ************************************
}
constructor THuaer.Create(Dasheng: IDasheng);
begin
FDasheng := Dasheng;
end;
procedure THuaer.Shuazi;
begin
inherited;
ShowMessage('花儿开了……');
end;
{
*********************************** TNiaoer ************************************
}
constructor TNiaoer.Create(Dasheng: IDasheng);
begin
FDasheng := Dasheng;
end;
procedure TNiaoer.Shuazi;
begin
inherited;
ShowMessage('鸟儿飞了……');
end;
{
************************************ TYuer *************************************
}
constructor TYuer.Create(Dasheng: IDasheng);
begin
FDasheng := Dasheng;
end;
procedure TYuer.Shuazi;
begin
inherited;
ShowMessage('鱼儿游了……');
end;
end.
界面单元:
unit ufmMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TfmMain = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fmMain: TfmMain;
implementation
uses ugmDasheng;
{$R *.dfm}
//大圣本尊——猢狲——耍子去也
procedure TfmMain.Button2Click(Sender: TObject);
begin
THusun.Create.Shuazi;
end;
//大圣化身——花儿——耍子去也
procedure TfmMain.Button4Click(Sender: TObject);
begin
THuaer.Create(THusun.Create).Shuazi;
end;
//大圣化身——鸟儿——耍子去也
procedure TfmMain.Button5Click(Sender: TObject);
begin
TNiaoer.Create(THusun.Create).Shuazi;
end;
//大圣化身——鱼儿——耍子去也
procedure TfmMain.Button6Click(Sender: TObject);
begin
TYuer.Create(THusun.Create).Shuazi;
end;
//大圣化身——花儿、鱼儿、鸟儿——耍子去也
procedure TfmMain.Button7Click(Sender: TObject);
begin
TYuer.Create(TNiaoer.Create(THuaer.Create(THusun.Create))).Shuazi;
end;
end.
请高手指教。
逻辑单元:
unit ugmDasheng;
interface
uses
Dialogs;
type
{
大圣接口,Component角色;
}
IDasheng = interface(IInterface)
procedure Shuazi;
end;
{
大圣本尊,ConcreteComponent角色,实现大圣接口;
}
THusun = class(TInterFacedObject, IDasheng)
public
procedure Shuazi;
end;
{
大圣化身,Decorator角色
}
THuashen = class(TInterFacedObject, IDasheng)
private
FDasheng: IDasheng;
public
procedure Shuazi;
virtual;
end;
{
花儿——大圣化身之一,Concrete Decorator角色
}
THuaer = class(THuashen)
public
constructor Create(Dasheng: IDasheng);
procedure Shuazi;
override;
end;
{
鸟儿——大圣化身之二,Concrete Decorator角色
}
TNiaoer = class(THuashen)
public
constructor Create(Dasheng: IDasheng);
procedure Shuazi;
override;
end;
{
鱼儿——大圣化身之三,Concrete Decorator角色
}
TYuer = class(THuashen)
public
constructor Create(Dasheng: IDasheng);
procedure Shuazi;
override;
end;
implementation
{
************************************ THusun ************************************
}
procedure THusun.Shuazi;
begin
ShowMessage('老孙耍子去也!');
end;
{
*********************************** THuashen ***********************************
}
procedure THuashen.Shuazi;
begin
FDasheng.Shuazi;
ShowMessage('摇身一变……');
end;
{
************************************ THuaer ************************************
}
constructor THuaer.Create(Dasheng: IDasheng);
begin
FDasheng := Dasheng;
end;
procedure THuaer.Shuazi;
begin
inherited;
ShowMessage('花儿开了……');
end;
{
*********************************** TNiaoer ************************************
}
constructor TNiaoer.Create(Dasheng: IDasheng);
begin
FDasheng := Dasheng;
end;
procedure TNiaoer.Shuazi;
begin
inherited;
ShowMessage('鸟儿飞了……');
end;
{
************************************ TYuer *************************************
}
constructor TYuer.Create(Dasheng: IDasheng);
begin
FDasheng := Dasheng;
end;
procedure TYuer.Shuazi;
begin
inherited;
ShowMessage('鱼儿游了……');
end;
end.
界面单元:
unit ufmMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TfmMain = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fmMain: TfmMain;
implementation
uses ugmDasheng;
{$R *.dfm}
//大圣本尊——猢狲——耍子去也
procedure TfmMain.Button2Click(Sender: TObject);
begin
THusun.Create.Shuazi;
end;
//大圣化身——花儿——耍子去也
procedure TfmMain.Button4Click(Sender: TObject);
begin
THuaer.Create(THusun.Create).Shuazi;
end;
//大圣化身——鸟儿——耍子去也
procedure TfmMain.Button5Click(Sender: TObject);
begin
TNiaoer.Create(THusun.Create).Shuazi;
end;
//大圣化身——鱼儿——耍子去也
procedure TfmMain.Button6Click(Sender: TObject);
begin
TYuer.Create(THusun.Create).Shuazi;
end;
//大圣化身——花儿、鱼儿、鸟儿——耍子去也
procedure TfmMain.Button7Click(Sender: TObject);
begin
TYuer.Create(TNiaoer.Create(THuaer.Create(THusun.Create))).Shuazi;
end;
end.