适配器模式

将一个类的接口转换成另一个接口。 Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
目标接口Target
1 2 3
| public interface Target { void operation1(); }
|
待适配类Adaptee,与目标接口不一致
1 2 3
| public class Adaptee { void operation2() {} }
|
适配器类
1 2 3 4 5 6 7 8
| public class Adapter implements Target { private Adaptee adaptee = new Adaptee();
public void operation1() { adaptee.sampleOperation1(); } }
|
测试类
1 2 3 4 5 6 7 8 9 10
| public class AdapterClient { public static void main(String... args) { Target adapter = new Adapter(); adapter.operation1(); } }
|