UML
要实现的功能抽象类
public abstract class Implementor {
public abstract void operation();
}
具体实现功能的两个类
public class ConcreteImplementorA extends Implementor {
@Override
public void operation() {
System.out.println("具体实现类A的方法执行》》》");
}
}
public class ConcreteImplementorB extends Implementor {
@Override
public void operation() {
System.out.println("具体实现类B的方法执行》》》");
}
}
对以上功能有需求的抽象类
public class Abstraction {
protected Implementor implementor;
public void setImplementor(Implementor implementor) {
this.implementor = implementor;
}
public void operation() {
implementor.operation();
}
}
具体的产品
public class RefinedAbstraction extends Abstraction {
@Override
public void operation() {
super.operation();
}
}
测试类
public class Main {
public static void main(String[] args) {
Abstraction abstraction = new RefinedAbstraction();
abstraction.setImplementor(new ConcreteImplementorA());
abstraction.operation();
abstraction.setImplementor(new ConcreteImplementorB());
abstraction.operation();
}
}
运行结果