Keeping the Design Patterns going…

Dolly Desir
4 min readJan 25, 2021

Hey ya’ll ! Prototype and Builder are the remaining methods for me to discuss in Creational design patterns. Starting with Prototype, this pattern creates new objects but it returns objects that are initialized with values it copied from a prototype. Okay, explain that in english — remember the sheep Dolly? Remember when they cloned her & it was such a big deal? Understandably so because humans living on Mars next, amirght?!?! Let’s get back on track, cloning in a developer’s world is a big deal because of the performance boost it offers & it’s cost effective.

Let’s dive into the code ! Lines 1–5 is my human object that I’ve created. Line 10 is where I create a new object that will inherit all the properties of my human object. Lines 11 & 12 I am adding more properties to my engineer object, a function called knownLanguage which will log a programming language. Lines 15–17 an instance of engineer is being created with all the properties of the human object and also inheriting the properties of the engineer object. Below is a closer look at the dolly object, the first prototype is our engineer object, below that prototype is our original human object with the properties of sayName and saySpecies!

inspecting the dolly object.
with prototypal design, properties can change & be inherited.

Now let’s move onto the Builder Pattern Design, this pattern is useful when you have an object that has a few different working parts with optional or required fields. There are two ways to write the Builder method.

Imagine that I’m building an app for a dog shelter and whenever a new dog comes into the shelter, at the very least it has to have a name. Until a vet can come assess the dog to know it’s breed and estimate an age, I don’t have to enter that information. Also until I spend more time with the dog and get to know it’s personality, I’d like to leave it blank. Lines 1–5 is the Dog class that only has a name as a parameter. Lines 7–10 is the DogBuilder class that’s also only taking a name as a parameter. Lines 12–29 are the optional properties that a dog object can be created with if I need to update my doggo Georgie with his age & breed. The return this within the optional properties always returns the same dog object, therefore allowing me to chain those properties together on line 30.

Another way to write a Builder design using way less code. Instead of having a Dog class and a DogBuilder class I can just narrow it down to one. Line 2 the name parameter is still required, but we pass in the optional parameters as objects. The empty {} just tells Javascript, if none of the optional parameters are given, just default it to an empty object instead of just nothing at all. Lines 3–6 work exactly as the set statements from the first Builder method but on the developer side they’ll be seen as key value pairs. Another plus about this method is, you can set a default parameter if one isn’t given until the object is updated.

All parameters passed in.
No personality parameter.
Cute as a default for the personality parameter.

The more Design Patterns I dive into the more I like one over another but honestly I just really really enjoy programming & learning to apply concepts and ways to program not only for myself but also for the future developers who have to read my code. I hope you found this blog helpful, I leave you with a picture of the infamous Georgie.

Georgie the Labradoodle is very silly but also quite demanding.

--

--