適配器模式git
public interface ITarget { void show(); } public class Target implements ITarget{ public Target(){ System.out.println("create target..."); } @Override public void show() { System.out.println(this.getClass().getSimpleName()); } }
public interface IAdaptee { } public class Adaptee implements IAdaptee{ }
public class Adapter1 implements IAdaptee{ ITarget target; public ITarget convert(IAdaptee adaptee){ if(adaptee != null){ target = new Target(); } return target; } }
public class Adapter2 implements ITarget,IAdaptee{ @Override public void show() { System.out.println(this.getClass().getSimpleName()); } }
public static void main(String[] args) { Adapter1 adapter1 = new Adapter1(); ITarget target = adapter1.convert(new Adaptee()); target.show(); Adapter2 adapter2 = new Adapter2(); adapter2.show(); }
create target... Target Adapter2
https://github.com/Seasons20/DisignPattern.git
ENDgithub