易截截图软件、单文件、免安装、纯绿色、仅160KB
热门标签: c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 最新文章 : delphi

delphi 多态

 什么是多态,字面意思就是“多种形态”,用对象来讲就是子类继承基类,而不同的子类又分别对基类进行功能的扩展。
多态在Object Pascal中是通过虚方法实现的(Virtual Method),在Object Pascal中基类的虚方法是可以被派生类覆盖(Override)的 ......

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码

     适配器模式的主要意图是对现有类的接口进行转换,以满足目标类的需求。其次,可以给目标类的接口添加新的行为(主要指方法)。这一点容易与装饰模式混淆。从意图方面来看,装饰模式不改变(通常指增加)接口中的行为(主要指方法),而是在原有行为(主要指方法)的基础上添加新的功能;从类结构方面来看,装饰模式中的装饰者既继承又组合被装饰者。类适配器同时继承现有类和目标类,对象适配器继承目标类组合现有类。
1.现有类,被转换接口者
{《HeadFirst设计模式》之适配器模式 }
{ 现有类 }
{ 编译工具:Delphi7.0 }
{ E-Mail :xshlife@163.com }

unit uAdaptee;

interface

type
TTurkey = class(TObject)
public
procedure Gobble; virtual; abstract;
procedure Fly; virtual; abstract;
end;

TWildTurkey = class(TTurkey)
public
procedure Gobble; override;
procedure Fly; override;
end;

implementation

{ TWildTurkey }

procedure TWildTurkey.Fly;
begin
Writeln('I''m flying a ......

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
......

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码

1.被装饰者
{《HeadFirst设计模式》之装饰模式 }
{ 本单元中的类为被装饰者 }
{ 编译工具: Delphi7.0 }
{ E-Mail : xshlife@163.com }

unit uComponent;

interface

type
TBeverage = class(TObject) //抽象饮料类
protected
FDescription: String;
public
function GetDescription: String; virtual;
function Cost: Double; virtual; abstract;
end;

TEspresso = class(TBeverage) //浓咖啡饮料类
public
constructor Create;
function Cost: Double; override;
end;

THouseBlend = class(TBeverage) //具体HouseBlend饮料类
public
constructor Create;
function Cost: Double; override;
end;

TDarkRoast = class(TBeverage) //具体DarkRoast饮料类
public
constructor Create;
function Cost: Double; override;
end;

implementation

{ TBeverage }

function TBeverage.GetDescription: String;
begin
Result := FDescription;
end;

{ TEspresso }

function TEspresso ......

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码

1. 复杂的子系统
unit uSubObject;

interface

type

{ TAmplifier与TTuner,TCDPlayer,TDVDPlayer相互依赖。 }
{ 在TTuner等的简单实现时用不到对TAmplifier的引用, }
{ 但现实生活中就应该让TAmplifier提供服务,所以这里保留了。 }
{ TProjector对TDVDPlayer的引用也是同样道理。 }
{ 前置TAmplifier也可以,把TAmplifier的声明放在TTuner等声明的后面 }

TTuner = class;
TCDPlayer = class;
TDVDPlayer = class;

TAmplifier = class(TObject)
private
FTuner: TTuner;
FCD : TCDPlayer;
FDVD: TDVDPlayer;
FDescription: String;
public
constructor Create(aDescription: String);
procedure Open;
procedure Off;
procedure SetStereoSound;
procedure SetSurroundSound;
procedure SetVolume(aLevel: Integer);
procedure SetTuner (aTuner: TTuner);
procedure SetCD (aCD : TCDPlayer);
procedure SetDVD(aDVD: TDVDPlayer);
function ToStr ......

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码

1. 命令的接收者
{《HeadFirst设计模式》之命令模式 }
{ 本单元中的类为命令的接收者 }
{ 编译工具 :Delphi7.0 }
{ 联系方式 :xshlife@163.com }

unit uReceiveObject;

interface

type
TLight = class(TObject)
private
FLocation: String;
public
constructor Create(aLocation: String);
procedure Open;
procedure Off;
end;

TCeilingFan = class(TObject)
private
FLevel : Integer;
FLocation: String;
function GetSpeed: Integer;
public
constructor Create(aLocation: String);
procedure High;
procedure Medium;
procedure Low;
procedure Off;
property Speed: Integer read GetSpeed;
end;

TGarageDoor = class(TObject)
private
FLocation: String;
public
constructor Create(aLocation: String);
procedure Up;
procedure Down;
procedure Stop;
procedure LightOn;
procedure LightOff;
end;

TStereo = class(TObject) ......
总记录数:644; 总页数:108; 每页6 条; 首页 上一页 [84] [85] [86] [87] 88 [89] [90] [91] [92] [93]  下一页 尾页
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号