电压实体类
package com.sheting.design.pattern.demo4;
/**
* @Author SheTing
* @Time 2018/3/22 7:55
*/
public class Volt {
private int volts;
public Volt(int v) {
this.volts = v;
}
public int getVolts() {
return volts;
}
public void setVolts(int volts) {
this.volts = volts;
}
}
默认是120V的插板
package com.sheting.design.pattern.demo4;
/**
* @Author SheTing
* @Time 2018/3/22 7:56
*/
public class Socket {
public Volt getVolt(){
return new Volt(120);
}
}
插板适配类
包含默认的120V,新增了12V和3V
package com.sheting.design.pattern.demo4;
/**
* @Author SheTing
* @Time 2018/3/22 7:56
*/
public interface SocketAdapter {
Volt get120Volt();
Volt get12Volt();
Volt get3Volt();
}
通过继承Socket,引入默认的Socket,同时实现SocketAdapter 新增3V 和 12V
package com.sheting.design.pattern.demo4;
/**
* @Author SheTing
* @Time 2018/3/22 7:58
*/
public class SocketClassAdapterImpl extends Socket implements SocketAdapter {
@Override
public Volt get120Volt() {
return getVolt();
}
@Override
public Volt get12Volt() {
Volt v = getVolt();
return convertVolt(v, 10);
}
@Override
public Volt get3Volt() {
Volt v = getVolt();
return convertVolt(v, 40);
}
private Volt convertVolt(Volt v, int i) {
return new Volt(v.getVolts() / i);
}
}
通过组合引入Socket,同时实现SocketAdapter 新增3V 和 12V
package com.sheting.design.pattern.demo4;
/**
* @Author SheTing
* @Time 2018/3/22 8:00
*/
public class SocketObjectAdapterImpl implements SocketAdapter {
//Using Composition for adapter pattern
private Socket sock = new Socket();
@Override
public Volt get120Volt() {
return sock.getVolt();
}
@Override
public Volt get12Volt() {
Volt v = sock.getVolt();
return convertVolt(v, 10);
}
@Override
public Volt get3Volt() {
Volt v = sock.getVolt();
return convertVolt(v, 40);
}
private Volt convertVolt(Volt v, int i) {
return new Volt(v.getVolts() / i);
}
}
测试
package com.sheting.design.pattern.demo4;
/**
* @Author SheTing
* @Time 2018/3/22 7:59
*/
public class AdapterPatternTest {
public static void main(String[] args) {
testClassAdapter();
testObjectAdapter();
}
private static void testObjectAdapter() {
SocketAdapter sockAdapter = new SocketObjectAdapterImpl();
Volt v3 = getVolt(sockAdapter, 3);
Volt v12 = getVolt(sockAdapter, 12);
Volt v120 = getVolt(sockAdapter, 120);
System.out.println("v3 volts using Object Adapter=" + v3.getVolts());
System.out.println("v12 volts using Object Adapter=" + v12.getVolts());
System.out.println("v120 volts using Object Adapter=" + v120.getVolts());
}
private static void testClassAdapter() {
SocketAdapter sockAdapter = new SocketClassAdapterImpl();
Volt v3 = getVolt(sockAdapter, 3);
Volt v12 = getVolt(sockAdapter, 12);
Volt v120 = getVolt(sockAdapter, 120);
System.out.println("v3 volts using Class Adapter=" + v3.getVolts());
System.out.println("v12 volts using Class Adapter=" + v12.getVolts());
System.out.println("v120 volts using Class Adapter=" + v120.getVolts());
}
private static Volt getVolt(SocketAdapter sockAdapter, int i) {
switch (i) {
case 3:
return sockAdapter.get3Volt();
case 12:
return sockAdapter.get12Volt();
case 120:
return sockAdapter.get120Volt();
default:
return sockAdapter.get120Volt();
}
}
}
Adapter Design Pattern Example in JDK
- java.util.Arrays#asList()
- java.io.InputStreamReader(InputStream) (returns a Reader)
- java.io.OutputStreamWriter(OutputStream) (returns a Writer)
- java.awt.event.MouseAdapter
网友评论