DEV Community

loading...

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

Collapse
ddarrko profile image
Daniel Benzie

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 Author

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 🙃