关闭

delphi 实现简单工厂模式

标签: delphiconstructorfunctionclassbutton设计模式
473人阅读 评论(0) 收藏 举报
分类:
计算器的功能实现
实现工厂设计模式
通过面向对象的思想,进行设计,所用的编程思想为对象的三大特性:封装,继承,多态
通过实现一个简单的 计算器的功能(输入两个数,进行加,减,乘,除)

思想思路:
1.设计虚拟父类;
2.子类继承父类
3. 通过虚拟方法,进行进行重载
4.工厂类,实例化对象,采用多态


}
unit uOperation;

interface

uses Classes, inifiles;

type
{基础类}
TOperation = class(Tobject)
private
FNumberA: Double;
FNumberB: Double;
public
function GetResult: double; virtual; abstract;
property NumberA: Double read FNumberA write FNumberA;
property NumberB: Double read FNumberB write FNumberB;
constructor create;
destructor Destroy; override;
end;

{加法}
TAdd = class(TOperation)
public
function GetResult: double; override;
end;

{减法}
TSub = class(Toperation)
public
function GetResult: double; override;
end;

{乘法}
TMultiple = class(TOperation)
public
function GetResult: double; override;
end;

{除法}
TDev = class(TOperation)
public
function GetResult: Double; override;
end;

{工厂类}

TOperationFactory = class
private
FOpMap: TStringHash;
public

{可以 GetOperation 定义为类方法,这样就不用创建TOperationFactory 对象,可以直接使用

TOperationFactory.GetOperation 但是在本例子中,由于要创建 FOpMap,所有不能定义 类方法,根据情况灵活应用,还有

简单工厂模式,返回值是父类型}
function GetOperation(Op: string): TOperation;
constructor create;
destructor Destroy; override;

end;

implementation

{ TOperation }

constructor TOperation.create;
begin

end;

destructor TOperation.Destroy;
begin

inherited;
end; 

{ TAdd }

function TAdd.GetResult: double;
begin
result := FNumberA + FNumberB;
end;

{ TSub }

function TSub.GetResult: double;
begin
result := FNumberA - FNumberB;
end;

{ TMultiple }

function TMultiple.GetResult: double;
begin
Result := FNumberA * FNumberB;
end;

{ TDev }

function TDev.GetResult: Double;
begin
Result := 0;
if FNumberB <> 0 then
Result := FNumberA / FNumberB;

end;

{ TOperationFactory }

constructor TOperationFactory.create;
begin
FOpMap := TStringHash.Create;
FOpMap.Add('+', 1);
FOpMap.Add('-', 2);
FOpMap.Add('*', 3);
FOpMap.Add('/', 4);
end;

destructor TOperationFactory.Destroy;
begin
FOpMap.Free;
inherited;
end;

function TOperationFactory.GetOperation(Op: string): TOperation;
begin
result := nil;
case FOpMap.ValueOf(Op) of
1: result := TAdd.create;
2: result := TSub.create;
3: result := TMultiple.create;
4: result := TDev.create;
end;
end;
end.

{使用}

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Edit4: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses uOperation;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
op: TOperation;
OpFact: TOperationFactory;
begin
OpFact := TOperationFactory.create;

op := OpFact.GetOperation(Edit4.Text);
try
if op = nil then exit;
op.NumberA := strtointdef(Edit1.Text, -1);
op.NumberB := strtointdef(Edit2.Text, -1);
edit3.Text := FloatToStr(op.GetResult);
finally
OPFact.Free;
OP.Free;
end;
end;

end.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:259195次
    • 积分:4084
    • 等级:
    • 排名:第7548名
    • 原创:148篇
    • 转载:51篇
    • 译文:0篇
    • 评论:13条
    最新评论