上一篇:设计模式(五)——建造者模式

下一篇:设计模式(七)——桥接模式

一、概要

官方解释:Convert the inface of a class into another interface clients expect.Adapter lets classes work together that couldn’t otherwise because of incompatible interface.(将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。) 

我的理解:

适配器模式三要素:适配者(Adaptee,即源)、适配器Adapter、目标Target

两种实现方式:类适配器、对象适配器

参与者:Target目标类、Adaptee适配者类、Adapter适配器类

类图:

设计模式(六)——适配器模式_设计模式

二、代码

2.1 不使用适配器模式

class Adaptee {
	public void display() {
		System.out.println("This is an Adaptee");
	}
}

interface Target {
	 void _target();
}

如上,有一个源Adaptee和一个目标Target。

如何在Target使用适配者Adaptee的方法?使用继承适配者或者注入引用方法,但是要在一个中间类去实现。

因为Target是接口或者抽象类(无法在客户端实例化对象),所以要想在在Target使用适配者Adaptee的方法,这个中间类不能由Target充当。

 然后写一串代码来实现这个中间类的需求:

package mypackage;

public class TestAdapterPattern {

	public static void main(String[] args) {
		ConcreteTarget _tTarget = new ConcreteTarget();
		_tTarget.set_adaptee(new Adaptee());
		_tTarget._target();
	}

}

class Adaptee {
	public void display() {
		System.out.println("This is an Adaptee");
	}
}

interface Target {
	void _target();
}

class ConcreteTarget implements Target {
	protected Adaptee _adaptee;

	@Override
	public void _target() {
		_adaptee.display();
	}

	public Adaptee get_adaptee() {
		return _adaptee;
	}

	public void set_adaptee(Adaptee _adaptee) {
		this._adaptee = _adaptee;
	}
}

输出:

This is an Adaptee

 写完后发现,实际上已经使用了适配器模式,这就是对象适配器的思想(设计模式就是这样,随手一写,以为没有用到设计模式,实际上已经用到了)

本来考虑不使用适配器模式,写一个中间类将Adaptee引用注入进去,调用其方法,结果发现使用了对象适配器,充分说明适配器模式是绕不开的。(这就是设计模式的好处,它更多的是思想而不是形式,它不强迫你必须使用它,但是当你编写代码或者重构代码的时候,你会无法绕开它,不知不觉的使用上它。)

下面介绍类适配器和对象适配器:

2.2 类适配器

代码

package mypackage;

public class TestAdapterPattern {

	public static void main(String[] args) {
		Adapter _tAdapter = new Adapter();
		_tAdapter._target();
	}

}

class Adaptee {
	public void display() {
		System.out.println("This is an Adaptee");
	}
}

interface Target {
	void _target();
}

class Adapter extends Adaptee implements Target {

	@Override
	public void _target() {
		super.display();
	}

}

输出:

This is an Adaptee

小结:要想Target调用Adaptee中的方法,只有继承和组成两种,类适配器使用了继承Adaptee的方法,Adaptee成为了中间类Adapter的父类,Adapter可以随时方便调用其方法。

2.3 对象适配器

代码:

package mypackage;

public class TestAdapterPattern {

	public static void main(String[] args) {
		Adapter _tAdapter = new Adapter();
		_tAdapter.set_adaptee(new Adaptee());
		_tAdapter._target();
	}

}

class Adaptee {
	public void display() {
		System.out.println("This is an Adaptee");
	}
}

interface Target {
	void _target();
}

class Adapter implements Target {
	protected Adaptee _adaptee;

	@Override
	public void _target() {
		_adaptee.display();
	}

	public Adaptee get_adaptee() {
		return _adaptee;
	}

	public void set_adaptee(Adaptee _adaptee) {
		this._adaptee = _adaptee;
	}
}

输出:

This is an Adaptee

小结:对象适配器采用了组合的方法,将Adaptee注入到中间类Adapter中,可以方便调用其方法。

三、总结

适配者模式很简单,就是要在一个类Target中调用另一个类Adaptee的方法或访问其成员,可以有继承和组合两种方法。按理说直接Target继承或者组合Adaptee就好了,但是当Target是接口或者抽象类时,就无法在客户端实例化对象,所有Target必须有继承或实现于自己的实现类——非抽象类Adapter,也是在这个Adapter类继承或组合Adaptee,就形成了类适配器和对象适配器了。就可以实现Target调用Adaptee的方法了,因为Target是中间类Adapter的父类。

 

上一篇:设计模式(五)——建造者模式

下一篇:设计模式(七)——桥接模式