Design Pattern — Factory Pattern

What are Design Patterns?

Design Patterns are solutions to common problems.

Types of Design Patterns:

  1. Creational =>deals with object creation while hiding the creation logic.
  2. Structural => deals with class and object composition.
  3. Behavioral => deals with communication between objects.

Factory Pattern

The Factory Pattern falls into the “Creational” category. It allows the creation of a given subtype through a Factory class. Its main advantages are that it decouples the client from the actual object creation and encapsulates object creation.
  • ConcreteProduct: Actual implementation of IProduct. Created through ConcreteFactory.
  • AbstractFactory: Optional component that defines the default behaviour for ConcreteFactory.
  • ConcreteFactory: Used for the creation of ConcreteProduct. It can inherit from AbstractFactory.
  • ConcreteFactory locates the implementation of ProductA and creates a new instance of it.
  • ConcreteFactory returns ProductA.
  • Decouples client from the actual object creation.
  • Encapsules object creation.


In this example let us say we have a bricks Factory with two types of bricks: “A” and “B”.

  • ConcreteProduct =/Products/BrickA.cs & /Products/BrickB.cs
  • AbstractFactory = /AbstractFactory/BrickFactory.cs
  • ConcreteFactory = /MyFactory/Factory.cs
PS C:\Users\azureuser\source\repos\FactoryPatternBricksExampleSolution\FactoryPatternBricksExample> tree /f
│ FactoryPatternBricksExample.csproj
│ Program.cs

│ BrickFactory.cs

│ Factory.cs

│ IBricks.cs

  • GetWeightInKG( return an int with the weight of the brick).
  • GetColor(return a string with the color of the brick).
  • GetWeightInKG() => return 3.
  • GetColor() => return “Red”.
  • GetWeightInKG() => return 4.
  • GetColor() => return “Blue”.
Type: BrickA.
Product: A.
Weight:3 Kg.
Color: Red.
Type: BrickB.
Product: B.
Weight: 4 Kg.
Color: Blue.


  • There are two products that inherit from an Interface.
  • The interface defines the based structure for the products.
  • There is a ConcreteFactory that inherits from AbstractFactory.
  • AbstractFactory defines the behaviour of ConcreteFactory.
  • ConcreteFactory is responsible for object creation.
  • ConcreteFactory sits between the client and the actual object.
  • Client never interacts directly with the actual object.
  • Factory Pattern is a creational pattern because it deals with object creation.
  • It encapsules object creation.
  • Use it when classes need to be created at runtime and all classes are in the same subclass hierarchy.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store