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.Cost: Double;
begin
Result := 1.99;
end;
constructor TEspresso.Create;
begin
FDescription := 'Espresso';
end;
{ THouseBlend }
function THouseBlend.Cost: Double;
begin
Result := 0.89;
end;
constructor THouseBlend.Create;
begin
FDescription := 'House Blend Coffee';
end;
{ TDarkRoast }
function TDarkRoast.Cost: Double;
begin
Result := 0.99;
end;
constructor TDarkRoast.Create;
begin
FDescription := 'Dark Roast Coffee';
end;
end.
2.装饰者
{《HeadFirst设计模式》之装饰模式 }
{ 装饰者既继承又组合被装饰者。继承 }
{ 在这里的意图主要是类型匹配 —— }
{ 与被装饰者是同一类型。 }
{ 编译工具: Delphi7.0 }
{ E-Mail : xshlife@163.com }
unit uDecorator;
interface
uses
uComponent;
type
TCondimentDecorator = class(TBeverage) //抽象装饰者
end;
TMocha = class(TCondimentDecorator) //具体装饰者:Mocha
private
FBeverage: TBever
相关文档:
设置图像关键颜色,使图像的某种或某个范围的颜色成为透明色,是图片合成、动画显示中经常用的图像处理手段。下面是实现代码:
过程定义:
// 设置色键(透明范围)。colorLow 低色键值; colorHigh 高色键值
// 当像素A、R、G、B值同时大于等于colorLow和小于等于colorHigh时为透明色
procedu ......
//定义MyClass
TMyClass = class
GUID: string;
Name: string;
bSex: Boolean;
Tel : string;
end;
//取值
var
obj: TMyClass;
begin
obj := TMyClass.Create;
with Memo1.Lines do
begin
Add('对象大小:' + IntToStr(obj.InstanceSize));
Add('对象所在地址:'+ ......
1.主题与观察者
{《HeadFirst设计模式》之观察者模式 }
{ 主题与观察者 }
{ 编译工具 :Delphi7.0 }
{ 联系方式 :xshlife@163.com }
unit uWeatherReport;
interface
uses
Classes, SysUtils;
type
TObserver = class; { Forward声明,创建两个相 ......
继承是为了表现类与类之间“是一种”关系,是多态存在的基础,继承是面象对象必不可少的基础,只支持封装而不支持继承的语言只能称为“基于对象”(Object-Based)面非面向对象“Object-Oriented”;
Object Pascal只支持单继承,也就是一个派生类只能有一个基类
但可以实现多个接口 ......
1.策略类
{《HeadFirst设计模式》之策略模式 }
{ 本单元中的类为策略类 }
{ 编译工具: Delphi7.0 }
{ E-Mail : xshlife@163.com }
unit uStrategy;
interface
type
{飞行接口,及其实现类 }
IFlyBehavior = Interface(IInterface)
procedure Fly;
......