DEV Community

Mirnes
Mirnes

Posted on • Originally published at optimalcoder.net on

5

Design Patterns: Factory Method

Let’s continue our series on design patterns with the Factory Method. Like the Builder or Singleton patterns we covered in previous posts, the Factory Method is also one of the creational patterns. It introduces a slightly more complex approach, offering a higher level of abstraction for object creation. Additionally, it helps decouple the object creation process from the rest of the application.

It can be represented through the following components:

  • Abstract object , which represents the base class for the objects that we want to create
  • Concreate objects , classes that inherit the given base class
  • Creator , abstraction of the classes resposible for the object creation
  • Concreate creators, classes that inherit and implement given creator class
  • Client or Consumer side , parts of the application where we call objects creation Bellow, we will show all these parts in one simple example.
using System;

// abstract object
abstract class Engine
{
    public abstract void Start();
}

// Concrete object, concrete Engine classes
class DieselEngine: Engine
{
    public override void Start()
    {
        Console.WriteLine("Starting diesel engine!");
    }
}

class PetrolEngine: Engine
{
    public override void Start()
    {
        Console.WriteLine("Starting petrol engine!");
    }
}

// Creator
abstract class EngineFactory
{
    public abstract Engine CreateEngine();
}

// Concrete Creators
class DieselEngineFactory : EngineFactory
{
    public override Engine CreateEngine()
    {
        return new DieselEngine();
    }
}

class PetrolEngineFactory : EngineFactory
{
    public override Engine CreateEngine()
    {
        return new PetrolEngine();
    }
}

// Client code
class Program
{
    static void Main(string[] args)
    {
        EngineFactory factory;

        // Let's assume user chooses the engine type at runtime.
        Console.WriteLine("Enter the type of engine (diesel/petrol):");
        string choice = Console.ReadLine()?.ToLower();

        if (choice == "diesel")
        {
            factory = new DieselEngineFactory();
        }
        else if (choice == "petrol")
        {
            factory = new PetrolEngineFactory();
        }
        else
        {
            Console.WriteLine("Invalid choice!");
            return;
        }

        Engine engine = factory.CreateEngine();
        engine.Start();
    }
}

Enter fullscreen mode Exit fullscreen mode

In the example above, we can see the simple case where this pattern could be applied. Now, when we want to add a new engine type, we just need to extend existing logic with new engine type, without change of existing code resposible for Diesel or Petrol engines. Here we can see the flexibility of such approach, but of course, in some cases this can be the overhead. Examples of the overhead could be when we have the cases for simple object creation or when we know that extension of existing objects creation won't happen in the future. In such cases, this pattern would be just an overengineering and not necessary at all.

Image of Stellar post

How a Hackathon Win Led to My Startup Getting Funded

In this episode, you'll see:

  • The hackathon wins that sparked the journey.
  • The moment José and Joseph decided to go all-in.
  • Building a working prototype on Stellar.
  • Using the PassKeys feature of Soroban.
  • Getting funded via the Stellar Community Fund.

Watch the video 🎥

Top comments (0)

AWS Industries LIVE! Stream

Business benefits of the cloud

Join AWS experts and tech leaders as they discuss the business impact of the cloud on Industries LIVE!

Learn More

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay