static void Main(string[] args)
        {
            Console.WriteLine("***Flyweight Pattern Demo.***\n");
            VehicleFactory vehiclefactory = new VehicleFactory();
            IVehicle       vehicle;

            /*
             * Now we are trying to get the 3 cars.Note that:we need not
             * create additional cars if we have already created one of this category.
             */
            for (int i = 0; i < 3; i++)
            {
                vehicle = vehiclefactory.GetVehicleFromVehicleFactory("car");
                vehicle.AboutMe(GetRandomColor());
            }
            int numOfDistinctRobots = vehiclefactory.TotalObjectsCreated;

            Console.WriteLine($"\n Now, total numbers of distinct vehicle object(s) is = {numOfDistinctRobots}\n");

            /*
             * Here we are trying to get the 5 more buses.Note that: we need not create
             * additional buses if we have already created one of this category.
             */
            for (int i = 0; i < 5; i++)
            {
                vehicle = vehiclefactory.GetVehicleFromVehicleFactory("bus");
                vehicle.AboutMe(GetRandomColor());
            }
            numOfDistinctRobots = vehiclefactory.TotalObjectsCreated;
            Console.WriteLine($"\n Now, total numbers of distinct vehicle object(s) is = {numOfDistinctRobots}\n");

            /*
             * Here we are trying to get the 2 future vehicles.Note that: we need not create
             * additional future vehicle if we have already created one of this category.
             */
            for (int i = 0; i < 2; i++)
            {
                vehicle = vehiclefactory.GetVehicleFromVehicleFactory("future");
                vehicle.AboutMe(GetRandomColor());
            }
            numOfDistinctRobots = vehiclefactory.TotalObjectsCreated;
            Console.WriteLine($"\n Now, total numbers of distinct vehicle object(s) is = {numOfDistinctRobots}\n");


            Console.ReadKey();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Flyweight Pattern");
            VehicleFactory vehicleFactory = new VehicleFactory();
            IVehicle       vehicle;

            //now we are trying to get the 3 cars. Note that: we don't need to create additional cars
            // if we have already created one of this category

            for (int i = 0; i < 3; i++)
            {
                vehicle = vehicleFactory.GetVehicleFromVehicleFactory("car");
                vehicle.AboutMe(GetRandomColor());
            }

            int numberOfDistinctVehicles = vehicleFactory.TotalObjectsCreated;

            Console.WriteLine($"Now, total numbers of distinct vehicle object(s) is: {numberOfDistinctVehicles}");

            //5 more buses
            for (int i = 0; i < 5; i++)
            {
                vehicle = vehicleFactory.GetVehicleFromVehicleFactory("bus");
                vehicle.AboutMe(GetRandomColor());
            }
            numberOfDistinctVehicles = vehicleFactory.TotalObjectsCreated;
            Console.WriteLine($"Now, total numbers of distinct vehicle object(s) is: {numberOfDistinctVehicles}");

            //2 future vehicles
            for (int i = 0; i < 2; i++)
            {
                vehicle = vehicleFactory.GetVehicleFromVehicleFactory("future");
                vehicle.AboutMe(GetRandomColor());
            }

            numberOfDistinctVehicles = vehicleFactory.TotalObjectsCreated;
            Console.WriteLine($"Now, total numbers of distinct vehicle object(s) is: {numberOfDistinctVehicles}");

            Console.Read();
        }