👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Creational Design Pattern - Factory

Creational Design Pattern - Factory

Design Pattern

10 Articles

Improve

In this article, let's learn about Factory Design Pattern in .NET.

Table of Contents

  1. Introduction
  2. Structure
  3. Use Cases
  4. Advantages
  5. Disadvantages
  6. Related Patterns
  7. Summary

Introduction

The Factory Method is a creational pattern from the famous Gang of Four (GoF) design patterns. Creational patterns focus on object creation, and the factory method is a common approach. Its purpose is to define an interface for object creation while allowing subclasses to determine the actual type of object to instantiate.

In essence, the Factory Method allows a class to defer instantiation to subclasses, preventing tight coupling between the client and the specific implementations.

Structure

The Factory pattern involves several key components:

  1. Creator: Declares the factory method that returns an object of type Product.
  2. Concrete Creator: Implements the factory method, returning an instance of a concrete product.
  3. Product: An abstract class or interface defining the object to be created.
  4. Concrete Product: The actual implementation of the product.

Imagine a shopping cart system where different discounts apply based on the user's country or discount code. Instead of the client tightly coupling to different discount services (e.g., CountryDiscountService or CodeDiscountService), we use the Factory Method pattern to abstract the creation process.

Code Sample - Factory Pattern Code Sample

Demo - Factory Pattern Demo

Let's try Factory Demo, Click on the Create Country Discount Service button and Create Code Discount Service button to see the factory pattern in action. The results will be displayed at the bottom.

Code Sample - Factory Pattern Demo

Total Amount: 0

From the above demo, you can see that how object creation is isolated from the client code. The client only interacts with the abstraction. In real world above Factory Pattern can be used to create different types of objects based on the user country claims or code entered by user to choose the right service to be used in shopping cart.

Use Cases

The Factory pattern is beneficial in several scenarios:

  1. When a class can't anticipate the class of objects it must create.
  2. When a class wants its subclasses to specify the objects it creates.
  3. When object creation logic might be complex, and you'd like to encapsulate it in one location.
  4. To avoid tight coupling between client code and specific classes.

Advantages

  • Loosely couples the client from concrete classes by working with interfaces or abstract classes.
  • Follows the Open/Closed Principle, allowing new product types without modifying existing code.
  • Supports the Single Responsibility Principle by centralizing object creation.

Disadvantages

  • If you only need one subclass, using the Factory Method might introduce unnecessary complexity.

The Factory pattern is closely related to several other design patterns:

  • Abstract Factory: Builds on the factory method to create families of related objects.
  • Prototype: Involves cloning objects rather than subclassing for creation.
  • Template Method: Often used in conjunction with Factory Method to handle the logic within the method.

Summary

In this article, we explored the Factory Method pattern, learning how it abstracts object creation by deferring it to subclasses. In our example, we built a discount system where the factory method determines which discount service to instantiate. This pattern prevents tight coupling and adheres to key design principles like Open/Closed and Single Responsibility.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Design Pattern
  • Creational
  • Factory