weidagang2046的专栏

物格而后知致
随笔 - 8, 文章 - 409, 评论 - 101, 引用 - 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 阅读(215) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航: