策略模式【Strategy Pattern 】
interface Istragy{ void operate(); } class A implements Istragy{ @Override public void operate() { System.out.println("A"); } } class B implements Istragy{ @Override public void operate() { System.out.println("B"); } } class Constent{ private Istragy is = null; public Constent(Istragy is) { this.is = is; } public void runOp(){ this.is.operate(); } } public class Do{ public static void main(String[] args) { Constent constenta = new Constent(new A()); constenta.runOp(); Constent constentb = new Constent(new B()); constentb.runOp(); } }
找机会实用吧,会写白搭。