The Power Of Golang's Decorator Pattern

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 พ.ย. 2024

ความคิดเห็น • 69

  • @anthonygg_
    @anthonygg_  ปีที่แล้ว +3

    ► Join my Discord community for free education 👉 discord.com/invite/bDy8t4b3Rz
    ► Pre order (get 30% off) my Golang course 👉 fulltimegodev.com
    ► Become a Patreon for exclusive tutorials 👉 www.patreon.com/anthonygg_
    Thanks for watching

  • @mirooo6243
    @mirooo6243 ปีที่แล้ว +63

    It's a good pattern to do complex stuff if you need a lot of freedom, but in most cases using function closures everywhere is just an overkill and hard to read/debug. In most cases using a struct with methods that accepts an interface (like DB in this case) is more readable, more logical to grasp and also perfectly doable to test.

    • @ragequilt_
      @ragequilt_ ปีที่แล้ว +5

      Bang on! I'd advocate doing most of the closures stuff around the middlewares and inject an encapsulated struct containing the db, logger and monitoring connections into the request, rather than using closures all over the place. I think the context object is the ideal candidate to extend when it comes to injecting dependency.

    • @Dozer456123
      @Dozer456123 10 หลายเดือนก่อน +3

      @@ragequilt_ Shoving dependencies into context is a nightmare for explicit dependency expression. It's convenient, at the cost of being unclear, hard to contain, and hard to reason about what's in the context and how it's getting there.

    • @robertsteele6415
      @robertsteele6415 8 หลายเดือนก่อน +1

      @@ragequilt_ @Dozer456123 Not sure where I read this but I remember reading that putting required data into context is an anti-patern. Context is meant to only contain optional values. It can be used to provide contextual information but the program shouldn't crash if something required isn't present in the context. Much better to explicitly pass what you need.

    • @ragequilt_
      @ragequilt_ 7 หลายเดือนก่อน

      @@robertsteele6415 Could you point me to anything which talks about this. My perspective was largely in the context of the stuff in the video. For ex: An API which is largely request-response doing standard CRUD operations, can pass on the database connection as a context object. A lot of these decision depend on what the application is doing. I don't mean to imply all dependencies should be wrapped up in the context.

    • @Palessan69
      @Palessan69 2 หลายเดือนก่อน

      New in functional programming. Can you give an example of the above code using the struct with methods that accepts an interface?

  • @duongphuhiep
    @duongphuhiep ปีที่แล้ว +25

    This is only a common technique in functional programming called "higher-order function". In Go, it is only useful to extend an existing "black Box Function" which you cannot touch. You simply create a newFunction(), which take the blackBoxFunction() as parameter, do your stuff in your newFunction before (and or after) calling the blackBoxFunction(). FInally, you will use your NewFunction() to replace the blackBoxFunction().
    You should not abuse this technique everywhere as he said, because it makes your code harder to read and to maintain (the more higher-order a function is, the harder to understand it). In normal circumstance where you don't have to extend a blackBoxFunction, You should just write normal & idiomatic Go codes:
    - Concerning the Decorator Pattern, this video show this pattern in idiomatic Go codes, no need to wrap your head around higher-order function: th-cam.com/video/xyBcoGNGrsI/w-d-xo.html
    - Concerning the "Dependency Injection" it is nothing fancy: Your function require a DB interface as input parameter (your function depends on this input), when you call it, you will provide (so inject) the real implementation of this interface (or a mock implementation to unit test your function).. Everything is normal idiot Go Codes, no need for anything fancy for this kind of "Dependency injection". However in a more complex Go project whose the Dependency Graph is complex, you might need a DI container and a DI Resolver, and there are already some lib providing you these things For eg: github.com/goioc/di or github.com/alibaba/IOC-golang Evens in this scenario your Go Code stays boring & idiot

    • @alexeyshevelyov1163
      @alexeyshevelyov1163 9 หลายเดือนก่อน

      Great info. You have a blog or something? Love it, bruh - straight to the point, no water.

  • @matthias2447
    @matthias2447 ปีที่แล้ว +79

    Ah yes, finally we are getting into software design patterns in the context of Go. Please show some more patterns (e.g. (abstract) factory, strategy, adapter etc.) :)

    • @anthonygg_
      @anthonygg_  ปีที่แล้ว +26

      Owke thats the feedback I need.

    • @Kyrogue
      @Kyrogue ปีที่แล้ว +1

      +1

    • @gokulakrishnanr8414
      @gokulakrishnanr8414 8 หลายเดือนก่อน +1

      Awesome! 😃 Here are some more patterns: factory, strategy, adapter, and more. 🚀

    • @gokulakrishnanr8414
      @gokulakrishnanr8414 8 หลายเดือนก่อน +1

      Awesome! 😃 Here are some more patterns: factory, strategy, adapter, and more. 🚀

    • @tsioryfitiavanaanhykrishna6992
      @tsioryfitiavanaanhykrishna6992 5 หลายเดือนก่อน

      +1 !

  • @tanko.reactions176
    @tanko.reactions176 ปีที่แล้ว +2

    this is the single most important technique in all of software development.
    i dont mean the closure where you capture the DB, but rather, the dependency injection and contract based programming (using interfaces to dictate what your injected dependencies can do, you can swap them out easily as you do not care about the actual underlying thing, just that it fulfills the contract outlined in the interface). this is super strong for mocking and testing things! and its super clean because of decoupling!
    your business logic has nothing to do with any concrete class.. it just requires some interfaces to be fulfilled to be happy!

  • @jackhall345
    @jackhall345 ปีที่แล้ว +3

    Dude Anthony I'm so happy to have found your channel. Amazing content.

  • @vanshajdhar9223
    @vanshajdhar9223 ปีที่แล้ว +3

    The way you show things at real time are so amazing. It teaches a lot.

  • @ievgenk.8991
    @ievgenk.8991 8 หลายเดือนก่อน +2

    This is an adapter pattern. Decorator pattern its more about returning entity with the same type, but with additional behaviour.

  • @whoopsimsorry2546
    @whoopsimsorry2546 9 หลายเดือนก่อน +1

    Add a little WithOptions pattern in there too, you can have 20 steps to through and just straight up inject them anywhere. This is how I write the Validation layer.

  • @hocky-ham324-zg8zc
    @hocky-ham324-zg8zc ปีที่แล้ว +6

    Great tutorial, but in my opinion, more concrete examples would be more helpful. It’s easy to get lost in the weeds when the amount of functions starts increasing and they do things like print “FOO BAR BAZ”

  • @amansarma417
    @amansarma417 ปีที่แล้ว +6

    So good content. I dont think anyone would explain dependency injection that easy. Even a noob beginner like me could easily understand it.

  • @melihkorkmaz
    @melihkorkmaz 11 หลายเดือนก่อน +2

    Great video thanks. It's something very common for JS developers I believe.

  • @nowarm
    @nowarm ปีที่แล้ว +2

    Bro you rock! Lmao, really love your style and how you always say why and why not to do things without BS! 🤩

  • @टिरंजननकले
    @टिरंजननकले ปีที่แล้ว +5

    Excellent but why do you type so fast as if some tiger is running behind you?

    • @anthonygg_
      @anthonygg_  ปีที่แล้ว +2

      How do you know there is no tiger behind me? 😅

  • @wralith
    @wralith ปีที่แล้ว +4

    Higher order function 🚀

  • @stephanschmidt2334
    @stephanschmidt2334 ปีที่แล้ว +4

    I would rather call it a Factory pattern (instead of class for functions) - a function that creates other functions based on parameters.
    Decorator to me would be more like: a function that takes a function, does something around it or with the parameters and then calls the function. func decorate(t func) func { return func() { print "X"; t() }}

  • @JR7SQUARE
    @JR7SQUARE ปีที่แล้ว +3

    Yup this pattern is good when used sparsely. I made the mistake before of using this everywhere and things quickly spiral out of control.

  • @gtw-ds9xl
    @gtw-ds9xl ปีที่แล้ว +7

    what use cases does the decorator pattern come in handy for? seems like a lot of boilerplate code and I'm not sure i understand yet where this could be applied

    • @anthonygg_
      @anthonygg_  ปีที่แล้ว +4

      Every golang http framework is based on this pattern

    • @abeplus7352
      @abeplus7352 ปีที่แล้ว +2

      Writing
      - code that has high cohesion and low coupli g
      - code that adheres to solid patterns
      - code that’s easily testable as a unit
      - code that is very modular and easy to change versions.
      Writing code without patterns can only get you so far before you end up with spaghetti …

    • @QckSGaming
      @QckSGaming ปีที่แล้ว +2

      Apart from what was described in the video, when you start unit testing your code, you will realize its power

    • @gtw-ds9xl
      @gtw-ds9xl ปีที่แล้ว

      ah makes sense, thanks

  • @mohamedmegg2796
    @mohamedmegg2796 ปีที่แล้ว +3

    the keyboard stroks sound is distracting bro, never seen a tutorial like that. can you please consider this in the fututre?

  • @ThomasJunkOS
    @ThomasJunkOS 3 หลายเดือนก่อน

    As a Pythonista you immediately understand :]

  • @eastquack3342
    @eastquack3342 ปีที่แล้ว +2

    Tony GG I'm gonna find myself a lady only to have you be my best man so that I can legitimately pick your brain whenever I want to; your content is amazing, it may take me a week to diggest a vid like this however it is amazing, thanks man

  • @iordanisgrigoriou
    @iordanisgrigoriou ปีที่แล้ว +1

    Great stuff! Can you maybe give an example of how to handle an error in myExecuteFunc?

  • @RodrigoBragaPrata
    @RodrigoBragaPrata 8 หลายเดือนก่อน +2

    Very cool topic. I want to boost my vim like yours, could you tell what plugins you use?

    • @anthonygg_
      @anthonygg_  8 หลายเดือนก่อน

      None.

    • @MythuSiva
      @MythuSiva 4 หลายเดือนก่อน

      th-cam.com/video/AzhSnM0uHvM/w-d-xo.html

  • @sjakievankooten
    @sjakievankooten ปีที่แล้ว +2

    Muscles go hard!

  • @sergsergesrgergseg
    @sergsergesrgergseg ปีที่แล้ว +5

    love u anthony

  • @nf-racing71
    @nf-racing71 6 หลายเดือนก่อน

    I really like your videos, but could you explain a bit more what is happening , how it works, etc + being a little more calm and ordered?

  • @pukito9123
    @pukito9123 ปีที่แล้ว +2

    lovely

  • @h3av3ns
    @h3av3ns ปีที่แล้ว +2

    Where do I find your dotfiles?

    • @anthonygg_
      @anthonygg_  ปีที่แล้ว +1

      Its on my Github. Https://github.com/anthdm/.nvim

  • @KManAbout
    @KManAbout ปีที่แล้ว +2

    Instead of passing writers and readers everywhere it would make more sense to pass a context

  • @zachadolphe3633
    @zachadolphe3633 ปีที่แล้ว +1

    Has anyone taken this course? How is it so far?

  • @delsoft99
    @delsoft99 2 หลายเดือนก่อน

    You're wrong, what you've presented is different, it's just a higher-order function. In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically.
    One more item to think about, your solution breaks the Open-closed principle, by adding a new “decorator” you need to refactor
    the previous chained function

  • @jackkorovev5217
    @jackkorovev5217 10 หลายเดือนก่อน +1

    MyChicken(YourEgg(HisChicken(EggStore))). Now could you tell me, at first glance, which function came first, the chicken or the egg?

  • @sarcasticdna
    @sarcasticdna 9 หลายเดือนก่อน +1

    People for js land, we call it currying 😅

  • @lying6624
    @lying6624 ปีที่แล้ว +2

    we also need Anthony's muscle pattern

  • @saurabh9446
    @saurabh9446 ปีที่แล้ว +1

    Plzz include k8s deployment as well in the course 😅

  • @ukrustacean
    @ukrustacean ปีที่แล้ว +3

    He still calls his ex func... That's sad

  • @darthzalim
    @darthzalim 11 หลายเดือนก่อน +1

    I find your videos very useful. Thank you sharing them. However, your IDE, especially the cursor is bouncing back and forth all the time and it makes it very hard to track. It is too distracting. Maybe it is just me, but wanted to share.

  • @anaz6794
    @anaz6794 ปีที่แล้ว +1

    Dude, give me your course for free if you're interested 😄

  • @KManAbout
    @KManAbout ปีที่แล้ว +2

    I think its better to call this the handler pattern

    • @anthonygg_
      @anthonygg_  ปีที่แล้ว +1

      Yeah, I can feel that. Why not.

  • @1879heikkisorsa
    @1879heikkisorsa 10 หลายเดือนก่อน +2

    I do like your videos, but this one seems a bit odd to me. First, the naming of your variables and functions is very generic for an example. It would be easier to follow with precise naming. Second, nobody will inject DB methods into a http handler in a real project. Using clean architecture solves this usecase entirely and in a better way. Third, your typing is distracting. You type like 500 characters a minute, but need to correct every third of them. Try to be less error prone in order to let people focus on the topic, not on typing errors. Otherwise, nice videos!

  • @soberhippie
    @soberhippie หลายเดือนก่อน

    Why `Execute(myExecuteFunc())` and not `myExecuteFunc()()`?

  • @ГеннадийОловянников
    @ГеннадийОловянников 8 หลายเดือนก่อน +2

    You are doing too much mistypes and cursor movements without reason - it distracts attention and makes difficult to follow the idea. Don't rush :) You can prepare code fragments before recording video and just uncomment nesessary parts during explanation

    • @anthonygg_
      @anthonygg_  8 หลายเดือนก่อน

      Ok papi

  • @user-ly2lh9ml4d
    @user-ly2lh9ml4d ปีที่แล้ว +2

    Love your stuff ... BUT stop moving the cursor around when not needed ... it drives me NUTS!!! I cant' watch it

  • @mSpinks01
    @mSpinks01 ปีที่แล้ว +2

    $$$