DEV Community

Cover image for Design Patterns Everyday

Design Patterns Everyday

Anurag Hazra on May 21, 2020

Photo by Flo P on Unsplash Originally posted on my site Hey folks! few weeks ago i started a new challenge to learn about one design pattern ev...
Collapse
 
mtfoley profile image
Matthew Foley

I'm trying to write a library for scaffolding test files for JS, and I think this post is really going to help a lot in that! Thanks for posting!

Collapse
 
anuraghazra profile image
Anurag Hazra

Great! Happy to help. :D

Collapse
 
mtfoley profile image
Matthew Foley

Yeah man - I forked the repo and ran my tool keurig
against the creational folder and found some things I needed to change!

Collapse
 
lariiurk profile image
Larissa Iurk

I started to study design patters yesterday, and in the beging I tought it is a little bit complicated. Your article really make it more easier to understand, the code examples realy helped me! Thanks!

Collapse
 
anuraghazra profile image
Anurag Hazra

Wow nice to hear that. I'm glad it helped you. 😊

Collapse
 
anuraghazra profile image
Anurag Hazra

Btw if you want you can also take the #DesignPatternsEveryday challenge.

twitter.com/hashtag/DesignPatterns...

Collapse
 
ozzythegiant profile image
Oziel Perez

Holy God! This needs to be an ebook!

Collapse
 
anuraghazra profile image
Anurag Hazra

hehe, Thank you but i learned from these sources and i think these two books deserves some kudos!

refactoring.guru/design-patterns/
sourcemaking.com/

Collapse
 
jmojico profile image
Julian Mojico • Edited

Nice Article Anurag. I need to refresh my knowledge in patterns so I will read one per day (and leave my comments, I hope that is ok!)

Day 1:Abstract Factory Pattern:
It would be good to have a summary before the ending. The most important thing to mention here:

"The benefit of this pattern is that no matter which family/factory your guiFactory instance is, you will ALWAYS implement using this code:
createButton(); and button.render());

This is where the flexibility of pattern comes in."

Day 2: Builder pattern
Missing the final step:
_"When you have finished building your object, you should call build() to instantiate:
let myTruck = truck.build();
let mySedan = sedan.build();
"

Day 4: Singleton
You can also implement a lazy initialization singleton. Same concept, but singleton instantiation it's delayed until the object is required. This can improve performance in some cases.

class LazySingleton {
name = "Im a LazySingleton";
static instance;
static getInstance() {
if(!instance){
this.instance = new LazySingleton();
}
return instance;
}
}
// instance does not exist yet
instance2 = LazySingleton.getInstance();
// instance was just created
console.log(instance.name);

Collapse
 
anuraghazra profile image
Anurag Hazra • Edited

@jmojico Opps yup! didn't noticed it the .build() part, fixed it.

Collapse
 
anuraghazra profile image
Anurag Hazra

Great. Thanks for the feedback Julian. 😊

Collapse
 
jmojico profile image
Julian Mojico

Hi again Anurag!
I'm still reading your article, day by day :)

Regarding the adapter pattern:
In this line: Does the method LoggerAdapter.log() needs type argument ?
Would it be better if it takes it from this.type ?

Thank you!

Thread Thread
 
anuraghazra profile image
Anurag Hazra

Actually i have seen some example where the type argument isn't required. And some of them used the type argument it depends on the context I think.

I think I have to update that example with better one.

Here's another example if you wanna look:github.com/anuraghazra/design-patt...

// more info
refactoring.guru/design-patterns/a...

Collapse
 
aghost7 profile image
Jonathan Boudreau

Very nice and exhaustive article! In my experience, I've seen people misuse a lot of these patterns. How would you tell if for example the strategy pattern is not a good choice for your problem? Would be great if you could expand on that in your article.

Collapse
 
anuraghazra profile image
Anurag Hazra

It's already mentioned in the post

Design patterns are solution to problems, not solution finding problems.

Design patterns are not silver bullet to every problem and if anyone is creating the problem just to solve it with a specific design pattern then it's very harmful.

IMO any solution should be rapidly prototyped first and then and only then if it's beneficial to implement a design pattern (or combination of them) we should approach for it.

Collapse
 
chidioguejiofor profile image
Chidiebere Ogujeiofor

Really nice article. Your examples are cool.

I think it would have beena better read if they were broken into smaller articles but I learnt a lot from it

Collapse
 
aju profile image
Aju Tamang

Guruji awesome.

Collapse
 
devarjunan profile image
dev-arjunan

Thanks for sharing this. One of the best posts I have come across in dev.to.keep writing 😃😃😃

Collapse
 
anuraghazra profile image
Anurag Hazra

Thank you arjun.. i'm glad you liked it.

Collapse
 
paskausks profile image
Rihards Paskausks

i know you probably did not read it

You thought wrong, sir, I read it all and even took some notes on the patterns i didn't know!

Thanks for the article!

Collapse
 
anuraghazra profile image
Anurag Hazra

Haha 😄 thanks for reading!

Collapse
 
cfecherolle profile image
Cécile Fécherolle

So great to see theoretical concepts which are usually hard to grasp made simple with examples and menmonics based on real life, thanks a lot for this article!

Collapse
 
anuraghazra profile image
Anurag Hazra

Thanks! I'm glad to that you liked it.

Collapse
 
jeferson_sb profile image
Jeferson Brito • Edited

Nice work!! I was looking for this

Collapse
 
anuraghazra profile image
Anurag Hazra

Thanks! I'm glad you found it helpful