static void Main(string[] args) { Array values = Enum.GetValues(typeof(OSSettings)); Random random = new Random(); OSSettings randomBar = (OSSettings)values.GetValue(random.Next(values.Length)); IGUIFactory factory; switch (randomBar) { case OSSettings.Win: factory = new WinFactory(); break; case OSSettings.OSX: factory = new OSXFactory(); break; default: throw new System.NotImplementedException(); } var button = factory.CreateButton(); button.Paint(); }
static void Main(string[] args) { IGUIFactory factory = new WinFactory(); var button = factory.CreateButton(); button.Paint(); }
private void abstractFactoryPatternBtn_Click(object sender, RoutedEventArgs e) { var appearance = OOPPatternsSettings.Default.Appearance; IGUIFactory factory; try { switch (appearance) { case Constants.WIN_APPEARANCE: factory = new WinFactory(); break; case Constants.OSX_APPEARANCE: factory = new OSXFactory(); break; default: throw new System.NotImplementedException(); } var button = factory.CreateButton(); button.Paint(); } catch (System.Exception ex) { if (ex.GetType() == typeof(System.NotImplementedException)) { statusBarTB.Text = "That format of type " + appearance + ", wasn't implemented!"; } } }
static void AbstractFactory() { var os = ""; Console.WriteLine("Enter your OS (mac or win): "); do { os = Console.ReadLine(); }while (os != "win" && os != "mac"); IGUIFactory factory; if (os == "mac") { factory = new MacFactory(); } else { factory = new WinFactory(); } var app = new Application(factory); app.CreateUI(); app.Paint(); Console.ReadLine(); }
/// <summary> /// Problem: Have to implement GUI elements(Button, Checkbox) for Windows and Mac OS (etc...) /// Solved: Use Factory pattern for solving this problem /// </summary> public override void Demo() { Console.WriteLine($"================Factory================{Environment.NewLine}"); var winFactory = new WinFactory(); var macFactory = new MacFactory(); var winButton = winFactory.createButton(); var macButton = macFactory.createButton(); Console.WriteLine(winButton.Clicked()); Console.WriteLine(macButton.Clicked()); var winCheckbox = winFactory.createCheckbox(); var macCheckbox = macFactory.createCheckbox(); Console.WriteLine(winCheckbox.Checked()); Console.WriteLine(macCheckbox.Checked()); Console.WriteLine($"{Environment.NewLine}================Factory================"); }
private static void AbstractFactoryPatternDemo() { var key = Console.ReadLine(); IGUIFactory factory; switch (key) { case "W": factory = new WinFactory(); break; case "O": factory = new OsxFactory(); break; default: factory = new WinFactory(); break; } CreateWindowWithTwoButtonsAndProgressBar(factory); }
static void Main() { var appearance = Settings.Appearance; IGUIFactory factory; switch (appearance) { case Appearance.Win: factory = new WinFactory(); break; case Appearance.OSX: factory = new OSXFactory(); break; default: throw new System.NotImplementedException(); } var button = factory.CreateButton(); button.Paint(); }
static void Main(string[] args) { try { Console.WriteLine("Введите ОС(win, mac, ubuntu):"); string str = Console.ReadLine(); if (str == "win") { Inp(new WinFactory()); WinFactory factory = new WinFactory(); // передаем в конструктор тип класса XmlSerializer formatter = new XmlSerializer(typeof(WinFactory)); File.Delete("Factory.xml"); // получаем поток, куда будем записывать сериализованный объект using (FileStream fs = new FileStream("Factory.xml", FileMode.OpenOrCreate)) { formatter.Serialize(fs, factory); } // десериализация using (FileStream fs = new FileStream("Factory.xml", FileMode.OpenOrCreate)) { WinFactory newFactory = (WinFactory)formatter.Deserialize(fs); } } else { if (str == "mac") { Inp(new MacFactory()); MacFactory factory = new MacFactory(); // передаем в конструктор тип класса XmlSerializer formatter = new XmlSerializer(typeof(MacFactory)); File.Delete("Factory.xml"); // получаем поток, куда будем записывать сериализованный объект using (FileStream fs = new FileStream("Factory.xml", FileMode.OpenOrCreate)) { formatter.Serialize(fs, factory); } // десериализация using (FileStream fs = new FileStream("Factory.xml", FileMode.OpenOrCreate)) { MacFactory newFactory = (MacFactory)formatter.Deserialize(fs); } } else { if (str == "ubuntu") { Inp(new UbFactory()); UbFactory factory = new UbFactory(); // передаем в конструктор тип класса XmlSerializer formatter = new XmlSerializer(typeof(UbFactory)); File.Delete("Factory.xml"); // получаем поток, куда будем записывать сериализованный объект using (FileStream fs = new FileStream("Factory.xml", FileMode.OpenOrCreate)) { formatter.Serialize(fs, factory); } // десериализация using (FileStream fs = new FileStream("Factory.xml", FileMode.OpenOrCreate)) { UbFactory newFactory = (UbFactory)formatter.Deserialize(fs); } } else { Console.WriteLine("Данной ОС не существует"); } } } } catch { Console.WriteLine("Введены неверные данные"); } Console.ReadKey(); }