标签:style blog http ar color os 使用 sp div
适配器类实现目标接口,在接口方法中调用被适配类的方法。 客户端直接调用适配器对象。
客户端期望的接口:
1 public interface Target { 2 public void doSomething(); 3 }
需要适配的类:
1 public class Adaptee1 { 2 3 public void doSomething1(){ 4 System.out.println("I am Adptee1"); 5 } 6 }
1 public class Adaptee2 { 2 public void doSomething2(){ 3 System.out.println("I am Adptee2"); 4 } 5 }
适配器类:
1 public class Adapter implements Target { 2 private Adaptee1 adaptee1; 3 private Adaptee2 adaptee2; 4 5 public Adapter(Adaptee1 adaptee1, Adaptee2 adaptee2) { 6 this.adaptee1 = adaptee1; 7 this.adaptee2 = adaptee2; 8 } 9 10 @Override 11 public void doSomething() { 12 adaptee1.doSomething1(); 13 adaptee2.doSomething2(); 14 } 15 16 }
客户端:
1 public class Client { 2 3 public static void main(String[] args) { 4 Target target = new Adapter(new Adaptee1(), new Adaptee2()); 5 target.doSomething(); 6 } 7 8 }
运行结果:
I am Adptee1
I am Adptee2
标签:style blog http ar color os 使用 sp div
原文地址:http://www.cnblogs.com/longzhaoyu/p/4123832.html