DEV Community

Cover image for The Three Little Creational Patterns - A Design Patterns Intro

The Three Little Creational Patterns - A Design Patterns Intro

Max Antonucci on May 30, 2019

Update: Several code samples have been updated after comment section feedback. Design patterns are essential for programmers to keep in mind, at l...
Collapse
 
rkichenama profile image
Richard Kichenama

I think the Builder example does not chain as described because those methods do not return this or a new instance of Builder with a preloaded copy of brickHouse

Collapse
 
laurieontech profile image
Laurie

This is really great stuff Max. My recall on names for this stuff is terrible, so thanks for the refresh!

Collapse
 
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.

Collapse
 
danielbenzie profile image
Daniel

Nice work but I think the builder pattern could be worked on.

The BrickHouse class expects arguments in its constructor (like width height etc) in the builder class you call new BrickHouse and these are not set. This would throw an exception. You should set types on these params also. Like string, int.

You are also mutating BrickHouse class properties from the builder class. This means that BrickHouse properties are public. Think encapsulation here.

It would be better to have the builder set some properties of its own class using methods like setHeight() setWidth() etc then call a build method which creates a new instance of BrickHouse with the inputted objects. Now you can make your BrickHouse properties private and you cannot create a new instance of BrickHouse without using the builder (or passing the properly required params)

Collapse
 
maxwell_dev profile image
Max Antonucci

Those are all fair criticisms. I'll look at the builder pattern and some other examples to see if I can update it for a later edit of the post. Especially the ones about encapsulation, that's definitely bitten me before with other code I've written 🙃

Collapse
 
bconfortin profile image
Bruno Goerck Confortin

Nice job with the 3 little pigs examples. It makes learning fun :D

Collapse
 
mnkhod profile image
Munkh-Od

Cant believe the experience of those 3 pigs gonna help my career out 😂

Collapse
 
maxwell_dev profile image
Max Antonucci

I'm happy to help and even more happy to help and confuse at the same time 👍

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

Great article. Very memorable explanations.

I barely know how the Flyweight pattern works and anyone who says they do is a liar.

Not so. Robert Nystrom's book "Game Programming Patterns" completely demystifies this one.

Collapse
 
stylecramper profile image
Matt Anderson

Hi, I have a question about the Prototype example. In this code:

smallStickHouse.copy().addMailbox('wood')

You're calling StickHousePrototype's copy method, which returns a StickHouse instance, then calling the StickHouse instance's addMailbox method - am I right? I'm not sure the complete example makes sense.

Collapse
 
maxwell_dev profile image
Max Antonucci

Yeah, looking back on this now I may have aiming a bit too complicated for this example. Part of my edit will likely simplify this.

Collapse
 
frandall profile image
Andry Kurniawan

What a great insight, when I read the title, I thought it will be in Java but well, it surprised me when it was written in JS.

Collapse
 
fdevinar profile image
Fabrício Devinar

Thank you Max, really well explained and good analogies! I looked over for God Pigs when you mentioned 'piggie prayers' (LOL). Shen Dzu is their deity!

Collapse
 
lmolivera profile image
Lucas Olivera

This is an excellent article! Please tell us more about patterns, they are not easy to understand but your article made them very clear.

Collapse
 
maxwell_dev profile image
Max Antonucci

Thank you! And don't worry, I've got a whole series for this planned out 😃

Collapse
 
majedfaris profile image
majed L

amazing.

Collapse
 
iberianpig profile image
Kohei Yamada

It was easy for me to understand🐷

Collapse
 
maxwell_dev profile image
Max Antonucci

Glad to hear that 😊Pigs/bacon make everything better after all.

Collapse
 
bulatoviczeljko profile image
Zeljko Bulatovic

Hi,

Great post! You have one issue:
const smallStickHouse = new StickHousePrototype(15),
largeStickHouse = new StickHousePrototype(50);

StickHousePrototype constructor takes two parameters :)

Collapse
 
maxwell_dev profile image
Max Antonucci

Ah good catch! The extra parameter was a mistake, it should only take one and then it calculates the other. All fixed!