Delphi 设计模式:《HeadFirst设计模式》Delphi7代码
1.策略类
{《HeadFirst设计模式》之策略模式 }
{ 本单元中的类为策略类 }
{ 编译工具: Delphi7.0 }
{ E-Mail : xshlife@163.com }
unit uStrategy;
interface
type
{飞行接口,及其实现类 }
IFlyBehavior = Interface(IInterface)
procedure Fly;
end;
TFlyWithWings = class(TInterfacedObject, IFlyBehavior)
public
procedure Fly;
end;
TFlyNoWay = class(TInterfacedObject, IFlyBehavior)
public
procedure Fly;
end;
TFlyRocketPowered = class(TInterfacedObject, IFlyBehavior)
public
procedure Fly;
end;
{叫声接口,及其实现类}
IQuackBehavior = Interface(IInterface)
procedure Quack;
end;
TQuack = class(TInterfacedObject, IQuackBehavior)
public
procedure Quack;
end;
TMuteQuack = class(TInterfacedObject, IQuackBehavior)
public
procedure Quack;
end;
TSqueak = class(TInterfacedObject, IQuackBehavior)
public
procedure Quack;
end;
implementation
{ TFlyWithWings }
procedure TFlyWithWings.Fly;
begin
Writeln('I am flying!');
end;
{ TFlyNoWay }
procedure TFlyNoWay.Fly;
begin
Writeln('I can not fly!');
end;
{ TFlyRocketPowered }
procedure TFlyRocketPowered.Fly;
begin
Writeln('I am flying with a rocket!');
end;
{ TQuack }
procedure TQuack.Quack;
begin
Writeln('Quack');
end;
{ TMuteQuack }
procedure TMuteQuack.Quack;
begin
Writeln('<Silence>');
end;
{ TSqueak }
procedure TSqueak.Quack;
begin
Writeln('Squeak');
end;
end.
2.策略的用户
{《HeadFirst设计模式》之策略模式 }
{ 本单元中的类为策略的用户, }
{ 一般策略模式中的上下文接口已包含在用户类中。}
{ 编译工具: Delphi7.0 }
{ E-Mail : xshlife@163.com }
unit uDuck;
interface
uses
uStrategy;
type
{ 鸭子抽象类 }
TDuck
相关文档:
Delphi2010集成了fastMM,这回大家调试程序是的时候可以方便地检查内存泄露了。
使用方法如下:
在project中,添加一行 ReportMemoryLeaksOnShutdown := DebugHook<>0;
DebugHook<>0 目的是保证单独运行exe文件不会弹出内存泄露框,源码可以不用注释掉此行
program Project1;
uses
Forms,
......
一、一个叫声接口和几只鸭子
1、从一个叫声接口开始
{《HeadFirst设计模式》Delphi代码之模式小结 }
{ 一个叫声接口 }
{ 编译工具:Delphi2010 for win32 }
{ E-Mail :xshlife@163.com }
unit uQuackable;
interface
type
IQuackable = in ......
在一个单元中声明的多个类互为友元类
type
TMyClass = class
GUID: string;
Name: string;
bSex: Boolean;
Tel : string;
end;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedu ......
继承是为了表现类与类之间“是一种”关系,是多态存在的基础,继承是面象对象必不可少的基础,只支持封装而不支持继承的语言只能称为“基于对象”(Object-Based)面非面向对象“Object-Oriented”;
Object Pascal只支持单继承,也就是一个派生类只能有一个基类
但可以实现多个接口 ......