public class BasePhoneFactory implements PhoneFactory { @Override public Phone produce(PhoneType type) { switch (type){ case XIAOMI: return new XiaoMiPhone(); case OPPO: return new OppoPhone(); case HUAWEI: return new HuaWeiPhone(); default: throw new AssertionError("没有该类型的手机"); } }
public static void main(String[] args) { PhoneFactory phoneFactory = new BasePhoneFactory(); Phone phone = phoneFactory.produce(PhoneType.XIAOMI); phone.info(); } }