DEV Community

Discussion on: Design Patterns Everyday

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

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
 
anuraghazra profile image
Anurag Hazra • Edited

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