Factory Pattern: A Comprehensive Guide

In this post, we’ll explore the different variations of the Factory Pattern(Simple Factory, Factory Method, and Abstract Factory), understand when to use each one, and see practical examples of how they help us write more modular and scalable code.

Factory Pattern: A Comprehensive Guide

Getting Started

In software development, one of the most important goals is keeping code flexible, maintainable, and easy to extend. Yet as applications grow more complex, the process of creating objects often becomes tightly coupled to concrete classes, making the system harder to modify over time. This is where creational design patterns come into play and among them, the Factory Pattern or Factory Design Patternstands out as one of the most powerful and widely used.

The Factory Pattern provides a clean way to delegate object creation to a dedicated component, allowing developers to focus on what needs to be created rather than how it is created. By encapsulating the instantiation logic, the pattern not only reduces dependencies but also makes it easier to introduce new object types without changing existing code. From GUI frameworks to logging systems and database connectors, factories appear everywhere in modern software engineering.

What is Factory Pattern?

The Factory Pattern is a creational design pattern used to create objects without exposing the creation logic to the client. Instead of instantiating classes directly with new, you delegate the creation to a factory.

It helps when:
  • You want to decouple object creation from usage.
  • You need to create objects from a family of related types.
  • The exact type to instantiate may vary based on input or configuration.

Simple Factory Pattern C#
class ShapeFactory {
  public Shape getShape(String type) {
    switch(type) {
      case "circle": return new Circle();
      case "square": return new Square();
      default: throw new IllegalArgumentException("Unknown type");
    }
  }
}
interface Shape { void draw(); }
class Circle implements Shape {
public void draw() { System.out.println("Circle"); }
}
class Square implements Shape {
public void draw() { System.out.println("Square"); }
}

Note:- Not a “realGoF pattern but widely used.

Usage:
ShapeFactory factory = new ShapeFactory();
Shape s = factory.getShape("circle");
s.draw();

Factory Method Pattern (GoF)

Define an interface for creating objects, but let subclasses decide which class to instantiate.

Example
abstract class Dialog {
  public void render() {
    Button okButton = createButton();
    okButton.render();
  }
  protected abstract Button createButton(); // Factory Method
}
class WindowsDialog extends Dialog {
  @Override
  protected Button createButton() {
    return new WindowsButton();
  }
}
class WebDialog extends Dialog {
  @Override
  protected Button createButton() {
    return new HTMLButton();
  }
}

Abstract Factory Pattern

Creates families of related objects without specifying their concrete classes.

Example
interface GUIFactory {
  Button createButton();
  Checkbox createCheckbox();
}
class WinFactory implements GUIFactory {
public Button createButton() { return new WinButton(); }
public Checkbox createCheckbox() { return new WinCheckbox(); }
}
class MacFactory implements GUIFactory {
public Button createButton() { return new MacButton(); }
public Checkbox createCheckbox() { return new MacCheckbox(); }
}

Usage:
GUIFactory factory = new WinFactory();
Button button = factory.createButton();

Summary

The Factory Pattern streamlines object creation by separating it from the main code, making software easier to maintain and extend. It reduces dependencies, supports adding new object types, and comes in several forms. The Simple Factory, Factory Method, and Abstract Factory each helping developers build more modular, scalable systems.

Thanks

Kailash Chandra Behera

I am an IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Previous Post Next Post

نموذج الاتصال