public static final int HP = 1; public static final int LENOVO = 2; public static final int APPLE = 3;
public static Computer createComputer(int param) { switch (param) { case HP: return new HpComputer(); case LENOVO: return new LenovoComputer(); case APPLE: return new AppleComputer(); default: return null; } } }
// 父类产品工厂 public interface ComputerProductFactory {
Keyboard createKeyboard();
Mouse createMouse();
Earphone createEarphone(); } // 惠普产品工厂实现 public class HpProductFactory implements ComputerProductFactory { @Override public Keyboard createKeyboard() { return new HpKeyboard(); }
@Override public Mouse createMouse() { return new HpMouse(); }
@Override public Earphone createEarphone() { return new HpEarphone(); } } // 联想产品工厂实现 public class LenovoProductFactory implements ComputerProductFactory { @Override public Keyboard createKeyboard() { return new LenovoKeyboard(); }
@Override public Mouse createMouse() { return new LenovoMouse(); }
@Override public Earphone createEarphone() { return new LenovoEarphone(); } }