“Simple Factory vs. Factory Method” over a gradient background

Simple Factory vs. Factory Method Patterns

The Factory Method Pattern and the Simple Factory are both creational design patterns used to create objects, but they do so in different ways.

Simple Factory Pattern

The Simple Factory encapsulates the object creation process in a single method and doesn’t involve inheritance. It’s a basic implementation where a class contains a method for creating instances of other classes. This method typically takes parameters to decide which instance to create.

Here is an example in C#:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class ShapeFactory
{
    public enum ShapeType
    {
        Circle,
        Square
    }

    public static IShape CreateShape(ShapeType type)
    {
        switch (type)
        {
            case ShapeType.Circle:
                return new Circle();
            case ShapeType.Square:
                return new Square();
            default:
                throw new ArgumentException("Invalid type");
        }
    }
}

public interface IShape
{
    void Draw();
}

public class Circle : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a Circle");
    }
}

public class Square : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a Square");
    }
}

// Usage
class Program
{
    static void Main(string[] args)
    {
        IShape circle = ShapeFactory.CreateShape(ShapeFactory.ShapeType.Circle);
        circle.Draw();

        IShape square = ShapeFactory.CreateShape(ShapeFactory.ShapeType.Square);
        square.Draw();
    }
}

Factory Method Pattern

The Factory Method Pattern involves a method in a base class which is overridden by subclasses to create specific instances. This pattern uses inheritance and relies on subclasses to handle the instantiation of objects.

Here is the example in C#:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Abstract class
public interface IShape
{
    void Draw();
}

// Concrete class: Circle
public class Circle : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a Circle");
    }
}

// Concrete class: Square
public class Square : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a Square");
    }
}

// Abstract Creator
public abstract class ShapeFactory
{
    public abstract IShape CreateShape();

    public void DrawShape()
    {
        var shape = CreateShape();
        shape.Draw();
    }
}

// Concrete Creator: Circle Factory
public class CircleFactory : ShapeFactory
{
    public override IShape CreateShape()
    {
        return new Circle();
    }
}

// Concrete Creator: Square Factory
public class SquareFactory : ShapeFactory
{
    public override IShape CreateShape()
    {
        return new Square();
    }
}

// Usage
class Program
{
    static void Main(string[] args)
    {
        ShapeFactory circleFactory = new CircleFactory();
        circleFactory.DrawShape();

        ShapeFactory squareFactory = new SquareFactory();
        squareFactory.DrawShape();
    }
}

Let’s Compare the Two Approaches

  1. Inheritance:
    • In the Simple Factory, we use a single class with a static method. It doesn’t involve inheritance.
    • In the Factory Method, we use inheritance. The base class defines a factory method and subclasses override it to create specific instances.
  2. Flexibility and Extensibility:
    • In the Simple Factory, adding a new product requires modifying the factory class, which may violate the Open/Closed Principle.
    • In the Factory Method, adding a new product involves creating a new subclass. The base class doesn’t need to change, adhering to the Open/Closed Principle.
  3. Responsibility:
    • In the Simple Factory, the method is responsible for deciding which class to instantiate based on parameters.
    • In the Factory Method, the decision of which class to instantiate remains in the subclasses.

Sources For More Reading

These examples and explanations should enrich your understanding of both patterns and their differences.

The Factory Method Pattern is the one that we use the most in software development, but you’ll see or use the Simple Factory for time to time.

The Factory Method may look like overkill in terms of boilerplate code you need.

Depending on your codebase and size of the project, you may prefer one method over the other.

Adhering to the Open/Closed Principle may guide your choice.

Follow me

Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.

License GPLv3 | Terms
Built with Hugo
Theme Stack designed by Jimmy