中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>数据库开发>设计模式>文章内容

delphi Adapter Pattern

来源:站内 关于:bill 发布时间:2007-06-23   [收藏] [推荐]

Converts the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't do so otherwise because of incompatible interfaces.

  • Use when you want to use an existing class and its interface does not match the one you need, or has an incompatible interface.

This pattern is also known as a wrapper and there are variations of it, such as "pluggable adapters," which are popular in Smalltalk and NextStep environments. The one presented here is known as an "object adapter" and consists of an adaptee, which defines the existing interface being adapted, and a target, which defines the interface the client class uses. The Adapter class inherits the target interface, and forwards the requests to the adaptee by using composition.

Structure of Adapter Pattern

Adapter Graphic

Implementation of Adapter Pattern

In this sample (uSingletonF.pas) the TFormAdapter is used to adapt the TSingletonForm (adaptee) interface to that of the TObserver (target) interface, which is required in order to be able to attach to TMySubject as an observer and receive notifications of state changes. TFormAdapter forwards any received Update() requests to the specific method corresponding to that request on the TSingletonForm interface, the UpdateView() method.

TFormAdapter = class(TObserver)
  private
    ;FAdaptee: TSingletonForm;
  public
    constructor Create(aForm: TSingletonForm);
    procedure Update(ChangedSubject: TObservable); override;
end;

implementation
//  TFormAdapter
constructor TFormAdapter.Create(aForm: TSingletonForm);
begin
  inherited Create;
  FAdaptee := aForm;
end;

procedure TFormAdapter.Update(ChangedSubject: TObservable);
begin
  FAdaptee.UpdateView;
  // adapted interface
end;

Conclusion

Patterns are a vehicle for capturing and documenting "best practices" in any field. In software engineering, design patterns allow you to improve your OO designs and architectures, improve documentation and facilitate maintenance of source code. If all members of your team understand the pattern names you are referring to then patterns allow a higher level of communication during analysis/design discussions. Using patterns allows you to leverage the expertise of others.

If you are an expert in your field, write down some patterns and have your peers review them. If you are not an expert, learn the published patterns. The more patterns you know, the better a designer and solution provider you will become and it will be reflected in improved quality of the products you build. This quality and aesthetic elegance in designs is referred to as "Quality Without A Name" (QWAN) by Christopher Alexander because it imparts incommunicable beauty and immeasurable value to a structure. (For more on QWAN, see the sources of information in the Appendix.)


[浏览: 次]   
上一篇:delphi Observer Pattern   下一篇:delphi treeview 使用
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi COM+对象缓冲池
·delphi Delphi_Delphi面向对象编程的20
·delphi Template Pattern (模板)
·delphi Singleton Pattern (设计模式)
·delphi Observer Pattern
·delphi Strategy Pattern (策略)
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932