Creational Design Pattern - Factory
Design Pattern
10 Articles
In this article, let's learn about Factory Design Pattern in .NET.
Table of Contents
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:
- Creator: Declares the factory method that returns an object of type Product.
- Concrete Creator: Implements the factory method, returning an instance of a concrete product.
- Product: An abstract class or interface defining the object to be created.
- 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
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:
- When a class can't anticipate the class of objects it must create.
- When a class wants its subclasses to specify the objects it creates.
- When object creation logic might be complex, and you'd like to encapsulate it in one location.
- 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.
Related Patterns
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.