《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:组合接口
来源:互联网 发布:知乎azis hop共产主义 编辑:程序博客网 时间:2024/05/01 03:05
示例:组合接口
说明:
(1)、定义
将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合对象的使用具有一致性。
(2)、结构
组合:
Component:为组合中的对象声明接口。在适当的情况下,实现所有类共有接口的缺省行为。声明一个接口用于访问和管理Component的子组件。在递归结构中定义一个接口,用于访问一个父部件,并在合适的情况下实现它(可选)。
Leaf:在组合中表示叶节点对象,叶节点没有子节点。在组合中定义图元对象的行为。
Composite:定义有子部件的那些部件的行为。存储子部件。在Component接口中实现与子部件有关的操作。
客户端:
Client:通过Component接口操纵组合部件的对象。
协作:
用户使用Component类接口与组合结构中的对象进行交互。如果接收者是一个叶节点,则直接处理请求。如果接收者是Composite,它通常将请求发送给它的子部件,在转发请求之前与/或之后可能执行一些辅助操作。
代码: unit uComposite; interface uses Dialogs; type TComponent = class public procedure Operation; virtual; abstract; procedure Add(AComponent: TComponent); virtual; procedure Remove(AComponent: TComponent); virtual; function GetChild(const AIndex: integer): TComponent; virtual; end; TLeaf = class(TComponent) public procedure Operation; override; end; TComposite = class(TComponent) private FChilds: array of TComponent; public constructor Create; destructor Destroy; override; //--- procedure Operation; override; procedure Add(AComponent: TComponent); override; procedure Remove(AComponent: TComponent); override; function GetChild(const AIndex: integer): TComponent; override; end; implementation procedure TComponent.Add(AComponent: TComponent); begin end; procedure TComponent.Remove(AComponent: TComponent); begin end; function TComponent.GetChild(const AIndex: integer): TComponent; begin Result := nil; end; procedure TLeaf.Operation; begin ShowMessage('Leaf'); end; constructor TComposite.Create; begin SetLength(FChilds,0); end; destructor TComposite.Destroy; //--- procedure _Clear; var i: integer; begin for i := low(FChilds) to high(FChilds) do FChilds[i].Free; SetLength(FChilds,0); end; begin _Clear; //--- inherited; end; procedure TComposite.Add(AComponent: TComponent); var ACount: integer; begin ACount := Length(FChilds); SetLength(FChilds,ACount + 1); FChilds[ACount] := AComponent; end; procedure TComposite.Remove(AComponent: TComponent); //--- function _IndexOf: integer; var i: integer; begin for i := low(FChilds) to high(FChilds) do begin if FChilds[i] = AComponent then begin Result := i; exit; end; end; //--- Result := -1; end; //--- procedure _Delete(AIndex: integer); var ACount: integer; begin ACount := Length(FChilds); if AIndex < ACount - 1 then Move(FChilds[AIndex + 1],FChilds[AIndex],(ACount - AIndex - 1) * 4); SetLength(FChilds,ACount - 1); end; var AIndex: integer; begin AIndex := _IndexOf; if AIndex >= 0 then _Delete(AIndex); end; function TComposite.GetChild(const AIndex: integer): TComponent; begin Result := FChilds[AIndex]; end; procedure TComposite.Operation; var i: integer; begin ShowMessage('Composite'); //--- for i := low(FChilds) to high(FChilds) do FChilds[i].Operation; end; end. procedure TForm1.Button1Click(Sender: TObject); var AComposite:TComponent; begin AComposite := TComposite.Create; try AComposite.Add(TLeaf.Create); AComposite.Add(TLeaf.Create); AComposite.Add(TComposite.Create); //--- AComposite.Operation; finally AComposite.Free; end; end;
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:组合接口
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:组合接口
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:最大化Component接口
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:绘图编辑器
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:设备
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:绘图编辑器
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:声明管理子部件的操作(安全性设计)
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:声明管理子部件的操作(透明性设计)
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:显式的父部件引用
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:使用高速缓冲存贮改善性能
- GOF 23设计模式之 组合模式(composite)
- GOF-设计模式之COMPOSITE(组合)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:适配器接口
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:桥接接口
- 《GOF设计模式》—装饰(DECORATOR)—Delphi源码示例:装饰接口
- 《GOF设计模式》—享元(FLYWEIGHT)—Delphi源码示例:享元接口
- 《GOF设计模式》—代理(PROXY)—Delphi源码示例:代理接口
- 《GOF设计模式》—命令(COMMAND)—Delphi源码示例:命令接口
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:创建正确的Implementor对象(缺省的实现)
- Let Say Hello
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:共享Implementor对象
- InstallShield 2010 SP1发布(posted @ 2009-10-14)
- 清除SQLServer数据库日志
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:组合接口
- 推荐一个修改MSI安装包和Merge Module的工具 - Orca
- 如何对行 表 数据库加锁
- Transact-SQL 存储过程 knowledge Note I
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:绘图编辑器
- mp8313 mtd 分区配置
- 用C#写的winform调用webservice天气预报小软件教程(转自百度文库)
- InstallShield厂商更名为Flexera Software
- 不要轻言放弃,否则对不起自己!男人就该对自己狠一点!!!