Example #1
0
        public void Get_Returns_Car_Factory()
        {
            Vehicle vehicle = new Vehicle();
            AbstractVehicleFactory vehicleFactory = vehicle.Get(VehicleType.Car);

            Assert.IsInstanceOfType(vehicleFactory, typeof(CarFactory));
        }
Example #2
0
        public Client()
        {
            var whatToMake = "car";
            AbstractVehicleFactory factory = null;

            if (whatToMake.Equals("car"))
            {
                factory = new CarFactory();
            }

            if (whatToMake.Equals("van"))
            {
                factory = new VanFactory();
            }

            if (factory == null)
            {
                return;
            }

            var vehicleBody      = factory.CreateBody();
            var vehicleChassis   = factory.CreateChassis();
            var vehicleGlassware = factory.CreateGlassware();

            Console.WriteLine(vehicleBody.BodyParts);
            Console.WriteLine(vehicleChassis.ChassisParts);
            Console.WriteLine(vehicleGlassware.GlasswareParts);
        }
Example #3
0
        static void Main(string[] args)
        {
            string whatToMake = "van"; // or "van"
            AbstractVehicleFactory factory = null;

            // Create the correct 'factory'...
            if (whatToMake.Equals("car"))
            {
                factory = new CarFactory();
            }
            else
            {
                factory = new VanFactory();
            }

            // Create the vehicle parts, either a car or a van...
            IBody      vehicleBody      = factory.CreateBody();
            IChassis   vehicleChassis   = factory.CreateChassis();
            IGlassware vehicleGlassware = factory.CreateGlassware();

            // See what we've created...
            Console.WriteLine(vehicleBody.BodyParts);
            Console.WriteLine(vehicleChassis.ChassisParts);
            Console.WriteLine(vehicleGlassware.GlasswareParts);

            Console.Read();
        }
Example #4
0
        public void Get_Returns_Body_Shell_For_Car()
        {
            Vehicle vehicle = new Vehicle();
            AbstractVehicleFactory vehicleFactory = vehicle.Get(VehicleType.Car);
            IChassis vehicleChassis = vehicleFactory.CreateChassis();

            Assert.AreEqual <string>(vehicleChassis.ChassisParts, "Chassis parts for a car");
        }
Example #5
0
        //Factory Pattern
        public AbstractVehicleFactory Get(VehicleType vehicleType)
        {
            AbstractVehicleFactory vehicleFactory = null;

            switch (vehicleType)
            {
            case VehicleType.Van:
                vehicleFactory = new VanFactory();
                break;

            default:
                vehicleFactory = new CarFactory();
                break;
            }

            return(vehicleFactory);
        }
        /// <summary>
        /// Demonstrates abstract/concrete method
        /// </summary>
        private static void AbstractFactoryExample()
        {
            Print("Abstract Factory Example");
            string divider = new string('-', 50);

            AbstractVehicleFactory[] factories = new AbstractVehicleFactory[3];
            factories[0] = new HangerFactory();
            factories[1] = new HarbourFactory();
            factories[2] = new ShowroomFactory();

            // Iterate through each factory
            foreach (var factory in factories)
            {
                System.Console.WriteLine(divider);
                System.Console.WriteLine(factory.GetType().Name);
                System.Console.WriteLine(divider);
                // Iterate through each vehicle in the factory
                foreach (var vehicle in factory.Vehicles)
                {
                    System.Console.WriteLine($"\t{vehicle.GetType().Name}");
                }
                System.Console.WriteLine();
            }
        }
 public void BuildBody(AbstractVehicleFactory factory)
 {
     Vehicle.VehicleBody = factory.GetBody();
 }
 public void BuildTransmition(AbstractVehicleFactory factory)
 {
     Vehicle.VehicleTransmition = factory.GetTransmition();
 }
 public void BuildEngine(AbstractVehicleFactory factory)
 {
     Vehicle.VehicleEngine = factory.GetEngine();
 }