public void MacFactoryTests() { var factory = new MacFactory(); var mac = factory.BuyNewComputer(); Assert.NotNull(mac); }
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(); }
public void Use_A_MacFactory_To_Create_A_Button() { GUIFactory factory = new MacFactory(); factory.CreateButton(window).Draw(window); Assert.AreEqual("MacButton", window.DrawnText); }
public void ExecutePattern() { Factory microsoft = new PCFactory(); Product msProduct = microsoft.Produce(); Console.WriteLine(msProduct.GetProduct()); Factory apple = new MacFactory(); Product appleProduct = apple.Produce(); Console.WriteLine(appleProduct.GetProduct()); }
public void AbstractMethodTest() { IComputadorFactory computadorFactory = new WindowsFactory(); var pcWindows = computadorFactory.CrearComputadorPC("2 GB", "500 GB", "2.4 Ghz"); TestContext.WriteLine(pcWindows.GetType().Name); TestContext.WriteLine(pcWindows.ObtenerHDD()); TestContext.WriteLine(pcWindows.ObtenerRam()); TestContext.WriteLine(pcWindows.ObtenerCPU()); var serverWindows = computadorFactory.CrearComputadorServidor("2 GB", "500 GB", "2.4 Ghz"); TestContext.WriteLine(serverWindows.GetType().Name); TestContext.WriteLine(serverWindows.ObtenerHDD()); TestContext.WriteLine(serverWindows.ObtenerRam()); TestContext.WriteLine(serverWindows.ObtenerCPU()); computadorFactory = new MacFactory(); var pcMac = computadorFactory.CrearComputadorPC("2 GB", "500 GB", "2.4 Ghz"); TestContext.WriteLine(pcMac.GetType().Name); TestContext.WriteLine(pcMac.ObtenerHDD()); TestContext.WriteLine(pcMac.ObtenerRam()); TestContext.WriteLine(pcMac.ObtenerCPU()); var serverMac = computadorFactory.CrearComputadorServidor("2 GB", "500 GB", "2.4 Ghz"); TestContext.WriteLine(serverMac.GetType().Name); TestContext.WriteLine(serverMac.ObtenerHDD()); TestContext.WriteLine(serverMac.ObtenerRam()); TestContext.WriteLine(serverMac.ObtenerCPU()); computadorFactory = new LinuxFactory(); var pcLinux = computadorFactory.CrearComputadorPC("2 GB", "500 GB", "2.4 Ghz"); TestContext.WriteLine(pcLinux.GetType().Name); TestContext.WriteLine(pcLinux.ObtenerHDD()); TestContext.WriteLine(pcLinux.ObtenerRam()); TestContext.WriteLine(pcLinux.ObtenerCPU()); var serverLinux = computadorFactory.CrearComputadorServidor("2 GB", "500 GB", "2.4 Ghz"); TestContext.WriteLine(serverLinux.GetType().Name); TestContext.WriteLine(serverLinux.ObtenerHDD()); TestContext.WriteLine(serverLinux.ObtenerRam()); TestContext.WriteLine(serverLinux.ObtenerCPU()); Assert.IsTrue(pcLinux is Pc); Assert.IsTrue(serverLinux is Server); Assert.IsTrue(pcWindows is Pc); Assert.IsTrue(serverWindows is Server); Assert.IsTrue(pcMac is Pc); Assert.IsTrue(serverMac is Server); }
/// <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================"); }
static Client GetClientOS() { IFactory concreteFactory; string OSName = RuntimeInformation.OSDescription.ToUpper(); if (OSName.Contains("WINDOWS")) { concreteFactory = new WinFactory(); } else if (OSName.Contains("MAC")) { concreteFactory = new MacFactory(); } else { throw new ArgumentException("Sistema operacional incompatível"); } return(new Client(concreteFactory)); }
static void Main(string[] args) { System.Console.WriteLine("Introduce OS name:"); string OS = Console.ReadLine(); IGUIFactory guiFactory; switch (OS) { case "Windows": default: guiFactory = new WinFactory(); break; case "Mac": guiFactory = new MacFactory(); break; } Application app = new Application(guiFactory); app.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(); }