/* * 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(); }
static void Main(string[] args) { // the client can instantiate the appropriate // 'factory' with which it can obtain the correct // parts, whether for car or van string whatToMake = "car"; AbstractVehicleFactory factory = null; // create the correct factory if (whatToMake.Equals("car")) { factory = new CarFactory(); } else if (whatToMake.Equals("van")) { factory = new VanFactory(); } // create the vehicle component parts... IBody body = factory.CreateBody(); IChassis chassis = factory.CreateChassis(); IGlassware glass = factory.CreateGlassware(); // see what we have created Console.WriteLine(body.BodyParts); Console.WriteLine(chassis.ChassisParts); Console.WriteLine(glass.GlasswareParts); Console.ReadLine(); // disadvantage: if adding a new product ex. Lights, to include lights in family of components // would need to amend AbstractVehicleFactory, CarFactory, VanFactory // in addition new ILights (CarLights/Vanlights) will need to be created }
/// <summary> /// 'Client' Application /// </summary> /// <param name="args">The arguments.</param> static void Main(string[] args) { string whatToMake = "car"; // or "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 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); Console.Read(); }
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(); }
public void CheckQuality(IGlassware pieceOfGlassware) { Console.WriteLine($"{pieceOfGlassware.ToString()} is {pieceOfGlassware.Condition} !"); if (pieceOfGlassware.Quality <= 20) { QualityControlFailed = true; } }
public void Initialize() { fakeSupplier = new FakeSupplier(); storage = new Storage(fakeSupplier); glass = new TestGlass() { ItemID = 0 }; }
public void ChangeQuality(IGlassware glasswareObject) { Random rand = new Random(); int randomValue = rand.Next(0, 100); if (glasswareObject.Quality > randomValue) { glasswareObject.Quality = randomValue; } }
public void Wash(IGlassware glassware) { if (!glassware.IsClean) { Console.WriteLine($"I'm starting to wash Your {glassware.ToString()}"); SimulateWashingProcess(); Console.WriteLine($"Your item {glassware.ToString()} is clean"); glassware.IsClean = true; } }
public bool CheckIfItemIsClean(IGlassware pieceOfGlassware) { if (pieceOfGlassware.IsClean) { return(true); } else { return(false); } }
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); }
public string DeleteItem(IGlassware item) { var productToDelete = storageItems.FirstOrDefault(i => i == item); if (productToDelete != null) { storageItems.Remove(productToDelete); return($"Item {productToDelete.ToString()} thrown out"); } else { return("Sorry,there's no product with this ID in Your storage"); } }
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)); }
public void ChangeCondition(IGlassware glassware) { Console.WriteLine(); if (glassware.Quality == 100) { glassware.Condition = "New"; } else if (glassware.Quality > 75) { glassware.Condition = "Good"; } else if (glassware.Quality > 25) { glassware.Condition = "Damaged"; Console.WriteLine($"Your {glassware.ToString()} is now damaged, but You can still use it"); } else if (glassware.Quality <= 20) { glassware.Condition = "Broken"; Console.WriteLine($"You've broken {glassware.ToString()} !"); } }
static void Main(string[] args) { string whatToMake = "car"; AbstractVehicleFactory factory = null; if (whatToMake.Equals("car")) { factory = new CarFactory(); } else { factory = new VanFactory(); } IBody vehicleBody = factory.CreateBody(); IChassis vehicleChassis = factory.CreateChassis(); IGlassware vehicleGlassware = factory.CreateGlassware(); Console.WriteLine(vehicleBody.BodyParts); Console.WriteLine(vehicleChassis.ChassisParts); Console.WriteLine(vehicleGlassware.GlasswarePart); Console.ReadKey(); }
public bool CheckIfItemIsClean(IGlassware pieceOfGlassware) { return(washer.CheckIfItemIsClean(pieceOfGlassware)); }
public void PolishItem(IGlassware pieceOfGlassware) { maintainer.Polish(pieceOfGlassware); }
public void WashItem(IGlassware pieceOfGlassware) { maintainer.Wash(pieceOfGlassware); }
public void Polish(IGlassware pieceOfGlassware) { Console.WriteLine($"Polishing {pieceOfGlassware.ToString()}..."); Thread.Sleep(300); Console.WriteLine($"{pieceOfGlassware.ToString()} is now clean and shiny!"); }
public void AddItem(IGlassware piece) { storageItems.Add(piece); }
public void Wash(IGlassware pieceOfGlassware) { washer.Wash(pieceOfGlassware); }
public void Polish(IGlassware pieceOfGlassware) { polisher.Polish(pieceOfGlassware); }