loading...

Design Patterns for Developers using JavaScript - Part Two

Oliver Mensah on February 17, 2018

Creational Design Patterns In the previous article, we had a look at design patterns, its definition, history, and the incorporation int... [Read Full]
markdown guide
 

As an engineer who uses JS daily, these patterns are often lost in popular framework conventions of today. I love that you're teaching them in a really clear, applicable way. Great post! Thanks for writing it, Mr. Mensah!

 

It's awesome that you love it, Mr. Garofalo

 

Nice summary. I'd say that your "Module pattern" is more of a Facade, but that's more of a personal preference... the label is immaterial.

I think I see a bit of a problem with your Singleton. A true Singleton is supposed to guard against accidental creation of multiple instances of the singleton. From what I see in your example code, it seems that someone could accidentally do "new TaskRepo()" and create a second base to then make use of another singleton instance. From what I know of JS, it's almost impossible to stop the construction of an instance... but I could be wrong. (Please tell me I'm wrong and potentially fix your example and prove me wrong :))

I really, really liked your description of the Constructor pattern! It makes a generally confusing concept extremely clear (to me at least), and that was something that I struggled with a lot when starting JS.

 

Thank you, Mr. Miller, for the feedback, I appreciate that

Since Facade wraps a complex system to expose a simple API for the client to interact with the System, it might be confused with the Module Pattern, which also serves as a wrapper. But the Facade design pattern is often used when a system is very complex or difficult to understand because the system has a lot of dependencies. So it hides the complexities of the larger system and provides a simpler interface to the client. I will talk about the Facade Pattern when I write about the Structural Design Pattern, so let's stay in touch.

Also, about the Singleton Pattern, it was a mistake so I could create multiple instances. I have updated that by wrapping in IIFE. If you any other means of attaining that in Vannila js let me know. For Node since it uses CommonJs pattern it creates singleton anytime we load an instance of any module.

 

Thanks Oliver for your nice breakdown of the patterns. As a newbie to JS this is very helpful. I'd also love if you included common use cases for each pattern.

 
 

Is it me or the singleton pattern is embedded twice? Once in the Singleton Pattern section and once in the Constructor Pattern?

 

From my end they are different but the constructor has two different codes; one using prototype and the other using classes

 

Hey Oliver,
thanks for your work,
I appreciate it.

 

Welcome. I am happy you appreciate and like it.

code of conduct - report abuse