类适配器模式
/**
* 介绍:src类: 我们有的220V电压
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/18.
*/
public class Voltage220 {
public int output220V() {
int src = 220;
System.out.println("我是" + src + "V");
return src;
}
}
/**
* 介绍:dst接口:客户需要的5V电压
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/18.
*/
public interface Voltage5 {
int output5V();
}
/**
* 介绍:Adapter类:完成220V-5V的转变
* 通过继承src类,实现 dst 类接口,完成src->dst的适配。
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/18.
*/
public class VoltageAdapter extends Voltage220 implements Voltage5 {
@Override
public int output5V() {
int src = output220V();
System.out.println("适配器工作开始适配电压");
int dst = src / 44;
System.out.println("适配完成后输出电压:" + dst);
return dst;
}
}
/**
* 介绍:Client类:手机 .需要5V电压
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/18.
*/
public class Mobile {
/**
* 充电方法
*
* @param voltage5
*/
public void charging(Voltage5 voltage5) {
if (voltage5.output5V() == 5) {
System.out.println("电压刚刚好5V,开始充电");
} else if (voltage5.output5V() > 5) {
System.out.println("电压超过5V,都闪开 我要变成note7了");
}
}
}
System.out.println("===============类适配器==============");
Mobile mobile = new Mobile();
mobile.charging(new VoltageAdapter());
对象的适配器模式
/**
* 介绍:对象适配器模式:
* 持有 src类,实现 dst 类接口,完成src->dst的适配。 。以达到解决**兼容性**的问题。
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/18.
*/
public class VoltageAdapter2 implements Voltage5 {
private Voltage220 mVoltage220;
public VoltageAdapter2(Voltage220 voltage220) {
mVoltage220 = voltage220;
}
@Override
public int output5V() {
int dst = 0;
if (null != mVoltage220) {
int src = mVoltage220.output220V();
System.out.println("对象适配器工作,开始适配电压");
dst = src / 44;
System.out.println("适配完成后输出电压:" + dst);
}
return dst;
}
}
System.out.println("\n===============对象适配器==============");
VoltageAdapter2 voltageAdapter2 = new VoltageAdapter2(new Voltage220());
Mobile mobile2 = new Mobile();
mobile2.charging(voltageAdapter2);
接口的适配器模式
public interface A {
void a();
void b();
void c();
void d();
void e();
void f();
}
适配器
public abstract class Adapter implements A {
public void a(){}
public void b(){}
public void c(){}
public void d(){}
public void e(){}
public void f(){}
}
public class Ashili extends Adapter {
public void a(){
System.out.println("实现A方法被调用");
}
public void d(){
System.out.println("实现d方法被调用");
}
}
public class Clienter {
public static void main(String[] args) {
A a = new Ashili();
a.a();
a.d();
}
}
使用场景:
适配器模式应用场景
类适配器与对象适配器的使用场景一致,仅仅是实现手段稍有区别,
二者主要用于如下场景:
想要使用一个已经存在的类,但是它却不符合现有的接口规范,导致无法直接去访问,这时创建一个适配器就能间接去访问这个类中的方法。
我们有一个类,想将其设计为可重用的类(可被多处访问),我们可以创建适配器来将这个类来适配其他没有提供合适接口的类。
以上两个场景其实就是从两个角度来描述一类问题,那就是要访问的方法不在合适的接口里,一个从接口出发(被访问),一个从访问出发(主动访问)
接口适配器使用场景:
- 想要使用接口中的某个或某些方法,但是接口中有太多方法,我们要使用时必须实现接口并实现其中的所有方法,可以使用抽象类来实现接口,并不对方法进行实现(仅置空),然后我们再继承这个抽象类来通过重写想用的方法的方式来实现。这个抽象类就是适配器。
参考:
http://blog.csdn.net/a394268045/article/details/51801258