G
gmsft
Unregistered / Unconfirmed
GUEST, unregistred user!
设计模式学习实例——Command模式(繁荣的股市)
上次提交的Decorator模式(孙悟空的七十二变) ( 积分:100, 回复:76, 阅读:782 )
得到了富翁们的关注,非常荣幸。
这次又改写的阎宏《Java与模式》中的一个例子,请富翁们批评。
另:
1)我用 MM 做的类图为什么没有字段,方法等项?
2)界面单元的两个按钮单击方法中,只有一句不同:
Command := TBuyCommand.Create(......);
Command := TSellCommand.Create(......);
请问,如何改写可以实现代码复用?
代码附后:
http://bbs.2ccc.com/topic.asp?topicid=173573
{
*************************************************
设计模式学习实例——Command模式
版权所有(C) 2005
*************************************************
}
unit ugmStock;
{
*************************************************
项目:繁荣的股市
单元:业务逻辑
功能:提供相关业务逻辑
作者:
创建:2005-06-07
修改:2005-06-08
版本:Ver0.1.0
历史:时间 版本 说明
2005-06-08 Ver0.1.0 第一个版本
*************************************************
}
interface
uses
classes;
type
{
***********************************************
股票市场类(TStockMarket)
扮演接收者(Receiver)角色
***********************************************
}
TStockMarket = class
private
FStockList: TStrings;
//上市股票
public
constructor Create;
destructor Destroy;
override;
function SearchStock(StockCode: string): boolean;
function BuyStock(StockCode: string;
StockQuantity: integer): boolean;
function SellStock(StockCode: string;
StockQuantity: integer): boolean;
end;
{
***********************************************
命令类(TCommand)
扮演抽象命令(Abstract Command)角色
***********************************************
}
TCommand = class
protected
FStockMarket: TStockMarket;
FStockCode: string;
FStockQuantity: integer;
public
constructor Create(Market: TStockMarket;
Code: string;
Quantity: integer);
function Execute: boolean;
virtual;
abstract;
end;
{
***********************************************
买入命令类(TBuyCommand)
扮演具体命令(Concrete Command)角色
***********************************************
}
TBuyCommand = class(TCommand)
public
function Execute: boolean;
override;
end;
{
***********************************************
卖出命令类(TSellCommand)
扮演具体命令(Concrete Command)角色
***********************************************
}
TSellCommand = class(TCommand)
public
function Execute: boolean;
override;
end;
{
***********************************************
股票经纪人类(TBroker)
扮演请求者(Inkover)角色
***********************************************
}
TBroker = class
private
FCommand: TCommand;
public
constructor Create(Command: TCommand);
function Action: boolean;
end;
implementation
uses
Dialogs, SysUtils;
{
*************************************************TStockMarket
}
constructor TStockMarket.Create;
var
i: integer;
begin
inherited;
FStockList := TStringList.Create;
for i := 0 to 9do
begin
FStockList.Add(IntToStr(i));
end;
end;
destructor TStockMarket.Destroy;
var
i: integer;
begin
for i := 0 to 9do
begin
FStockList.Objects.Free;
end;
FStockList.Free;
inherited;
end;
function TStockMarket.SearchStock(StockCode: string): boolean;
var
i: integer;
begin
result := false;
for i := 0 to 9do
begin
if FStockList.Strings = StockCode then
begin
result := true;
exit;
end;
end;
end;
function TStockMarket.BuyStock(StockCode: string;
StockQuantity: integer): boolean;
begin
result := false;
if SearchStock(StockCode) then
begin
ShowMessage('买入股票:'+StockCode+chr(13)+IntToStr(StockQuantity)+' 股');
result := true;
end
else
ShowMessage('错误的股票代码!');
end;
function TStockMarket.SellStock(StockCode: string;
StockQuantity: integer): boolean;
begin
result := false;
if SearchStock(StockCode) then
begin
ShowMessage('卖出股票:'+StockCode+chr(13)+IntToStr(StockQuantity)+' 股');
result := true;
end
else
ShowMessage('错误的股票代码!');
end;
{
*************************************************TCommand
}
constructor TCommand.Create(Market: TStockMarket;
Code: String;
Quantity: integer);
begin
FStockMarket := Market;
FStockCode := Code;
FStockQuantity := quantity;
end;
{
*************************************************TBuyCommand
}
function TBuyCommand.Execute: boolean;
begin
result := FStockMarket.BuyStock(FStockCode, FStockQuantity);
end;
{
*************************************************TSellCommand
}
function TSellCommand.Execute: boolean;
begin
result := FStockMarket.SellStock(FStockCode, FStockQuantity)
end;
{
*************************************************TBroker
}
constructor TBroker.Create(Command: TCommand);
begin
FCommand := Command;
end;
function TBroker.Action: boolean;
begin
result := FCommand.Execute;
end;
end.
{
*************************************************
设计模式学习实例——Command模式
版权所有(C) 2005
*************************************************
}
unit fmMain;
{
*************************************************
项目:繁荣的股市
单元:用户界面
功能:提供用户界面
作者:
创建:2005-06-07
修改:2005-06-08
版本:Ver0.1.0
历史:时间 版本 说明
2005-06-08 Ver0.1.0 第一个版本
*************************************************
}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ActnList, jpeg;
type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses ugmStock;
{$R *.dfm}
var
StockMarket: TStockMarket;
StockCode: string;
StockQuantity: integer;
Broker: TBroker;
Command: TCommand;
procedure TForm1.FormCreate(Sender: TObject);
begin
StockMarket := TStockMarket.Create;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
StockCode := Edit1.Text;
StockQuantity := StrToInt(Edit2.Text);
except
ShowMessage('交易指令错误!'+chr(13)+chr(13)+ '请检查股票代码和数量。');
end;
Command := TBuyCommand.Create(StockMarket, StockCode, StockQuantity);
Broker := TBroker.Create(Command);
if Broker.Action then
ShowMessage('交易成功!')
else
ShowMessage('交易失败!');
Broker.Free;
Command.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
try
StockCode := Edit1.Text;
StockQuantity := StrToInt(Edit2.Text);
except
ShowMessage('交易指令错误!'+chr(13)+chr(13)+ '请检查股票代码和数量。');
end;
Command := TSellCommand.Create(StockMarket, StockCode, StockQuantity);
Broker := TBroker.Create(Command);
if Broker.Action then
ShowMessage('交易成功!')
else
ShowMessage('交易失败!');
Broker.Free;
Command.Free;
end;
end.
end.
上次提交的Decorator模式(孙悟空的七十二变) ( 积分:100, 回复:76, 阅读:782 )
得到了富翁们的关注,非常荣幸。
这次又改写的阎宏《Java与模式》中的一个例子,请富翁们批评。
另:
1)我用 MM 做的类图为什么没有字段,方法等项?
2)界面单元的两个按钮单击方法中,只有一句不同:
Command := TBuyCommand.Create(......);
Command := TSellCommand.Create(......);
请问,如何改写可以实现代码复用?
代码附后:
http://bbs.2ccc.com/topic.asp?topicid=173573
{
*************************************************
设计模式学习实例——Command模式
版权所有(C) 2005
*************************************************
}
unit ugmStock;
{
*************************************************
项目:繁荣的股市
单元:业务逻辑
功能:提供相关业务逻辑
作者:
创建:2005-06-07
修改:2005-06-08
版本:Ver0.1.0
历史:时间 版本 说明
2005-06-08 Ver0.1.0 第一个版本
*************************************************
}
interface
uses
classes;
type
{
***********************************************
股票市场类(TStockMarket)
扮演接收者(Receiver)角色
***********************************************
}
TStockMarket = class
private
FStockList: TStrings;
//上市股票
public
constructor Create;
destructor Destroy;
override;
function SearchStock(StockCode: string): boolean;
function BuyStock(StockCode: string;
StockQuantity: integer): boolean;
function SellStock(StockCode: string;
StockQuantity: integer): boolean;
end;
{
***********************************************
命令类(TCommand)
扮演抽象命令(Abstract Command)角色
***********************************************
}
TCommand = class
protected
FStockMarket: TStockMarket;
FStockCode: string;
FStockQuantity: integer;
public
constructor Create(Market: TStockMarket;
Code: string;
Quantity: integer);
function Execute: boolean;
virtual;
abstract;
end;
{
***********************************************
买入命令类(TBuyCommand)
扮演具体命令(Concrete Command)角色
***********************************************
}
TBuyCommand = class(TCommand)
public
function Execute: boolean;
override;
end;
{
***********************************************
卖出命令类(TSellCommand)
扮演具体命令(Concrete Command)角色
***********************************************
}
TSellCommand = class(TCommand)
public
function Execute: boolean;
override;
end;
{
***********************************************
股票经纪人类(TBroker)
扮演请求者(Inkover)角色
***********************************************
}
TBroker = class
private
FCommand: TCommand;
public
constructor Create(Command: TCommand);
function Action: boolean;
end;
implementation
uses
Dialogs, SysUtils;
{
*************************************************TStockMarket
}
constructor TStockMarket.Create;
var
i: integer;
begin
inherited;
FStockList := TStringList.Create;
for i := 0 to 9do
begin
FStockList.Add(IntToStr(i));
end;
end;
destructor TStockMarket.Destroy;
var
i: integer;
begin
for i := 0 to 9do
begin
FStockList.Objects.Free;
end;
FStockList.Free;
inherited;
end;
function TStockMarket.SearchStock(StockCode: string): boolean;
var
i: integer;
begin
result := false;
for i := 0 to 9do
begin
if FStockList.Strings = StockCode then
begin
result := true;
exit;
end;
end;
end;
function TStockMarket.BuyStock(StockCode: string;
StockQuantity: integer): boolean;
begin
result := false;
if SearchStock(StockCode) then
begin
ShowMessage('买入股票:'+StockCode+chr(13)+IntToStr(StockQuantity)+' 股');
result := true;
end
else
ShowMessage('错误的股票代码!');
end;
function TStockMarket.SellStock(StockCode: string;
StockQuantity: integer): boolean;
begin
result := false;
if SearchStock(StockCode) then
begin
ShowMessage('卖出股票:'+StockCode+chr(13)+IntToStr(StockQuantity)+' 股');
result := true;
end
else
ShowMessage('错误的股票代码!');
end;
{
*************************************************TCommand
}
constructor TCommand.Create(Market: TStockMarket;
Code: String;
Quantity: integer);
begin
FStockMarket := Market;
FStockCode := Code;
FStockQuantity := quantity;
end;
{
*************************************************TBuyCommand
}
function TBuyCommand.Execute: boolean;
begin
result := FStockMarket.BuyStock(FStockCode, FStockQuantity);
end;
{
*************************************************TSellCommand
}
function TSellCommand.Execute: boolean;
begin
result := FStockMarket.SellStock(FStockCode, FStockQuantity)
end;
{
*************************************************TBroker
}
constructor TBroker.Create(Command: TCommand);
begin
FCommand := Command;
end;
function TBroker.Action: boolean;
begin
result := FCommand.Execute;
end;
end.
{
*************************************************
设计模式学习实例——Command模式
版权所有(C) 2005
*************************************************
}
unit fmMain;
{
*************************************************
项目:繁荣的股市
单元:用户界面
功能:提供用户界面
作者:
创建:2005-06-07
修改:2005-06-08
版本:Ver0.1.0
历史:时间 版本 说明
2005-06-08 Ver0.1.0 第一个版本
*************************************************
}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ActnList, jpeg;
type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses ugmStock;
{$R *.dfm}
var
StockMarket: TStockMarket;
StockCode: string;
StockQuantity: integer;
Broker: TBroker;
Command: TCommand;
procedure TForm1.FormCreate(Sender: TObject);
begin
StockMarket := TStockMarket.Create;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
StockCode := Edit1.Text;
StockQuantity := StrToInt(Edit2.Text);
except
ShowMessage('交易指令错误!'+chr(13)+chr(13)+ '请检查股票代码和数量。');
end;
Command := TBuyCommand.Create(StockMarket, StockCode, StockQuantity);
Broker := TBroker.Create(Command);
if Broker.Action then
ShowMessage('交易成功!')
else
ShowMessage('交易失败!');
Broker.Free;
Command.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
try
StockCode := Edit1.Text;
StockQuantity := StrToInt(Edit2.Text);
except
ShowMessage('交易指令错误!'+chr(13)+chr(13)+ '请检查股票代码和数量。');
end;
Command := TSellCommand.Create(StockMarket, StockCode, StockQuantity);
Broker := TBroker.Create(Command);
if Broker.Action then
ShowMessage('交易成功!')
else
ShowMessage('交易失败!');
Broker.Free;
Command.Free;
end;
end.
end.