Target(目标接口):所要转换的所期待的接口
Adaptee(源角色):需要适配的接口
Adapter (适配器):将源接口适配成目标接口,继承源接口,实现目标接口
Adaptee
package design.pattern.adapter; public interface Adaptee { public void adapteeWorkType(); }
Target
package design.pattern.adapter; public interface Target { public void targetWorkType(); }
Adapter
package design.pattern.adapter; public class Adapter implements Target{ private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void targetWorkType() { adaptee.adapteeWorkType(); } }
ConcreteAdaptee
package design.pattern.adapter; public class ConcreteAdaptee implements Adaptee{ @Override public void adapteeWorkType() { System.out.println("adapteeWork"); } }
Client
package design.pattern.adapter; public class Client { public static void main(String[] args) { new Adapter(new ConcreteAdaptee()).targetWorkType(); } }