Update: Several code samples have been updated after comment section feedback.
Design patterns are essential for programmers to keep in mind, at l...
For further actions, you may consider blocking this person and/or reporting abuse
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 brickHouseThis is really great stuff Max. My recall on names for this stuff is terrible, so thanks for the refresh!
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.
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)
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 🙃
Nice job with the 3 little pigs examples. It makes learning fun :D
Cant believe the experience of those 3 pigs gonna help my career out 😂
I'm happy to help and even more happy to help and confuse at the same time 👍
Great article. Very memorable explanations.
Not so. Robert Nystrom's book "Game Programming Patterns" completely demystifies this one.
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.
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.
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.
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!
This is an excellent article! Please tell us more about patterns, they are not easy to understand but your article made them very clear.
Thank you! And don't worry, I've got a whole series for this planned out 😃
amazing.
It was easy for me to understand🐷
Glad to hear that 😊Pigs/bacon make everything better after all.
Hi,
Great post! You have one issue:
const smallStickHouse = new StickHousePrototype(15),
largeStickHouse = new StickHousePrototype(50);
StickHousePrototype constructor takes two parameters :)
Ah good catch! The extra parameter was a mistake, it should only take one and then it calculates the other. All fixed!