DEV Community 👩‍💻👨‍💻

Discussion on: The Three Little Creational Patterns - A Design Patterns Intro

erikpischel profile image
Erik Pischel

Nice introduction to creations design patterns.

I've got an issue with your explanation of AbstractFactory. In your example you have an AbstractFactory class that has three methods - one for each type of houses. AFAIK the idea is rather you have an abstract class HouseAbstractyFactory with a method makeHouse and a concrete implementation for each type of houses that implements this method. Then in one place you decide which house type to create and pass the appropriate concrete factory around. In some other place you call makeHouse without knowing the concrete factory. I write this with an OOP language like java or c# in mind but it should translate to JavaScript as well.

In the original gang of four book, the example is a GUI library (think rich client) that supports several styles like windows or macos and the AbstractFactory class has methods for creating buttons, checkboxes, labels etc. and concrete implementations for each style. You initially choose the style by instantiating the concrete implementation but the the rest of the code only knows the AbstractFactory type (which is obviously important in an statically typed language). It also shows that an AbstractFactory may contain methods to create a family of related objects.