Hey there!
I recently went through multiple knowledge resources to learn more about popular design and architectural patterns in Node.js. My aim w...
For further actions, you may consider blocking this person and/or reporting abuse
Trying to be positive here, but these examples leave a lot of room for improvement. I.e.
getInstance
static method, when you can just export the instance by itself. E.g.Hey there. Thanks for these additional examples!
I do agree that my examples are not perfect. I have selected them for simplicity while trying to explain the subject. For anyone who will be reading it and looking for something more optimized and advanced will see your comment and will be able to use it :)
I agree on most points. The factory one is more like a preference thing - I usually use the object map approach you suggested, rather than a switch statement, but I'd still consider that a preference.
Re: observer.. sure.. but since the article's title is like it's aimed at node.js, it's insane to not talk about EventEmitter - it's the de-facto pub-sub thing to use in node.js and it is used so much in almost every single project - most people won't implement their own observer thing - they'll just use EventEmitter - so that is the most valualble feedback to give on that front.
And regarding decorators.. dude, the state of decorators is in shambles.. I would strongly discourage people from using decorators as much as possible until they become part of standard ecmascript -- they're at stage 3 right now, and with all the differing interfacing between typescript and node.js transpilation etc.. it's a mess better left out if possible.
I do realize a lot of frameworks use them by using the legacy decorator transpilation stuff, but to me that is just an ugly mess, and I'd rather just write plain code until they are fully standardized.
Hey there,
Thanks for this comment and additional details! Viewers can now see it and get more examples and start using it in their projects :)
I've learned a lot
export const redis = new Redis(...);
export default redis;
With this, I think we need to fix the constructor to make sure new Instance are not created?
constructor() {
if (Redis.instance) {
return Redis.instance;
}
Redis.instance = this;
}
You don't, as you are not exporting the classz just the instance, which is the whole point.
You may want to test some of your examples.
You have an error in the first one with capitalising
Return
this will cause an error.Also, you have a space between
Redis.
andinstance
which is not actually an error, just reads weirdly.For the Factory pattern, you re-used the variable
name
for bothtype
of character to create and thename
of the created character.This will obviously work, but just muddies the water for any new developer, and there is not a single advantage to doing so.
You use both direct variable access,
character.endurance
and getter methodaction.character.getEndurance()
but with no explanation of why one and not the other in either scenario. It leavesgetEndurance()
as completely unnecessary and muddies the water for no advantage.Hey, thank you so much for pointing that out. I fixed first two issues but not sure about the third one. Could you please elaborate which part does not make sense to you? I am not sure what to rewrite :(
What has this got to do with Node or JavaScript? Those are common patterns in any language capable of OOP.
Also the decorator pattern can be very confusing when decorators are typical known for something else in JavaScript...
In the article, I mentioned few times that some of these patterns could be observed in frameworks such as Nest.js - I am using this framework on the daily basis and while it is built using TypeScript it is technically Node.js application with Express framework.
I wanted to share some of these patterns that could be used for regular Node.js as well. You can also build Node.js applications with TypeScript -> nodejs.org/en/learn/typescript/int...
I do agree that the decorator pattern could be missleading. Would you recommend to adding a note there that it only applies to Node.js apps built with TypeScript? :)
You should have a look at deepkit.io that utilizes TypeScript to the fullest.
Nest framework built on top of OOP, I believe
Good description of these common 5 patterns. Thank you!
P.S.: In the singleton example, the first "redis" instance is declared as
const medicine = Redis.getInstance();
I'm sure you mean
const redisOne = Redis.getInstance();
don't you?
Damn, good catch!
Gramarly must have corrected this redisOne into medicine.
Thank you for catching that! :)
This post is a cool learning experiment 💪🏼 though I feel like it's necessary to explain that there's no real need for most design patterns mentioned here, specially in JavaScript, a multi-paradigm language.
This might help giving context to beginners:
Demystifying Design Patterns
JoelBonetR 🥇 ・ Oct 30
Cheers!
Article has 5 examples, the perex says 10... 🤨
Good catch! I initially wanted 10 but decided to decrease it to make it shorter. I will fix it right away! :)
Five design pattern to know in every OOP capable language, should be a less click bait version of the title.
Correct, but honestly I was not aiming for click baiting. Node.js is my area of expertise and these design patterns I managed to use by building applications with Nest.js and Nuxt (Vue) where TypeScript is use :)
Your title is far more click bait than the node one