Exemplo n.º 1
0
        public void CreateShipWrong()
        {
            MethodFactory factory = EarthFactory.Instance;
            var           ship    = factory.BuildFrigate("aeria");

            Assert.IsNull(ship);
        }
Exemplo n.º 2
0
        public void CreateShipEmpty()
        {
            MethodFactory factory = EarthFactory.Instance;
            var           ship    = factory.BuildFrigate(null);

            Assert.IsNull(ship);
        }
Exemplo n.º 3
0
        public void CreateShip()
        {
            MethodFactory factory = EarthFactory.Instance;
            var           ship    = factory.BuildFrigate("disciple");

            Assert.IsNotNull(ship);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            int n = 1000000; // 1 mln

            double[] times = new double[5];
            TimeSpan roznica;
            DateTime startTime, stopTime;

            var   simpleFactory = SimpleFactory.Instance;
            Ships ship;

            startTime = DateTime.Now;
            for (int i = 0; i < n; i++)
            {
                ship = simpleFactory.CreateFrigate("cobalt");
            }
            stopTime = DateTime.Now;
            roznica  = stopTime - startTime;
            times[0] = roznica.TotalMilliseconds;

            //------------------------------------------------------------------------------

            startTime = DateTime.Now;
            MethodFactory methodFactory = EarthFactory.Instance;

            for (int i = 0; i < n; i++)
            {
                ship = methodFactory.BuildFrigate("cobalt") as Ships;
            }
            stopTime = DateTime.Now;
            roznica  = stopTime - startTime;
            times[1] = roznica.TotalMilliseconds;

            //------------------------------------------------------------------------------

            AbstractFactory abstractFactory = new Abstract1(EarthAbstractFactory.Instance);

            startTime = DateTime.Now;
            for (int i = 0; i < n; i++)
            {
                ship = abstractFactory.CreateFrigate() as Ships;
            }
            stopTime = DateTime.Now;
            roznica  = stopTime - startTime;
            times[2] = roznica.TotalMilliseconds;

            //------------------------------------------------------------------------------


            RegistrationFactory registrationFactory = RegistrationFactory.Instance;

            registrationFactory.BuildShip("cobalt", typeof(Frigate));
            startTime = DateTime.Now;
            for (int i = 0; i < n; i++)
            {
                ship = abstractFactory.CreateFrigate() as Ships;
            }
            stopTime = DateTime.Now;
            roznica  = stopTime - startTime;
            times[3] = roznica.TotalMilliseconds;

            //------------------------------------------------------------------------------

            RegistrationFactoryWithReflection reflectionFactory = RegistrationFactoryWithReflection.Instance;

            reflectionFactory.BuildShip();
            startTime = DateTime.Now;
            for (int i = 0; i < n; i++)
            {
                ship = abstractFactory.CreateFrigate() as Ships;
            }
            stopTime = DateTime.Now;
            roznica  = stopTime - startTime;
            times[4] = roznica.TotalMilliseconds;


            Console.WriteLine("Porównanie czasu tworzenia obiektów przez fabryki:");
            Console.WriteLine("Simple Factory: " + times[0] + " ms");
            Console.WriteLine("Factory Method: " + times[1] + " ms");
            Console.WriteLine("Abstract Factory: " + times[2] + " ms");
            Console.WriteLine("\nZ rejertracją klas:");
            Console.WriteLine("NoReflection: " + times[3] + " ms");
            Console.WriteLine("Reflection: " + times[4] + " ms");
            Console.ReadKey();
        }