weidagang2046的专栏

物格而后知致
随笔 - 8, 文章 - 409, 评论 - 57, 引用 - 0
数据加载中……

Adapter Design Pattern

Note:  Do not confuse the abstract concept of an "interface" with the Java "interface" type in the following discussion!  

Adapters are used to enable objects with different interfaces to communicate with each other.

Adapters come in two flavors, object adapters and class adapters.  These two methods embody the difference between the use of inheritance and composition to solve problems.

Object Adapters

Object adapters use a compositional technique to adapt one interface to another.    The adapter inherits the target interface that the client expects to see, while it holds an instance the adaptee.   When the client calls the request() method on its target object (the adapter), the request is translated into the corresponding specific request on the adaptee.

Object adapters enable the client and the adaptee to be completely decoupled from eachother.   Only the adapter knows about both of them.

Class Adapters

Class adapters use multiple inheritance to achieve their goals.   As in the object adapter, the class adapter inherits the interface of the client's target.   However, it also inherits the interface of the adaptee as well.   Since Java does not support true multiple inheritance, this means that one of the interfaces must be inherited from a Java Interface type.   Note that either or both of the target or adaptee interfaces could be an Java Interfaces.   The request to the target is simply rerouted to the specific request that was inherited fro the adaptee interface.

Note that class adapters have a problem with name conflicts if methods of the same signature exist on both the target and the adaptee.  Note that just because two objects have methods that have the same signature (syntax), it does not guarantee that the two methods have the same meaning or behavior (sematics).    That is, the two methods do not necessarily map directly to each other.   Object adapters do not have this problem.

Class adapters are simpler than object adapters in that they involve fewer classes and are useful if total decoupling of the client and adaptee is not needed.

from: http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/adapter.htm

posted on 2006-02-26 22:46 weidagang2046 阅读(49) 评论(0)  编辑  收藏 所属分类: Java




标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 
相关链接:
网站导航: