static void Main(string[] args) { Console.WriteLine("Please choose the brand you want to buy from: Apple (a) or Huawei (h): "); char input = Console.ReadKey().KeyChar; ElectronicsFactory electronicsFactory; switch (input) { case 'a': electronicsFactory = new AppleFactory(); break; case 'h': electronicsFactory = new HuaweiFactory(); break; default: throw new NotImplementedException(); } var computer = electronicsFactory.CreateComputer(); var phone = electronicsFactory.CreatePhone(); Console.WriteLine("\nComputer: " + computer.GetType().Name); Console.WriteLine("Phone: " + phone.GetType().Name); Console.ReadKey(); }
static void Main(string[] args) { var samsungFactory = new SamsungFactory(); var samsungPhone = samsungFactory.GetPhone(); var samsungTablet = samsungFactory.GetTablet(); Console.WriteLine("Samsung factory:"); Console.WriteLine($"Phone - {samsungPhone.Name}\nTablet - {samsungTablet.Name}"); var appleFactory = new AppleFactory(); var applePhone = appleFactory.GetPhone(); var appleTablet = appleFactory.GetTablet(); Console.WriteLine("Apple factory:"); Console.WriteLine($"Phone - {applePhone.Name}\nTablet - {appleTablet.Name}"); }
public void Execute() { Console.WriteLine("请选择产品:1.Iphone 2.Ipad"); string productNumber = Console.ReadLine(); AppleFactory af = null; if (productNumber == "1") { af = new IphoneFactory(); } else { af = new IpadFactory(); } af.CreateProductSize().Size(); af.CreateProductPrice().Price(); }
static void Main(string[] args) { Console.WriteLine("Are you an apple fangirl?"); var isFangirl = Console.ReadLine() == "yes" ? true : false; ITechnologyAbstractFactory techFactory = null; if (isFangirl) { techFactory = new AppleFactory(); } else { techFactory = new SamsungFactory(); } IMobilePhone myphone = techFactory.CreatePhone(); ITablet mytablet = techFactory.CreateTablet(); }
static void Main(string[] args) { //小米工厂生产小米手机的屏幕和主板 AbstractFactory xiaomiFactory = new XiaoMiFactory(); Screen xiaomiScreen = xiaomiFactory.CreateScreen(); xiaomiScreen.print(); MotherBoard xiaomiMotherBoard = xiaomiFactory.CreateMotherBoard(); xiaomiMotherBoard.print(); //苹果工厂生产苹果手机屏幕和主板 AbstractFactory appleFactory = new AppleFactory(); Screen appleScreen = appleFactory.CreateScreen(); appleScreen.print(); MotherBoard appleMotherBoard = appleFactory.CreateMotherBoard(); appleMotherBoard.print(); Console.Read(); }
static void Main(string[] args) { Console.WriteLine("Hello Design Patterns!"); Console.WriteLine(); // Create and run the DELL factory IComputerFactory dell = new DellFactory(); Console.WriteLine(); // I need a dell desktop computer var newDellDesktopOfMine = dell.CreateDesktop("my new"); newDellDesktopOfMine.GetType(); // My brother need a dell notebook var newMacbookOfMyBrother = dell.CreateNotebook("my brother`s new"); newMacbookOfMyBrother.GetType(); Console.WriteLine(); // Create and run Apple factory IComputerFactory apple = new AppleFactory(); Console.WriteLine(); // My new MacBook Pro var newMacbookOfMine = apple.CreateNotebook("my new"); newMacbookOfMine.GetType(); // My brother`s new Mac Mini desktop computer var newMacMiniOfMyBrother = apple.CreateDesktop("my brother`s new"); newMacbookOfMyBrother.GetType(); Console.ReadLine(); }
//抽象工厂模式主要是一个具体工厂可以创建多种类型具体产品 static void Main(string[] args) { Factory factory; MobilePhone phone; PersonalComputer pc; factory = new MIFactory(); phone = factory.CreatePhone(); pc = factory.CreatePC(); Console.WriteLine($"{factory.FactoryName}:{phone.PhoneName}:{pc.PCName}"); phone.Calling(); pc.Online(); Console.WriteLine(); factory = new AppleFactory(); phone = factory.CreatePhone(); pc = factory.CreatePC(); Console.WriteLine($"{factory.FactoryName}:{phone.PhoneName}:{pc.PCName}"); phone.Calling(); pc.Online(); Console.ReadKey(); }
static void Main(string[] args) { //小米工厂生产小米手机的屏幕和主板 Console.WriteLine("小米工厂:"); AbstractFactory xiaomiFactory = new XiaoMiFactory(); Screen xiaomiScreen = xiaomiFactory.CreateScreen(); xiaomiScreen.Print(); MotherBoard xiaomiMotherBoard = xiaomiFactory.CreateMotherBoard(); xiaomiMotherBoard.Print(); Console.WriteLine(); //苹果工厂生产苹果手机屏幕和主板 Console.WriteLine("苹果工厂:"); AbstractFactory appleFactory = new AppleFactory(); Screen appleScreen = appleFactory.CreateScreen(); appleScreen.Print(); MotherBoard appleMotherBoard = appleFactory.CreateMotherBoard(); appleMotherBoard.Print(); Console.WriteLine(); //华为工厂生产苹果手机屏幕和主板 Console.WriteLine("华为工厂:"); AbstractFactory huaWeiFactory = new HuaWeiFactory(); Screen huaWeiScreen = huaWeiFactory.CreateScreen(); appleScreen.Print(); MotherBoard huaWeiMotherBoard = appleFactory.CreateMotherBoard(); appleMotherBoard.Print(); Console.Read(); }