DEV Community

Igor Neves Faustino
Igor Neves Faustino

Posted on • Originally published at nfaustino.com

2 2

Finally understanding the factory design pattern

When I was studying Computer Science, I remember my professor trying to explain factories so enthusiastically about the subject that I honestly felt bad for not fully understanding it at the time.

To be honest, I understood the concept, but the UML diagram didn't quite relate to how I could use it to create better code, and now, years later, I finally have the same enthusiasm about the topic that my professor had. Because nowadays I find myself needing to create factories from time to time.

What are factories

The factory is a design pattern listed by the famous GOF design pattern book.
the factory is a creation pattern that informs ways to abstract object instantiations in a way that reduces couple and make the code simple

Factories are very common and work as a base to many other patterns.

Code example

Let's create some simple examples to show the pattern. These examples are created using typescript but can be translated into any language.

class ControllerFactory {
    static create() {
        const repo = new DatabaseConnection();
        const service = new Service(repo);
        const authenticator = new Authenticator();
        return new Controller(service, authenticator);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this first example, we use a factory to abstract all the logic to create a controller. This means that whoever uses the controller doesn't need to know anything about how the object is being created and its dependencies.

This abstraction means that eventually, we can change the steps of creating an object without changing everywhere the object is being used.

Now let's create a more complex example.

interface Message {
    send(): void;
}

class EmailMessage implements Message {
    send() {
        // ...
    }
}

class TelegramMessage implements Message {
    send() {
        // ...
    }
}

class MessageFactory {
    static create(type: 'email' | 'telegram'): Message {
        if (type === 'email') return new EmailMessage();
        if (type === 'telegram') return new TelegramMessage();
    }
}

class Application {
    main() {
        const message = MessageFactory.create('email');
        message.send();
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we create a factory that can decide which object needs to be created depending on the strategy passed. The factory will always return a Message, but depending on the type, this message can behave completely differently from one to another, which is the principle of the strategy pattern by the way.

Conclusion

So, all in all, the factory pattern is one of the most common patterns when working with object-oriented programming, and the goal is to abstract all the logic needed to instantiate an object.
Creating a simple interface to interact with and allowing you to select the right object that needs to be used.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay