public static void FactoryGenerations() { int n = 10_000_000; IComputer notebook; // 1. SIMPLE FACTORY var simpleFactory = ComputerFactory.Instance; for (int i = 0; i < n; i++) { notebook = simpleFactory.CreateNotebook("Acer"); } // 2. FACTORY METHOD var factoryMethod = NotebookFactory.Instance; for (int i = 0; i < n; i++) { notebook = factoryMethod.CreateComputer("Acer"); } // 3. ABSTRACT FACTORY var abstractFactory = EpicFactory.Instance; for (int i = 0; i < n; i++) { notebook = abstractFactory.CreateNotebook(); } // 4. GENERIC ABSTRACT FACTORY var genericAbstract = new EpicCompany(CreatonFactory.Instance); for (int i = 0; i < n; i++) { notebook = genericAbstract.CreateNotebook(); } // 5. NON-REFLECTIVE FACTORY var nonReflection = NonReflectedFactory.Instance; nonReflection.RegisterNotebook("Acer", typeof(AcerNotebook)); for (int i = 0; i < n; i++) { notebook = nonReflection.CreateNotebook("Acer"); } // 6. NON-REFLECTIVE GENERIC FACTORY nonReflection = NonReflectedFactory.Instance; for (int i = 0; i < n; i++) { notebook = nonReflection.CreateNotebook <AcerNotebook>(); } // 7. REFLECTIVE FACTORY var reflection = ReflectedFactory.Instance; reflection.RegisterNotebooks(); for (int i = 0; i < n; i++) { notebook = reflection.CreateNotebook("Acer"); } }
public static void FactoryGenerateWithTimes() { int n = 10000000; Console.WriteLine(n + " iterations for each method..."); var watch = new Stopwatch(); double[] times = new double[7]; string[] methodNames = { "SimpleFactory", "FactoryMethod", "AbstractFact.", "Generic", "NoReflection", "GenericNoReflection", "Reflection" }; IComputer notebook; var simpleFactory = ComputerFactory.Instance; watch.Start(); for (int i = 0; i < n; i++) { notebook = simpleFactory.CreateNotebook("Acer"); } watch.Stop(); times[0] = watch.ElapsedMilliseconds; watch.Reset(); var factoryMethod = NotebookFactory.Instance; watch.Start(); for (int i = 0; i < n; i++) { notebook = factoryMethod.CreateComputer("Acer"); } watch.Stop(); times[1] = watch.ElapsedMilliseconds; watch.Reset(); var abstractFactory = EpicFactory.Instance; watch.Start(); for (int i = 0; i < n; i++) { notebook = abstractFactory.CreateNotebook(); } watch.Stop(); times[2] = watch.ElapsedMilliseconds; watch.Reset(); var genericAbstract = new EpicCompany(CreatonFactory.Instance); watch.Start(); for (int i = 0; i < n; i++) { notebook = genericAbstract.CreateNotebook(); } watch.Stop(); times[3] = watch.ElapsedMilliseconds; watch.Reset(); var nonReflection = NonReflectedFactory.Instance; watch.Start(); for (int i = 0; i < n; i++) { notebook = nonReflection.CreateNotebook("Acer"); } watch.Stop(); times[4] = watch.ElapsedMilliseconds; watch.Reset(); nonReflection = NonReflectedFactory.Instance; watch.Start(); for (int i = 0; i < n; i++) { notebook = nonReflection.CreateNotebook <AcerNotebook>(); } watch.Stop(); times[5] = watch.ElapsedMilliseconds; watch.Reset(); var reflection = ReflectedFactory.Instance; reflection.RegisterNotebooks(); watch.Start(); for (int i = 0; i < n; i++) { notebook = reflection.CreateNotebook("Acer"); } watch.Stop(); times[6] = watch.ElapsedMilliseconds; watch.Reset(); Console.WriteLine("Method: \t time"); for (int i = 0; i < times.Length; i++) { Console.WriteLine(methodNames[i] + ":\t " + times[i] + " ms"); } }
static void Main(string[] args) { int choose; int chooseComputer; Motherboard tmpMother; Processor tmpProc; var epicCompany = new EpicCompany(); CompCollection computerList = new CompCollection(); Iterator.Iterator iterator = computerList.CreateIterator(); //INIT LIST computerList[0] = epicCompany.Build(new LowEndBuilder(1, 1, 1, 1, 1, 1, "nVidia", "SSD")); computerList[1] = epicCompany.Build(new LowEndBuilder(1, 2, 1, 2, 2, 3, "AMD", "HDD")); computerList[2] = epicCompany.Build(new LowEndBuilder(3, 2, 2, 2, 1, 3, "nVidia", "SSD")); Console.WriteLine("Witaj w sklepie komputerowym!"); do { Console.WriteLine("Co chcesz zrobić? "); Console.WriteLine("1. Zbuduj nowy komputer!"); Console.WriteLine("2. Pokaż specyfikację komputera."); Console.WriteLine("3. Pokaż wszystkie komputery."); Console.WriteLine("4. Wyjdź."); choose = Convert.ToInt32(Console.ReadLine()); Console.Clear(); switch (choose) { case 1: { mother = ChooseMotherboard(); tmpMother = MotherboardFactory.Instance.TakeMotherboard(mother); proc = ChooseProcessor(mother); tmpProc = ProcessorFactory.Instance.TakeProcessor(tmpMother.Socket, proc); ram = ChooseRam(); ChooseGraphHard(tmpProc); power = ChoosePower(); computerList.Add(epicCompany.Build(new LowEndBuilder(mother, proc, ram, graph, hard, power, "AMD", "SSD"))); //iterator.Step = 2; Console.Clear(); break; } case 2: { Console.WriteLine("Wpisz numer swojego komputera: "); chooseComputer = Convert.ToInt32(Console.ReadLine()) - 1; iterator.Curr = chooseComputer; Console.Clear(); PrintData(iterator.GetCurrent); Console.WriteLine("\nNaciśnij dowolny klawisz..."); Console.ReadKey(); Console.Clear(); break; } case 3: { Console.WriteLine("Lista wszystkich komputerów.\n\n"); for (Computer computer = iterator.First(); !iterator.IsDone; computer = iterator.Next()) { Console.WriteLine($"\n#{iterator.Curr + 1}\n"); PrintData(computer); } Console.WriteLine("\nNaciśnij dowolny klawisz..."); Console.ReadKey(); Console.Clear(); break; } default: break; } } while (choose != 4); }