コード例 #1
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);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: shawcoder/DotNetNewbies
        /*
         *      Implementation details:
         *      - Start with interface
         *      - Add "functionality" using virtual properties in the abstract class
         *
         *      - Downside of Abstract Factory pattern:
         *      - If new functionality is needed (e.g. Trailer Hitch parts), then that
         *              functionality must be added using the same (pardon the pun) pattern
         *              e.g. Add the interface, then add the abstract class, then add the
         *              functionality where necessary by modifying the appropriate factory
         *              method.
         */
        static void Main()
        {
            WhatToMake             WHAT_VEHICLE_TO_MAKE = WhatToMake.Car;
            AbstractVehicleFactory vFactory;

            switch (WHAT_VEHICLE_TO_MAKE)
            {
            case WhatToMake.Car:
            {
                vFactory = new CarFactory();
                break;
            }

            case WhatToMake.Van:
            {
                vFactory = new VanFactory();
                break;
            }

            default:
            {
                throw new Exception("Unhandled vehicle type selected!");
            }
            }
            IBody      vBody      = vFactory?.CreateBody();
            IChassis   vChassis   = vFactory?.CreateChassis();
            IGlassware vGlassware = vFactory?.CreateGlassware();

            WriteLine(vBody.BodyParts);
            WriteLine(vChassis.ChassisParts);
            WriteLine(vGlassware.GlasswareParts);
            ReadKey();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: shawcoder/DotNetNewbies
        static void Main()
        {
            // Econical car, blue
            VehicleFactory vCarFactory = new CarFactory();
            IVehicle       vCar        = vCarFactory.Build
                                             (VehicleFactory.DrivingStyle.Economical, VehicleColour.Blue);

            WriteLine(vCar);

            // White van
            VehicleFactory vVanFactory = new VanFactory();
            IVehicle       vVan        = vVanFactory.Build
                                             (VehicleFactory.DrivingStyle.Powerful, VehicleColour.White);

            WriteLine(vVan);

            // Red Sports car using static factory
            IVehicle vSportsVehicle =
                VehicleFactory.Make
                (
                    VehicleFactory.BuildWhat.Car
                    , VehicleFactory.DrivingStyle.Powerful
                    , VehicleColour.Red
                );

            WriteLine(vSportsVehicle);

            ReadKey();
        }
コード例 #4
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();
        }
コード例 #5
0
        public Sample()
        {
            VehicleFactory carFactory = new CarFactory();

            carFactory.Build(VehicleFactory.DrivingStyle.Economical, VehicleColour.Blue);

            VehicleFactory vanFactory = new VanFactory();

            vanFactory.Build(VehicleFactory.DrivingStyle.Economical, VehicleColour.Silver);
        }
コード例 #6
0
        public void VanFactoryMethodTest()
        {
            //given
            VehicleFactory vanFactory = new VanFactory();

            //When
            IVehicle van = vanFactory.Build(VehicleFactory.DrivingStyle.Economical, VehicleColour.Blue);

            //then
            Assert.AreEqual(van.GetType(), typeof(Pickup));
        }
コード例 #7
0
        public void VanFactoryTests()
        {
            AbstractVehicleFactory factory = new VanFactory();

            IBody      vehicleBody      = factory.CreateBody();
            IChassis   vehicleChassis   = factory.CreateChassis();
            IGlassware vehicleGlassware = factory.CreateGlassware();

            Assert.AreEqual(expected: "Body shell parts for a van", actual: vehicleBody.BodyParts);
            Assert.AreEqual(expected: "Chassis parts for a van", actual: vehicleChassis.ChassisParts);
            Assert.AreEqual(expected: "Window glassware for a van", actual: vehicleGlassware.GlasswareParts);
        }
コード例 #8
0
        public void VanFactoryTest()
        {
            //given
            AbstractVehicleFactory factory = new VanFactory();

            //when
            IBody      vehicleBody      = factory.CreateBody();
            IChassis   vehicleChasis    = factory.CreateChassis();
            IGlassware vehicleGlassware = factory.CreateGlassware();

            //then
            Assert.AreEqual(vehicleBody.GetType(), typeof(VanBody));
            Assert.AreEqual(vehicleChasis.GetType(), typeof(VanChassis));
            Assert.AreEqual(vehicleGlassware.GetType(), typeof(VanGlassware));
        }
コード例 #9
0
        public void Run(string vehicleType)
        {
            AbstractVehicleFactory vehicleFactory;

            if (vehicleType.ToLower() == "car")
            {
                vehicleFactory = new CarFactory();
            }
            else
            {
                vehicleFactory = new VanFactory();
            }

            var parts = vehicleFactory.GetParts;
            var body  = vehicleFactory.GetBody;
        }
コード例 #10
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);
        }
コード例 #11
0
      // Using Static factory
      public static IVehicle Make(Category cat,
                                  DrivingStyle style,
                                  VehicleColour colour)
      {
          VehicleFactory factory;

          if (cat == Category.Car)
          {
              factory = new CarFactory();
          }
          else
          {
              factory = new VanFactory();
          }

          return(factory.Build(style, colour));
      }
コード例 #12
0
ファイル: AbstractFactory.cs プロジェクト: walrus7521/code
        public static void Main()
        {
            // specify the platform here: "mac", "windows", "android"
            string whatToMake = "car"; // "van"

            AbstractVehicleFactory factory = null;

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

            // Create the vehicle's component parts ...
            // These will either be all car parts or all van parts
            // These can be widgets of different platforms - Mac, Windows, Android
            IBody vehicleBody = factory.CreateBody();
            IChassis vehicleChassis = factory.CreateChassis();
            IGlassware vehicleGlassware = factory.CreateGlassware();

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

            vehicleBody.Draw();
        }
コード例 #13
0
ファイル: Factory.cs プロジェクト: walrus7521/code
        public static void Main()
        {
            // I want an economical car, coloured blue
            VehicleFactory carFactory = new CarFactory();
            IVehicle car = carFactory.Build(
                                VehicleFactory.DrivingStyle.Economical,
                                VehicleColour.Blue);
            Console.WriteLine(car);

            // I want a "white van"
            VehicleFactory vanFactory = new VanFactory();
            IVehicle van = vanFactory.Build(
                                VehicleFactory.DrivingStyle.Powerful,
                                VehicleColour.White);
            Console.WriteLine(van);

            // use the static Make method to create a red sports car
            IVehicle sporty = VehicleFactory.Make(
                                VehicleFactory.Category.Car,
                                VehicleFactory.DrivingStyle.Powerful,
                                VehicleColour.Red);
            Console.WriteLine(sporty);
        }
コード例 #14
0
ファイル: Factory.cs プロジェクト: walrus7521/code
 public static IVehicle Make(Category cat,
                             DrivingStyle style,
                             VehicleColour colour)
 {
     VehicleFactory factory;
     if (cat == Category.Car)
     {
         factory = new CarFactory();
     }
     else
     {
         factory = new VanFactory();
     }
     return factory.Build(style, colour);
 }