Implementing Factory Method in C#






4.62/5 (11 votes)
Decoupling creation of objects from the client
Factory Method
Factory method is a widely used mechanism for creating instances of classes in any object oriented programming language. The Factory method abstracts the creation of objects from the consumer. It also provides a single place where objects can be created. I supplied the desired objects hiding the complexity of creation for the consumers.Advantages of Factory Method
- Hides the complexity of creation from the consumer.
- Ensures the object creation logic to be in a single place
- Helps customise creation without disturbing the consumer logic
- Brings in logical separation between creation and usage
CarFactory
example to demonstrate the same. The Factory manufactures swift cars of 2 types basic and featured with 3 different colors black, blue and red.
Steps for implementing the Factory Method
- Create an
abstract
class Swift Car with an attribute color and a methodCaliculatePrice
as the price differs for different models:/// <summary> /// abstract class for swift car /// </summary> public abstract class SwiftCar { /// <summary> /// color of the Swift car /// </summary> public CarColor Color { get; private set; } protected SwiftCar(CarColor color) { this.Color = color; } /// <summary> /// function that calculates the price /// of the swift car /// </summary> /// <returns></returns> public abstract float CaliculatePrice( ); }
- Create 2 derived classes For Basic Swift Car and Featured Swift Car:
/// <summary> /// the basic swift car /// </summary> public class SwiftCarBasic:SwiftCar { /// <summary> /// creates a basic swift car of defined color /// </summary> /// <param name="color">the color requested by the client</param> public SwiftCarBasic(CarColor color):base(color ) { } /// <summary> /// customised calculation of price /// </summary> /// <returns> the price of the basic model of swift car</returns> public override float CaliculatePrice() { float BasicPrice = 400000F; return BasicPrice; } } /// <summary> /// The swift car with additional features /// </summary> public class SwiftCarFeatured : SwiftCar { /// <summary> /// Creates a Featured swift car with a color requested by the client /// </summary> /// <param name="Color">the color of the featured swift car</param> public SwiftCarFeatured(CarColor Color):base(Color ) { } /// <summary> /// customised calculation of the car with additional features such as airbags,speakers power window etc /// </summary> /// <returns></returns> public override float CaliculatePrice() { float basicPrice = 400000F; float otherEquipmentCosts= 200000F; return basicPrice +otherEquipmentCosts ; } }
- Define Enumerations which describe the
Car
types andCar
colors/// <summary> /// The different types of Swift Car /// </summary> public enum SwiftCarType { /// <summary> /// basic model of Swift car /// </summary> BASIC, /// <summary> /// Advanced model of swift car /// </summary> FEATURED } /// <summary> /// the car color types /// </summary> public enum CarColor { BLACK, RED, BLUE }
- Create a
static
class with astatic
method which returns theCar
of desired Type. Note: It is very important to keep theabstract
base classSwift car
as a Return Type./// <summary> /// The Class represents the Factory of the Swift Car /// </summary> public static class SwiftCarFactory { /// <summary> /// The method creates a swift car as desired by the consumer /// </summary> /// <param name="carType"> the car type specified by the Consumer</param> /// <param name="carColor"> the color of the car specified by the consumer </param> /// <returns> the desired swift car</returns> public static SwiftCar CreateSwiftCar(SwiftCarType carType, CarColor carColor) { SwiftCar car = null; switch (carType) { case SwiftCarType.BASIC: car = new SwiftCarBasic(carColor); break; case SwiftCarType.FEATURED: car = new SwiftCarFeatured(carColor); break; } return car; } }
- Design a Client to consume the
Car
from the Swift Car Factory./// <summary> /// Client to Consume a Swift Car from the Swift Car Factory /// </summary> class ClientClass { public static void Main(string[] args) { //the basic Swift car of Blue color -Creation SwiftCar BlueBasicSwiftCar = SwiftCarFactory.CreateSwiftCar(SwiftCarType.BASIC, CarColor.BLUE); //Computation of Price float BlueBasicSwiftCarPrice = BlueBasicSwiftCar.CaliculatePrice(); Console.WriteLine("price of Blue Basic Swift car" + BlueBasicSwiftCarPrice); //the Red Featured Swift car of Blue color -Creation SwiftCar RedFeaturedSwiftCar = SwiftCarFactory.CreateSwiftCar(SwiftCarType.FEATURED, CarColor.RED); // Computation of Price float RedFeaturedSwiftCarPrice = RedFeaturedSwiftCar.CaliculatePrice(); Console.WriteLine("price of Red Featured Swift car" + RedFeaturedSwiftCarPrice ); } }