《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:使用双向适配器提供透明操作
2014-08-03 21:55
741 查看
示例:使用双向适配器提供透明操作
说明:
使用适配器的一个潜在问题是,它们不对所有的客户都透明。被适配的对象不再兼容Adaptee的接口,因此并不是所有Adaptee对象可以被使用的方它都可以被使用。双向适配器提供了这样的透明性。在两个不同的客户需要用不同的方式查看同一个对象时,双向适配器尤其有用。
实现:
考虑一个双向适配器,它将图形编辑框架Unidraw与约束求解工具箱QOCA集成起来。这两个系统都有一些类,这些类显式地表示变量:Unidraw含有类StateVariable,QOCA中含有类ConstraintVariable,如下图所示。
为了使Unidraw与QOCA协同工作,必须首先使类ConstraintVariable与类StateVariable相匹配;而为了将QOCA的求解结果传递给Unidraw,必须使StateVariable与ConstraintVariable相匹配。
这一方案中包含了一个双向适配器ConstraintStateVariable,它是类ConstraintVariable与类StateVariable共同的子类,ConstraintStateVariable使得两个接口互相匹配。在该例中多重继承是一个可行的解决方案,因为被适配类的接口差异较大。双向适配器与这两个被匹配的类都兼容,在这两个系统中它都可以工作。
代码:
unit uAdapter1;
interface
uses
Dialogs;
type
IStateVariable = interface
['{BACF300B-1E14-42A0-8EBC-DD9AA1878E44}']
procedure Show;
end;
IConstraintVariable = interface
['{54997F39-8BF1-47E0-9461-7AEFA8984B1A}']
procedure Display;
end;
TStateVariable = class(TInterfacedObject,IStateVariable)
procedure Show;
end;
TConstraintVariable = class(TInterfacedObject,IConstraintVariable)
procedure Display;
end;
TConstraintStateVariable = class(TInterfacedObject,IStateVariable,IConstraintVariable)
procedure Show;
procedure Display;
end;
implementation
procedure TStateVariable.Show;
begin
ShowMessage('StateVariable');
end;
procedure TConstraintVariable.Display;
begin
ShowMessage('ConstraintVariable');
end;
procedure TConstraintStateVariable.Display;
begin
ShowMessage('ConstraintVariable');
end;
procedure TConstraintStateVariable.Show;
begin
ShowMessage('StateVariable');
end;
end.
procedure TForm1.Button1Click(Sender: TObject);
var
AVariable: TConstraintStateVariable;
AStateVariable:IStateVariable;
AConstraintVariable: IConstraintVariable;
begin
AVariable := TConstraintStateVariable.Create;
//---
AStateVariable := AVariable;
AStateVariable.Show;
//---
AConstraintVariable := AVariable;
AConstraintVariable.Display;
end;
示例:使用双向适配器提供透明操作
说明:
使用适配器的一个潜在问题是,它们不对所有的客户都透明。被适配的对象不再兼容Adaptee的接口,因此并不是所有Adaptee对象可以被使用的方它都可以被使用。双向适配器提供了这样的透明性。在两个不同的客户需要用不同的方式查看同一个对象时,双向适配器尤其有用。
实现:
考虑一个双向适配器,它将图形编辑框架Unidraw与约束求解工具箱QOCA集成起来。这两个系统都有一些类,这些类显式地表示变量:Unidraw含有类StateVariable,QOCA中含有类ConstraintVariable,如下图所示。
为了使Unidraw与QOCA协同工作,必须首先使类ConstraintVariable与类StateVariable相匹配;而为了将QOCA的求解结果传递给Unidraw,必须使StateVariable与ConstraintVariable相匹配。
这一方案中包含了一个双向适配器ConstraintStateVariable,它是类ConstraintVariable与类StateVariable共同的子类,ConstraintStateVariable使得两个接口互相匹配。在该例中多重继承是一个可行的解决方案,因为被适配类的接口差异较大。双向适配器与这两个被匹配的类都兼容,在这两个系统中它都可以工作。
代码:
unit uAdapter1;
interface
uses
Dialogs;
type
IStateVariable = interface
['{BACF300B-1E14-42A0-8EBC-DD9AA1878E44}']
procedure Show;
end;
IConstraintVariable = interface
['{54997F39-8BF1-47E0-9461-7AEFA8984B1A}']
procedure Display;
end;
TStateVariable = class(TInterfacedObject,IStateVariable)
procedure Show;
end;
TConstraintVariable = class(TInterfacedObject,IConstraintVariable)
procedure Display;
end;
TConstraintStateVariable = class(TInterfacedObject,IStateVariable,IConstraintVariable)
procedure Show;
procedure Display;
end;
implementation
procedure TStateVariable.Show;
begin
ShowMessage('StateVariable');
end;
procedure TConstraintVariable.Display;
begin
ShowMessage('ConstraintVariable');
end;
procedure TConstraintStateVariable.Display;
begin
ShowMessage('ConstraintVariable');
end;
procedure TConstraintStateVariable.Show;
begin
ShowMessage('StateVariable');
end;
end.
procedure TForm1.Button1Click(Sender: TObject);
var
AVariable: TConstraintStateVariable;
AStateVariable:IStateVariable;
AConstraintVariable: IConstraintVariable;
begin
AVariable := TConstraintStateVariable.Create;
//---
AStateVariable := AVariable;
AStateVariable.Show;
//---
AConstraintVariable := AVariable;
AConstraintVariable.Display;
end;
相关文章推荐
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:使用双向适配器提供透明操作
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(使用抽象操作)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(使用代理对象)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(使用代理对象)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(使用抽象操作)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:绘图编辑器
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:适配器接口
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(参数化的适配器)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(参数化的适配器)
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:适配器接口
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:绘图编辑器
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:为子类提供挂钩(hook)
- 《GOF设计模式》—解释器 (INTERPRETER)—Delphi源码示例:使用Flyweight模式共享终结符
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:声明管理子部件的操作(透明性设计)
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于类的原型管理器(使用TList和TObject对象服务实现)
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于类的原型管理器(使用TStringList实现)
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于流操作的拷贝
- 《GOF设计模式》—迭代器 (ITERATOR)—Delphi源码示例:内部迭代器(使用函数指针)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:单件接口(使用类字段)
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:使用高速缓冲存贮改善性能