Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14490577
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 20:49:48

下载本文示例代码
  Wrapper设计模式能够把一个类的接口修改成另一个类所需要的接口,然后让本来因为接口不兼容的类能够在一起配合工作。   在Delphi中,为了让两个类能够支持同样的接口,它们必须有同样的祖先类这样才能在其他类调用的时候实现多态性。但是有的时候我们希望两个没有关系的类能够在一起工作,Wrapper设计模式就能够让一个类wrap部分另一个类的部分接口(当然也可以是全部,看具体的需要而定),从而能够模拟出一个类似于“uses”的类的多继承关系。   让我们来看一个例子,有一个类TSimple从TObject继承下来,但是我们希望能把TSimple这个类放到Delphi的控件面板(component palette)上,然而我们知道,如果一个类能够放置到控件面板上,这个类必须从TComponent派生下来。现在我们不想在改变TSimple的祖先类TObject的情况下(比如我们并没有TSimple的源代码)把TSimple放到控件面板上,我们可以构建一个继承自TComponent的TSimpleWapper类,在TSimpleWapper类中Wrap TSimple的接口。下面是这个例子的范例代码: type TSample = class (TObject)private FSomeValue: Integer;public function SomeAction(const Data: string): Boolean;  property SomeValue: Integer read FSomeValue write FSomeValue; end; TSampleWrapper = class(TComponent)private FSample: TSample;public property Sample: TSample read FSample;end;  然后我们就可以在TSample类中实现TSample的接口了,代码如下: TSampleWrapper = class (TComponent)privateFSample: TSample;protectedfunction GetSomeValue: Integer;procedure SetSomeValue(Value: Integer);publicfunction SomeAction(const Data: string): Boolean;property Sample: TSample read FSample;property SomeValue: Integer read GetSomeValue write SetSomeValue;   接口实现代码如下: function TSampleWrapper.GetSomeValue: Integer;beginResult := Sample.SomeValue;end;procedure TSampleWrapper.SetSomeValue(Value: Integer);beginSample.SomeValue := Value;end;function TSampleWrapper.SomeAction(const Data: string): Boolean;beginResult := Sample.SomeAction(Data);end;   Wrapper设计模式能够把一个类的接口修改成另一个类所需要的接口,然后让本来因为接口不兼容的类能够在一起配合工作。   在Delphi中,为了让两个类能够支持同样的接口,它们必须有同样的祖先类这样才能在其他类调用的时候实现多态性。但是有的时候我们希望两个没有关系的类能够在一起工作,Wrapper设计模式就能够让一个类wrap部分另一个类的部分接口(当然也可以是全部,看具体的需要而定),从而能够模拟出一个类似于“uses”的类的多继承关系。   让我们来看一个例子,有一个类TSimple从TObject继承下来,但是我们希望能把TSimple这个类放到Delphi的控件面板(component palette)上,然而我们知道,如果一个类能够放置到控件面板上,这个类必须从TComponent派生下来。现在我们不想在改变TSimple的祖先类TObject的情况下(比如我们并没有TSimple的源代码)把TSimple放到控件面板上,我们可以构建一个继承自TComponent的TSimpleWapper类,在TSimpleWapper类中Wrap TSimple的接口。下面是这个例子的范例代码: type TSample = class (TObject)private FSomeValue: Integer;public function SomeAction(const Data: string): Boolean;  property SomeValue: Integer read FSomeValue write FSomeValue; end; TSampleWrapper = class(TComponent)private FSample: TSample;public property Sample: TSample read FSample;end;  然后我们就可以在TSample类中实现TSample的接口了,代码如下: TSampleWrapper = class (TComponent)privateFSample: TSample;protectedfunction GetSomeValue: Integer;procedure SetSomeValue(Value: Integer);publicfunction SomeAction(const Data: string): Boolean;property Sample: TSample read FSample;property SomeValue: Integer read GetSomeValue write SetSomeValue;   接口实现代码如下: function TSampleWrapper.GetSomeValue: Integer;beginResult := Sample.SomeValue;end;procedure TSampleWrapper.SetSomeValue(Value: Integer);beginSample.SomeValue := Value;end;function TSampleWrapper.SomeAction(const Data: string): Boolean;beginResult := Sample.SomeAction(Data);end; 下载本文示例代码


Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式Delphi中的Wrapper设计模式
阅读(138) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~