Delphi 设计模式:《HeadFirst设计模式》Delphi7代码
容器的主要职责有两个:存放元素和浏览元素。根据单一职责原则(SRP)要将二者分开,于是将浏览功能打包封装就有了迭代器。
用迭代器封装对动态数组的遍历:
1.容器中的元素类
{《HeadFirst设计模式》之迭代器模式 }
{ 容器中的元素类 }
{ 编译工具:Delphi7.0 }
{ E-Mail :xshlife@163.com }
unit uItem;
interface
type
TMenuItem = class(TObject)
private
FName: String;
FDescription: String;
FVegetarian : Boolean;
FPrice: Double;
public
constructor Create(aName, aDescription: String;
aVegetarian : Boolean;
aPrice: Double);
function GetName: String;
function GetDescription: String;
function GetPrice: Double;
function IsVegetarian: Boolean;
end;
implementation
{ TMenuItem }
constructor TMenuItem.Create(aName, aDescription: String;
aVegetarian: Boolean;
aPrice: Double);
begin
FName := aName;
FDescription := aDescription;
FVegetarian := aVegetarian;
FPrice := aPrice;
end;
function TMenuItem.GetDescription: String;
begin
Result := FDescription;
end;
function TMenuItem.GetName: String;
begin
Result := FName;
end;
function TMenuItem.GetPrice: Double;
begin
Result := FPrice;
end;
function TMenuItem.IsVegetarian: Boolean;
begin
Result := FVegetarian;
end;
end.
2.迭代器
{《HeadFirst设计模式》之迭代器模式 }
{ 迭代器:封装对容器的遍历 }
{ 编译工具:Delphi7.0 }
{ E-Mail :xshlife@163.com }
unit uIterator;
interface
uses
uItem;
type
TMenuItems = array of TMenuItem;
TIterator = class(TObject)
public
function HasNext: Boolean; virtual; abstract;
function Next : TObject; virtual; abstract;
end;
TDinerMenuIter
相关文档:
第一章 DELPHI的原子世界
第二章 DELPHI与WIN32时空
第三章 多线程
第四章 接口
第五章 包
第六章 事件与消息
第七章 组织你的模块
第八章 探索数据库
第九章 多层体系结构
第十章 操作界面与操作逻辑
第十一章 面向对象数据库基础
空 ......
第一章 DELPHI的原子世界
第二章 DELPHI与WIN32时空
第三章 多线程
第四章 接口
第五章 包
第六章 事件与消息
第七章 组织你的模块
第八章 探索数据库
第九章 多层体系结构
第十章 操作界面与操作逻辑
第十一章 面向对象数据库基础
空 ......
在一个单元中声明的多个类互为友元类
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 ......
1.主题与观察者
{《HeadFirst设计模式》之观察者模式 }
{ 主题与观察者 }
{ 编译工具 :Delphi7.0 }
{ 联系方式 :xshlife@163.com }
unit uWeatherReport;
interface
uses
Classes, SysUtils;
type
TObserver = class; { Forward声明,创建两个相 ......
模板方法模式定义了一个算法骨架,允许子类对算法的某个或某些步骤进行重写(override)。
1.CoffeineBeverageWithHook
{《HeadFirst设计模式》之模板方法模式 }
{ 编译工具: Delphi7.0 }
{ E-Mail : xshlife@163.com }
unit uCoffeineBeverageWithHook;
interface
uses
SysUti ......