一个工厂会制造很多产品,比如产品A和B,但它下属的生产工作室可能只生产A和B的其中一种,这便引进了适配器模式。
贴上代码:
接口:
public interface IFactory {
public void makeA();
public void makeB();
}
抽象类:
public abstract class ControlCentre implements IFactory {
public void makeA() {
// TODO Auto-generated method stub
}
public void makeB() {
// TODO Auto-generated method stub
}
}
只生产A的车间:
public class FactoryA extends ControlCentre {
public void makeA() {
System.out.println("我只制造A");
}
}
只生产B的车间:
public class FactoryB extends ControlCentre {
public void makeB() {
System.out.println("我只制造B");
}
}
public class TestFactory {
public static void main(String[] args) {
FactoryA a = new FactoryA();
FactoryB b = new FactoryB();
a.makeA();
b.makeB();
}
}
运行结果:
我只制造A
我只制造B