static void Main(string[] args) { Console.WriteLine("Hello World!"); //小米工厂生产 ProductFactory xiaomi = new MiFactory(); PadProduct mipad = xiaomi.CreatePad(); mipad.Print(); PhoneProduct miphone = xiaomi.CreatePhone(); miphone.Print(); //苹果工厂生产 ProductFactory apple = new AppleFactory(); PadProduct ipad = apple.CreatePad(); ipad.Print(); PhoneProduct iphone = apple.CreatePhone(); iphone.Print(); //当有新的公司时(Nexus) //创建 NexusFactory:继承工厂基类,实现创建pad产品、phone产品方法 //创建 NexusPad:继承Pad产品基类,实现Pad产品信息 //创建 NexusPhone:继承Phone产品基类,实现Phone产品信息 Console.ReadKey(); }
static void Main(string[] args) { // Factory in Asia creates phone and tablet Console.WriteLine("----------- ASIA MOBILE DEVICE FACTORY -----------"); Console.WriteLine(); MobileDeviceAbstractFactory abstractASIAFactory = new MobileDeviceAsiaConcreteFactory(); PhoneProduct phoneAsia = abstractASIAFactory.CreatePhone(); phoneAsia.CreatePhoneProduct(); TabletProduct tabletAsia = abstractASIAFactory.CreateTablet(); tabletAsia.CreateTabletProduct(); Console.WriteLine(); Console.WriteLine(); // Factory in US creates phone and tablet Console.WriteLine("----------- US MOBILE DEVICE FACTORY -----------"); Console.WriteLine(); MobileDeviceAbstractFactory abstractUSFactory = new MobileDeviceUSConcreteFactory(); PhoneProduct phoneUS = abstractUSFactory.CreatePhone(); phoneUS.CreatePhoneProduct(); TabletProduct tabletUS = abstractUSFactory.CreateTablet(); tabletUS.CreateTabletProduct(); }