コード例 #1
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();
        }
コード例 #2
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);
        }
コード例 #3
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));
        }