Javascript Design Patterns #6 - Proxy Pattern

Sdílet
Vložit
  • čas přidán 14. 11. 2019
  • What is a proxy object?
    A proxy object is an object that acts as an interface (or placeholder) for something else. The proxy could be an interface to anything: an API, a network connection, a large object in memory, or some other resource that is expensive or impossible to duplicate.
    A proxy is a 'stand-in' object that is used to access the 'real' object behind the scenes. In the proxy, extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked.
    📚Materials/References:
    GitHub Code: github.com/pkellz/devsage/blo...
    "Design Patterns Explained Simply" Ebook: payhip.com/b/MLtJ
    🌎 Find Me Here:
    Twitter: / realdevsage
    Ebooks: payhip.com/devsage
    Discord: / discord
    Merch: cottonbureau.com/people/devsage

Komentáře • 34

  • @DevSage
    @DevSage  Před 3 lety

    🤖 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 Před 4 lety +25

    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  Před 4 lety +2

      Thanks for the tip!

    • @sbr4339
      @sbr4339 Před 3 lety +2

      @@DevSage Yeah those prices definitely seem to be outdated by now

    • @CottidaeSEA
      @CottidaeSEA Před rokem +1

      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];

  • @neuodev
    @neuodev Před 2 lety +1

    this video is my "Proxy" for Design Patterns!

  • @GopalSinghR1
    @GopalSinghR1 Před 4 lety +1

    Thanks man this is crystal clear explanation

  • @kirillpavlovskii8342
    @kirillpavlovskii8342 Před 2 lety +2

    great, I like this teaching style , it would be nice to do patterns on TS

  • @zikrilariliusra4737
    @zikrilariliusra4737 Před 4 lety +1

    Nice and clear explanation. i hope your channel will grow up like thenewboston and traversymedia

  • @TimWinfred
    @TimWinfred Před 3 lety +1

    Awesome video! Thank you!!!

  • @hadibohluli5207
    @hadibohluli5207 Před rokem

    by far best explain for design patterns , I appreciate it, please make Adapter pattern video

  • @unicrix
    @unicrix Před 4 lety +5

    I wanna know about ES6 proxy instead of this. But thanks for making this video, this is a nice design pattern.

  • @developerAKX
    @developerAKX Před 3 lety +1

    Love form India bro

  • @satlibwanzai111
    @satlibwanzai111 Před 4 lety +1

    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  Před 4 lety +7

      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'

  • @raymondmichael4987
    @raymondmichael4987 Před 4 lety +4

    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  Před 4 lety

      Thank you

    • @be2wa
      @be2wa Před 2 lety

      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

  • @js_programmer8423
    @js_programmer8423 Před 2 lety +1

    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  Před 2 lety +1

      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 Před 2 lety

      @@DevSage got it, thanks !

  • @mahdiali4299
    @mahdiali4299 Před 9 měsíci

    Thank you very much, but you haven't used Proxy keyword in the code. This is Proxy pattern or memoization pattern?

  • @vasurangpariya8492
    @vasurangpariya8492 Před 2 lety

    I have question that we can also impliment features in main function then why we need proxy pattern

  • @tommyshaw2420
    @tommyshaw2420 Před 4 lety +1

    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  Před 4 lety +2

      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 Před 3 lety

      @@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?

  • @theotherme9965
    @theotherme9965 Před 2 lety +1

    Is this the pattern used for service workers?

    • @DevSage
      @DevSage  Před 2 lety

      Yes. Good observation

  • @pawandeore1656
    @pawandeore1656 Před rokem

    but now don't we have the problem of stale data? how to deal with that

  • @clingyking2774
    @clingyking2774 Před rokem

    I think this is also reffered to as facade pattern

  • @TheArmanist
    @TheArmanist Před 5 měsíci

    Looks like more as singleton pattern..