适配器模式定义:
将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份.
为何使用?
我们经常碰到要将两个没有关系的类组合在一起使用,第一解决方案是:修改各自类的接口,但是如果我们没有源代码,或者,我们不愿意为了一个应用而修改各自的接口。 怎么办?
使用Adapter,在这两种接口之间创建一个混合接口(混血儿).
如何使用?
实现Adapter方式,其实"think in Java"的"类再生"一节中已经提到,有两种方式:组合(composition)和继承(inheritance).
假设我们要打桩,有两种类:方形桩 圆形桩.
public class SquarePeg{
public void insert(String str){
System.out.println("SquarePeg insert():"+str);
}
}
public class RoundPeg{
public void insertIntohole(String msg){
System.out.println("RoundPeg insertIntoHole():"+msg);
}
}
现在有一个应用,需要既打方形桩,又打圆形桩.那么我们需要将这两个没有关系的类综合应用.假设RoundPeg我们没有源代码,或源代码我们不想修改,那么我们使用Adapter来实现这个应用:
public class PegAdapter extends SquarePeg{
private RoundPeg roundPeg;
public PegAdapter(RoundPeg peg)(this.roundPeg=peg;)
public void insert(String str){ roundPeg.insertIntoHole(str);}
}
在上面代码中,RoundPeg属于Adaptee,是被适配者.PegAdapter是Adapter,将Adaptee(被适配者RoundPeg)和Target(目标SquarePeg)进行适配.实际上这是将组合方法(composition)和继承(inheritance)方法综合运用.
PegAdapter首先继承SquarePeg,然后使用new的组合生成对象方式,生成RoundPeg的对象roundPeg,再重载父类insert()方法。从这里,你也了解使用new生成对象和使用extends继承生成对象的不同,前者无需对原来的类修改,甚至无需要知道其内部结构和源代码.
如果你有些Java使用的经验,已经发现,这种模式经常使用。
进一步使用
上面的PegAdapter是继承了SquarePeg,如果我们需要两边继承,即继承SquarePeg 又继承RoundPeg,因为Java中不允许多继承,但是我们可以实现(implements)两个接口(interface)
public interface IRoundPeg{
public void insertIntoHole(String msg);
}
public interface ISquarePeg{
public void insert(String str);
}
下面是新的RoundPeg 和SquarePeg, 除了实现接口这一区别,和上面的没什么区别。
public class SquarePeg implements ISquarePeg{
public void insert(String str){
System.out.println("SquarePeg insert():"+str);
}
}
public class RoundPeg implements IRoundPeg{
public void insertIntohole(String msg){
System.out.println("RoundPeg insertIntoHole():"+msg);
}
}
下面是新的PegAdapter,叫做two-way adapter:
public class PegAdapter implements IRoundPeg,ISquarePeg{
private RoundPeg roundPeg;
private SquarePeg squarePeg;
// 构造方法
public PegAdapter(RoundPeg peg){this.roundPeg=peg;}
// 构造方法
public PegAdapter(SquarePeg peg)(this.squarePeg=peg;)
public void insert(String str){ roundPeg.insertIntoHole(str);}
}
还有一种叫Pluggable Adapters,可以动态的获取几个adapters中一个。使用Reflection技术,可以动态的发现类中的Public方法。
c#的Adapter模式
using System;
namespace DoFactory.GangOfFour.Adapter.Structural
{
// Mainapp test application
class MainApp
{
static void Main()
{
// Create adapter and place a request
Target target = new Adapter();
target.Request();
// Wait for user
Console.Read();
}
}
// "Target"
class Target
{
public virtual void Request()
{
Console.WriteLine("Called Target Request()");
}
}
// "Adapter"
class Adapter : Target
{
private Adaptee adaptee = new Adaptee();
public override void Request()
{
// Possibly do some other work
// and then call SpecificRequest
adaptee.SpecificRequest();
}
}
// "Adaptee"
class Adaptee
{
public void SpecificRequest()
{
Console.WriteLine("Called SpecificRequest()");
}
}
}
---------------------------------------------------------------------------------------------
In computer programming, the adapter design pattern (sometimes referred to as the wrapper pattern or simply a wrapper) 'adapts' one interface for a class into one that a client expects. An adapter allows classes to work together that normally could not because of incompatible interfaces by wrapping its own interface around that of an already existing class.
There are two types of adapter patterns:
(本人英语也不怎么样,翻译了还不如不翻译呢 呵呵)
The Object Adapter pattern - In this type of adapter pattern the adapter contains an instance of the class it wraps. In this situation the adapter makes calls to a physical instance of the wrapped object.

The Class Adapter pattern - This type of adapter uses multiple inheritance to achieve its goal. The adapter is created inheriting interfaces from both the interface that is expected and the interface that is pre-existing. The Object Adapter pattern is more often used as some popular languages, such as Java, do not support true multiple inheritance as the designers of these languages consider it a dangerous practice.

The adapter pattern is useful in situations where an already existing class provides some or all of the services you need but does not use the interface you need. A good real life example is an adapter that converts the interface of a Document Object Model of an XML document into a tree structure that can be displayed. A link to a tutorial that uses the adapter design pattern is listed in the links below.
[edit]
Sample - Object Adapter
/**
* Java code sample
*/
interface Stack
{
void push (Object o);
Object pop ();
Object top ();
}
/* DoubleLinkedList */
class DList
{
public void insert (DNode pos, Object o) { ... }
public void remove (DNode pos, Object o) { ... }
public void insertHead (Object o) { ... }
public void insertTail (Object o) { ... }
public Object removeHead () { ... }
public Object removeTail () { ... }
public Object getHead () { ... }
public Object getTail () { ... }
}
/* Adapt DList class to Stack interface */
class DListStack implements Stack
{
private DList _dlist;
public DListStack() { _dlist = new DList(); }
public void push (Object o) {
_dlist.insertTail (o);
}
public Object pop () {
return _dlist.removeTail ();
}
public Object top () {
return _dlist.getTail ();
}
}
[edit]
Sample - Class Adaptor1
/**
* Java code sample
*/
interface Stack
{
void push (Object);
Object pop ();
Object top ();
}
/* DoubleLinkedList */
class DList
{
public void insert (DNode pos, Object o) { ... }
public void remove (DNode pos, Object o) { ... }
public void insertHead (Object o) { ... }
public void insertTail (Object o) { ... }
public Object removeHead () { ... }
public Object removeTail () { ... }
public Object getHead () { ... }
public Object getTail () { ... }
}
/* Adapt DList class to Stack interface */
class DListImpStack extends DList implements Stack
{
public void push (Object o) {
insertTail (o);
}
public Object pop () {
return removeTail ();
}
public Object top () {
return getTail ();
}
}
No comments:
Post a Comment