public abstract class Abstraction { protected Implementor impl; public Abstraction(Implementor impl) { this.impl = impl; } // 示例方法 public void operation() { impl.operationImpl(); } } public abstract class Implementor { /** * 示例方法,實現抽象部分須要的某些具體功能 */ public abstract void operationImpl(); }
public class ConcreteImplementorA extends Implementor { @Override public void operationImpl() { //具體操做 System.out.println(ConcreteImplementorA.class); } } public class ConcreteImplementorB extends Implementor { @Override public void operationImpl() { //具體操做 System.out.println(ConcreteImplementorB.class); } }
public class RefinedAbstraction extends Abstraction { public RefinedAbstraction(Implementor impl) { super(impl); } //其餘的操做方法 public void otherOperation(){ System.out.println(RefinedAbstraction.class); } }
public class Client { public static void main(String[] args) { Implementor a = new ConcreteImplementorA(); Abstraction ref = new RefinedAbstraction(a); ref.operation(); } }
學習設計模式強烈推薦的博客:java_my_lifejava
代碼地址:lennongit