Javascript Design Patterns #6 - Proxy Pattern

  Рет қаралды 28,487

DevSage

DevSage

Күн бұрын

Пікірлер: 36
@DevSage
@DevSage 4 жыл бұрын
🤖 GitHub: github.com/pkellz/devsage/blob/master/DesignPatterns/Proxy.js 📕 "Design Patterns Explained Simply" Ebook - payhip.com/b/MLtJ 💙 Twitter: twitter.com/realDevSage 📙 Ebooks: payhip.com/devsage 💥 Discord: discord.gg/BP8wPv6raA
@RudeyTV
@RudeyTV 5 жыл бұрын
Nice video. Just also want to mention that if you were to follow such a pattern, be aware that you need to check for outdated data, especially if you're working with data that changes a lot. One way to do that is to also attach a Date object to that specific data you're pulling in, and add some time limit for how long something will be stored in the proxy - which will be checked every time you try to fetch something via it. If you want to do a lot of this, it's worth checking out Redis, a cache database.
@DevSage
@DevSage 5 жыл бұрын
Thanks for the tip!
@sbr4339
@sbr4339 3 жыл бұрын
@@DevSage Yeah those prices definitely seem to be outdated by now
@CottidaeSEA
@CottidaeSEA 2 жыл бұрын
It's easy enough to just clear it with setTimeout. Let it be cached for 10 minutes or something like that. if (cache[key] === undefined) { cache[key] = fetchFromApi(); setTimeout(function() { cache[key] = undefined; }, 10 * 60 * 1000); // 600000 ms but easier to read just for the sake of the example. } return cache[key];
@georgegolovchiner657
@georgegolovchiner657 5 ай бұрын
Hash comparison between proxy and the original object is required. So there should be at least one network call to the original to verify cache validity.
@neuodev
@neuodev 2 жыл бұрын
this video is my "Proxy" for Design Patterns!
@GopalSinghR1
@GopalSinghR1 4 жыл бұрын
Thanks man this is crystal clear explanation
@TheArmanist
@TheArmanist 11 ай бұрын
Looks like more as singleton pattern..
@zikrilariliusra4737
@zikrilariliusra4737 4 жыл бұрын
Nice and clear explanation. i hope your channel will grow up like thenewboston and traversymedia
@DevSage
@DevSage 4 жыл бұрын
I appreciate it
@js_programmer8423
@js_programmer8423 2 жыл бұрын
can you actaully use this example in the real world or would i need to set up a real cache? like setting up cookies? or is cache basically a proxy pattern itself?
@DevSage
@DevSage 2 жыл бұрын
Really depends on what your application needs to do. Having the cache built in the code here is obviously just for example purposes, but if you had a real life web application that lived on a real server, yeah you probably would need to implement the cache in a different way (like using a Redis instance as the proxy, or using browser cookies etc). The cache is more just the idea, how you implement it is completely up to you.
@js_programmer8423
@js_programmer8423 2 жыл бұрын
@@DevSage got it, thanks !
@theotherme9965
@theotherme9965 3 жыл бұрын
Is this the pattern used for service workers?
@DevSage
@DevSage 3 жыл бұрын
Yes. Good observation
@pawandeore1656
@pawandeore1656 Жыл бұрын
but now don't we have the problem of stale data? how to deal with that
@kirillpavlovskii8342
@kirillpavlovskii8342 3 жыл бұрын
great, I like this teaching style , it would be nice to do patterns on TS
@vasurangpariya8492
@vasurangpariya8492 2 жыл бұрын
I have question that we can also impliment features in main function then why we need proxy pattern
@mahdiali4299
@mahdiali4299 Жыл бұрын
Thank you very much, but you haven't used Proxy keyword in the code. This is Proxy pattern or memoization pattern?
@hadibohluli5207
@hadibohluli5207 2 жыл бұрын
by far best explain for design patterns , I appreciate it, please make Adapter pattern video
@satlibwanzai111
@satlibwanzai111 4 жыл бұрын
Thank you Sage. But why would you declare cache as an object, then use it as an Array? Why don't you declare cache as an array in the first place?
@DevSage
@DevSage 4 жыл бұрын
I'm not using it as an array. The 'cache[coin]' syntax is an ES6 feature known as 'computed property'. 'obj' is an object and 'coin' is a variable. Javascript automatically uses the value of the variable and creates a new property on 'obj' with the value of that variable. Example const obj = {} const coin = 'bitcoin' obj[coin] = 'hello' // this is the same thing as obj.bitcoin = 'hello'
@TimWinfred
@TimWinfred 3 жыл бұрын
Awesome video! Thank you!!!
@DevSage
@DevSage 3 жыл бұрын
You're welcome! 🤙
@clingyking2774
@clingyking2774 Жыл бұрын
I think this is also reffered to as facade pattern
@tommyshaw2420
@tommyshaw2420 5 жыл бұрын
but wait im confused....If you cashe the value, then the value will not update? so when the value changes on the other end you wont know?
@DevSage
@DevSage 5 жыл бұрын
You are correct. This example could be modified in a way that cached values store a timestamp as well as the coin data. Whenever you call the proxy, check to see if the timestamp of the previous call is 'too old'. If it's old, call the API and store the new value.
@johnsonye7124
@johnsonye7124 4 жыл бұрын
@@DevSage what if handling real time data? If set timestamp for the cache, it still not going to get the data update in real time. How can we use observer pattern to handle real time data?
@developerAKX
@developerAKX 4 жыл бұрын
Love form India bro
@developerAKX
@developerAKX 4 жыл бұрын
Thinks bro
@unicrix
@unicrix 5 жыл бұрын
I wanna know about ES6 proxy instead of this. But thanks for making this video, this is a nice design pattern.
@georgegolovchiner657
@georgegolovchiner657 5 ай бұрын
ES6 adds syntactical sugar, however it's useful to know the basics first, so this video is still very useful for a novice
@raymondmichael4987
@raymondmichael4987 5 жыл бұрын
Dude you're doing such a great work bro. But can adjust that theme, it's too dark. Otherwise I learned a lot. Greetings from Tanzania 🇹🇿
@DevSage
@DevSage 5 жыл бұрын
Thank you
@be2wa
@be2wa 2 жыл бұрын
What's too dark? The contrast is OK, so it's readable and it doesn't tire your eyes like the white theme. If anything is too dark then it's probably your monitor
Javascript Design Patterns #7 - Mediator Pattern
13:07
DevSage
Рет қаралды 20 М.
Actual use case for JavaScript PROXY!
13:09
Kevin Ghadyani - JavaScript
Рет қаралды 14 М.
#behindthescenes @CrissaJackson
0:11
Happy Kelli
Рет қаралды 27 МЛН
🎈🎈🎈😲 #tiktok #shorts
0:28
Byungari 병아리언니
Рет қаралды 4,5 МЛН
Entropy 003
3:14
TJolde
Рет қаралды 49
Design Patterns in Angular Source Code - Bridge Design Pattern
16:47
Decoded Frontend
Рет қаралды 26 М.
Proxy Pattern in JavaScript
18:35
Code with Ahsan
Рет қаралды 1,1 М.
JavaScript Proxy
18:36
Kyle Robinson Young
Рет қаралды 21 М.
Proxy Pattern - Design Patterns (ep 10)
37:35
Christopher Okhravi
Рет қаралды 166 М.
Observer Pattern in JavaScript
14:29
Code with Ahsan
Рет қаралды 6 М.
5 Design Patterns Every Engineer Should Know
11:51
Traversy Media
Рет қаралды 943 М.
This is why JavaScript proxies are important
8:38
Web Dev Cody
Рет қаралды 15 М.
JavaScript Design Patterns #10 - Builder Pattern
11:26
DevSage
Рет қаралды 13 М.