Javascript Design Patterns #8 - Visitor Pattern

  Рет қаралды 18,443

DevSage

DevSage

Күн бұрын

Пікірлер: 44
@DevSage
@DevSage 4 жыл бұрын
🤖 GitHub: github.com/pkellz/devsage/blob/master/DesignPatterns/Visitor.js 📕"Design Patterns Explained Simply" Ebook: payhip.com/b/MLtJ 💙 Twitter: twitter.com/realDevSage 📙 Ebooks: payhip.com/devsage 💥 Discord: discord.gg/BP8wPv6raA
@GauravBoraJodhpur
@GauravBoraJodhpur 4 жыл бұрын
I was going to sleep but then I came across a video of yours and binge-watched all the videos in the Tutorials playlist. And it is 3.30AM. So, awesome work!!
@DevSage
@DevSage 4 жыл бұрын
Glad you like them!
@clingyking2774
@clingyking2774 Жыл бұрын
2 years later and I'm watching this at 0355am
@neuodev
@neuodev 2 жыл бұрын
I happy that I "visited" DevSage!
@beastblack6654
@beastblack6654 4 жыл бұрын
I've been struggling to understand this, thanks a lot!
@DevSage
@DevSage 4 жыл бұрын
You're welcome!
@ינוןאלבז-כ1ז
@ינוןאלבז-כ1ז 2 жыл бұрын
very cool, thanks for shering, exellent channel!!!
@aniketpaul5118
@aniketpaul5118 4 жыл бұрын
Amazing videos. Very nice explanations. Would like you to consider making a video on composite pattern
@MrLinuxFreak
@MrLinuxFreak Жыл бұрын
isnt this is same as bind?
@amerikan
@amerikan 4 жыл бұрын
Overall good video, though I would just say be careful when doing Employee.prototype = { getFoo: function () { ... code ....}}; as this will overwrite the original prototype reference! It’s safer to do Employee.prototype.getFoo = function () {.....};
@DevSage
@DevSage 4 жыл бұрын
That's actually a really good point. I hadn't even thought about that until reading this comment. Thanks
@gkarapeev
@gkarapeev 3 жыл бұрын
What is the benefit of this over simply writing: devsage.salary = devsage.salary * 2; Right? We just changed the internal state of an object instance of a constructor function. What did we achieve? I'm sure there is a point to it, and it is useful in some situations. However, I didn't understand why we did it.
@SR-er6hx
@SR-er6hx 3 жыл бұрын
Useful for AST walking
@javascriptduniya1201
@javascriptduniya1201 4 жыл бұрын
var DevSage = new Employee('DevSage',10000); console.log(DevSage.getSalary()); // 1000 function doubleSalary(DevSage){ DevSage.setSalary(DevSage.getSalary() *2) ; } doubleSalary(DevSage); console.log(DevSage.getSalary()); //2000 We can also call this way, Can you please telll me why do we need accept method here ?
@DevSage
@DevSage 4 жыл бұрын
I suppose either way could work. I don't see any major advantage. The Visitor pattern is not very widely used/known in Javascript but in other languages there may be good reasons to use it.
@javascriptduniya1201
@javascriptduniya1201 4 жыл бұрын
@@DevSage Thank you
@subrotomukherjee8571
@subrotomukherjee8571 Ай бұрын
very good series
@georgidimitranov8781
@georgidimitranov8781 5 жыл бұрын
Hey, greeting from Bulgaria. Thanks for making this videos. Your explaining stuff really good. My request is. Can you make videos for the PubSup and Mediator patters. ^^
@DevSage
@DevSage 5 жыл бұрын
Greetings. Thank you for the compliment. I have a video about the mediator pattern here -> kzbin.info/www/bejne/kKbLmIKrYql6d5Y
@gkarapeev
@gkarapeev 3 жыл бұрын
+1 from Bulgaria! :D
@ratias0
@ratias0 4 жыл бұрын
Thank you very much. Now it is very clear how this pattern works, but I am still a bit confused about its use case.
@traveltechtaste41
@traveltechtaste41 Жыл бұрын
Suppose we have a method which calculates monthly salary of employees. Now company has decided to add spot bonus for few employees. In that case we can create a modifiedSalary() function. This function will contain a variable 'spotBonus' which gets added to calculatedSalary of employees. In this way we don't need to modify existing class/ function.
@PhilanJames
@PhilanJames 5 жыл бұрын
Nice tutorial. I’m trying to think about a practical use case for this. Is this scenario valid.. The Employee class is manipulated by the HR department who hires employees, logs their information and such. The ExtraSalary class is handled by the Finance department?
@DevSage
@DevSage 5 жыл бұрын
Yeah that actually sounds like it could be a valid application
@Chris-qg6kc
@Chris-qg6kc 3 жыл бұрын
Is there a particular reason that you always do JavaScript OOP using the prototype method vs class kw? I like what you are doing -continued Success 👍🏾💯👌🏾
@yshuttle
@yshuttle 3 жыл бұрын
I went to watch this video and realized I'm watching it at the exact same time it was recorded 2 years later. 3:28 PM on December 7th
@DevSage
@DevSage 3 жыл бұрын
😅😅
@DevSage
@DevSage 3 жыл бұрын
Maybe it's some kind of omen
@octaviusbytes
@octaviusbytes 5 жыл бұрын
How do you feel about making a video on javascript prototypes?
@DevSage
@DevSage 5 жыл бұрын
Hmm.. I will definitely consider it!
@ishdx9374
@ishdx9374 4 жыл бұрын
I think they are obsolete, considering that classes are introduced, and if you worry about compatibility, there's transpilers
@mfhsieh6858
@mfhsieh6858 4 жыл бұрын
@@ishdx9374 I don't think so. ES6 class is sugar syntax, it main concept is from prototype inheritance. So, it’s good to know the prototype.
@ishdx9374
@ishdx9374 4 жыл бұрын
@@mfhsieh6858 well, I meant that they are obsolete in a way that they aren't that interesting to a programmer any more, and JS can do some optimizations on classes because it knows what you are doing
@karthickdurai2157
@karthickdurai2157 5 жыл бұрын
Hey man great video but do you think solving this with class that came with ES6 is simpler.
@CrassCriss
@CrassCriss 4 жыл бұрын
just a bit simpler :o class Employee{ constructor(name, salary){ this.name = name; this.salary = salary; } getSalary(){ return this.salary; } setSalary(salary){ this.salary = salary; } accept(visitorFunction){ // this is a reference to our Employee visitorFunction(this); } } const devChris = new Employee("Chris", 10000); console.log(devChris.getSalary()); function ExtraSalary(emp){ emp.setSalary(emp.getSalary() * 2); } devChris.accept(ExtraSalary); console.log(devChris.getSalary());
@karthickdurai2157
@karthickdurai2157 4 жыл бұрын
@@CrassCriss thanks dude, though now I have become very much comfortable with js from the time the comment was posted
@CrassCriss
@CrassCriss 4 жыл бұрын
@@karthickdurai2157 haha okay, just saw the video today sorry... well..maybe its useful for someone else :)
@juliocontreras340
@juliocontreras340 4 жыл бұрын
nice pattern! thanks!
@DevSage
@DevSage 4 жыл бұрын
No problem!
@ehSamurai3483
@ehSamurai3483 6 ай бұрын
It does resembles decorator pattern.
@fatimaiqra2169
@fatimaiqra2169 3 ай бұрын
Thanks!
@preetamvarun9219
@preetamvarun9219 Жыл бұрын
Thank you
@baxi9227
@baxi9227 Жыл бұрын
lowkey thought this was a fireship video
Javascript Design Patterns #9 - Retry Pattern
21:42
DevSage
Рет қаралды 11 М.
JavaScript Design Patterns #10 - Builder Pattern
11:26
DevSage
Рет қаралды 13 М.
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 62 МЛН
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 700 М.
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН
Understanding The Visitor Design Pattern
32:08
Ryan Schachte
Рет қаралды 59 М.
Javascript Design Patterns #6 - Proxy Pattern
12:11
DevSage
Рет қаралды 28 М.
8 Design Patterns | Prime Reacts
22:10
ThePrimeTime
Рет қаралды 453 М.
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 326 М.
The Visitor Pattern
19:07
Coding Tutorials
Рет қаралды 1,8 М.
Javascript Design Patterns #7 - Mediator Pattern
13:07
DevSage
Рет қаралды 20 М.
10 Design Patterns Explained in 10 Minutes
11:04
Fireship
Рет қаралды 2,4 МЛН
Facade Pattern - Design Patterns (ep 9)
16:19
Christopher Okhravi
Рет қаралды 185 М.
Builder Pattern - Design Patterns
10:49
Web Dev Simplified
Рет қаралды 139 М.
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 62 МЛН