Exploring the Factory Method Design Pattern

Exploring the Factory Method Design Pattern

The Factory Method is a design pattern that falls under the category of creational design patterns. It provides a way to create objects without specifying their exact class. Instead of directly calling a constructor to create an object, the Factory Method pattern defines an interface or an abstract class for creating objects, and lets the subclasses decide which class to instantiate.


In simpler terms, the Factory Method pattern encapsulates the object creation logic and delegates the responsibility of creating objects to its subclasses. It provides a common interface for creating objects, but the actual class of the object is determined by the subclass implementation.


Here's a step-by-step breakdown of the Factory Method pattern:

  1. Define an interface or an abstract class: This is the common interface or abstract class that declares the factory method. It provides a contract for creating objects.
  2. Create concrete implementations: Subclasses of the interface or abstract class are created to provide specific implementations of the factory method. Each subclass can decide which class to instantiate.
  3. Implement the factory method: The factory method is implemented in each subclass, and it's responsible for creating objects of a specific class.
  4. Use the factory method: Instead of directly calling a constructor to create objects, the client code calls the factory method defined in the interface or abstract class. The factory method, in turn, creates the appropriate object based on the subclass implementation.


By using the Factory Method pattern, you can achieve loose coupling between the client code and the specific classes being instantiated. It allows for flexibility and extensibility, as new subclasses can be added without modifying the client code. Additionally, it promotes the Single Responsibility Principle by separating the object creation logic from the client's code.


Here's an example code snippet in C# to illustrate the concept:


public interface SubscriptionPlan
{
    public void DisplaySelectedPlan();
}


public class PlatinumPlan : SubscriptionPlan
{
    public void DisplaySelectedPlan()
    {
        Console.WriteLine("Platinum plan is selected");
    }
}


public class DiamondPlan : SubscriptionPlan
{
    public void DisplaySelectedPlan()
    {
        Console.WriteLine("Diamond plan is selected");
    }
}


public class GoldenPlan : SubscriptionPlan
{
    public void DisplaySelectedPlan()
    {
        Console.WriteLine("Golden plan is selected");
    }
}


public class SubscriptionPlanFactory
{
    public static SubscriptionPlan SelectPlan(string type)
    {
        switch (type)
        {
            case "patinum":
                return new PlatinumPlan();
            case "diamond":
                return new DiamondPlan();
            case "golden":
                return new GoldenPlan();
            default:
                throw new ArgumentException("Invalid plan type.");
        }
    }
}


public class Program
{
    public static void Main()
    {
        SubscriptionPlan subscription = SubscriptionPlanFactory.SelectPlan("patinum");
        subscription.DisplaySelectedPlan();
    }
}        


The Factory Method pattern offers several benefits, including:

  1. Encapsulation: The Factory Method pattern encapsulates the object creation process within the factory method, hiding the creation logic from the client code. This promotes encapsulation and information hiding principles, as the client doesn't need to know the specific implementation details of the created objects.
  2. Loose coupling: The pattern promotes loose coupling between the client code and the created objects. The client code only depends on the abstract factory interface or base class, rather than directly depending on concrete classes. This allows for easier maintenance and future extensibility, as new subclasses can be added without affecting the client code.
  3. Flexibility and extensibility: With the Factory Method pattern, it's straightforward to introduce new product classes by creating a new subclass of the factory interface or base class. This allows for easy expansion of the product family without modifying existing code. The pattern also enables runtime determination of the concrete class to instantiate, providing flexibility and adaptability.
  4. Code reuse: The Factory Method pattern promotes code reuse by centralizing the object creation logic in a single location. The creation logic can be shared among multiple subclasses, avoiding code duplication and ensuring consistency in the creation process.
  5. Dependency inversion: By depending on the abstract factory interface or base class rather than concrete classes, the Factory Method pattern follows the Dependency Inversion Principle. It allows for easy substitution of different concrete factories, facilitating the use of different implementations without affecting the client code.
  6. Testing and mocking: The Factory Method pattern simplifies unit testing and mocking. Since the client code depends on an abstract factory interface or base class, it becomes easier to substitute mock factories during testing, enabling isolated testing of client code.

Overall, the Factory Method pattern provides a structured approach to object creation, promoting modularity, flexibility, and maintainability in software design.

To view or add a comment, sign in

More articles by Md Arifuzzaman Tanin

Others also viewed

Explore content categories