DEV Community

Discussion on: Design Patterns for Developers using JavaScript - Part Two

Collapse
 
millebi profile image
Bill Miller

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.

Collapse
 
olivermensahdev profile image
Oliver Mensah

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.