How To Reduce Coupling With Facade | Design Pattern Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 มิ.ย. 2024
  • The Facade design pattern is a great choice if you want to connect with a complex system via a simplified interface. In this tutorial, I show you how to apply the pattern to your own code and discuss some of the limitations of the pattern.
    👉🏻 The code I worked on in this video is available here: github.com/ArjanCodes/2022-fa...
    👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis
    💻 ArjanCodes Blog: www.arjancodes.com/blog
    🎓 Courses:
    The Software Designer Mindset: www.arjancodes.com/mindset
    The Software Designer Mindset Team Packages: www.arjancodes.com/sas
    The Software Architect Mindset: Pre-register now! www.arjancodes.com/architect
    Next Level Python: Become a Python Expert: www.arjancodes.com/next-level...
    The 30-Day Design Challenge: www.arjancodes.com/30ddc
    🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
    👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!
    💬 Discord: discord.arjan.codes
    🐦Twitter: / arjancodes
    🌍LinkedIn: / arjancodes
    🕵Facebook: / arjancodes
    📱Instagram: / arjancodes
    👀 Code reviewers:
    - Yoriz
    - Ryan Laursen
    - James Dooley
    - Dale Hagglund
    🎥 Video edited by Mark Bacskai: / bacskaimark
    🔖 Chapters:
    0:00 Intro
    0:51 Example explanation (IoT)
    4:22 The facade pattern
    5:47 Controller
    12:25 Connecting to the GUI
    16:14 Partial function applications
    18:58 Facade pattern layer
    26:03 Outro
    #arjancodes #softwaredesign #python
    DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

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

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

    👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis

  • @stonebam4611
    @stonebam4611 ปีที่แล้ว +11

    Your videos, especially on coupling, are really great! This series helped me so, so much beeing a better developer! Thank you very much!

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thanks so much, glad the content is helpful!

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

    Love the design patterns content!

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

      Thanks Mikko, Glad you like them!

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

    Excellent, I've been throwing the word "Facade" around recently in my design for an Android App... This video popped up in my TH-cam suggestions and I thought I'd better revisit it to make sure I'm not embarrassing myself. :)

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

    totally love the video! I’m just curious about this but do you ever use your editor or IDE’s re-factoring functions or do you always just re-factor manually?

  • @mamazu1995
    @mamazu1995 ปีที่แล้ว +8

    The facade pattern is a good pattern and you explained it well. But I think a better example would have been if you had abstracted away the IOTSpeaker as a class. Then the object would just have an on, off and maybe toggle function which hide away the complexity of the interaction with the speaker. And this would also make it much easier to add more speakers later on.

    • @aflous
      @aflous ปีที่แล้ว

      Yep, I was expecting to see protocol at some point as well

  • @MRECoelho
    @MRECoelho ปีที่แล้ว

    I love the videos on patterns especially in combination with some examples, so this is another great one imo. However, I must admit that the delivery of the joke in the intro rivals the content. I don't know what it was, but it had me in stitches :D
    Keep up the great work!

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

      Thanks M. Coelho, happy you’re enjoying the content!

  • @thisoldproperty
    @thisoldproperty ปีที่แล้ว

    I wasn’t aware of the partial function. Nice one!

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thanks so much, glad the content is helpful!

  • @mrsystem9560
    @mrsystem9560 ปีที่แล้ว

    I admire you man! you are amazing

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thank you, glad you liked the video!

  • @FolkOverplay
    @FolkOverplay ปีที่แล้ว

    I would love to see another device to this code as a follow-up to see the more long-term benefits of this.

  • @gurkhart
    @gurkhart ปีที่แล้ว

    Nice use of partial here. Any thoughts on managing a larger amount of controller functions with this approach?
    It seems like the benefit may taper off at some point, and would leave you reaching for classes.

  • @aashayamballi
    @aashayamballi ปีที่แล้ว

    Thank you! 🙏

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      You are so welcome Aashay, Glad the content is useful.

  • @doluparsad6379
    @doluparsad6379 ปีที่แล้ว

    Hi Arjan! As always great and helpful content.
    I am a recent graduate who just started a new job. As of now, my main object in this job is to read the code base and understand the system. However, I find it quite overwhelming to go through heaps of codes. Although the code is written well (modular and well organized), I am finding it difficult to piece them together to understand the bigger picture. I find it easy to understand functions and classes that have no dependencies, but I can't get my head around with classes and functions with dependencies (and the difficulty seem to increase exponentially if the number and levels of dependencies increases. I think the obvious solution would be to build my way up from classes that don't have any dependencies but that's sometimes not feasible if the code base is huge). I would really appreciate it if you could make a video of how to read code from huge code base (by may be looking at a huge open source repo).

  • @jaimesegura4129
    @jaimesegura4129 ปีที่แล้ว

    Hello. Thank you for your videos. I appreciate the beat software engineering coding practices you present. Any recommendations regarding code that is meant to interface a command line tool with other formats? How should the commands to the command line tool should be stored? In a dictionary... Perhaps each command in a function/method? Thank you in advance.

  • @yassinemahtat9373
    @yassinemahtat9373 ปีที่แล้ว

    Great video, I'm learning a lot from you. Thank you very much!
    It would be very cool if you make a video on 'How to use the Domain-Driven Design architecture with Django'.

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thanks Yassine, Glad the content is useful.
      And Great suggestion thank you. I have put it in my list.

  • @python6367
    @python6367 ปีที่แล้ว

    Perfect
    It's great if use material icon theme for vscode

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

    Great video as always !
    What if you would also have temparature sensor that periodically triggers specified callback function with the current value of the temparature as an argument. How would you connect this callback to update value in the UI window? I imagine IOTFacade would require additional argument that expects a function that is called with the temparature value, everytime the sensor sends it. But as you create UI (SmartApp) once the IOTFacade is already created, you cannot pass the SmartApp's method, that would update UI with new temparature value, to IOTFacade. Basically my question is, how to connect signals that come from IOTFacade towards SmartApp class.

  • @patrick1532
    @patrick1532 ปีที่แล้ว

    Wow, I started watching this wondering if it might be something I'd like to implement in a big project I'm currently working on and it turns out the design I have currently implemented is pretty much exactly this.

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Haha, always love that when it happens :).

  • @beaulingpin
    @beaulingpin ปีที่แล้ว

    Thanks for the video. Here a bit of engagement for your work (I wonder if the algorithm factors comment_length into its weighting).

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

    Thanks for the video Arjan! When you create IOTFacade is there any particular reason you don't use dependency injection for SmartSpeakerDevice() but you create it inside the init method instead?
    Also, I assume the controller functions could also be part of a separate class, e.g. Controller? Then we could use composition in a way that the Controller class owns an instance of IOTFacade.

    • @egorrodygin4693
      @egorrodygin4693 ปีที่แล้ว

      About the second question. He says about it in the video. He prefers partial functions because they can possibly be moved into separate files. But the class is OK too.

    • @danny_p466
      @danny_p466 ปีที่แล้ว

      @@egorrodygin4693 Yeah figured this out in the meantime, I was writing the comments while I was watching the video :D

  • @nalinor
    @nalinor ปีที่แล้ว

    You said you;d use a separate function to create the service and the facade and stitch it together to further decrease coupling.
    What's your opinion on passing a service object to the facade's __init__ function?
    I'd default it to =None and then create the service object in the body of the __init__. In testing, I'd explicitly pass some mock service to the __init__function.

  • @ivanherreros7773
    @ivanherreros7773 ปีที่แล้ว

    I think that all these patterns do really achieve practical "simplification" when the code refactoring is documented with a class/module diagram.
    Another thing to understand is that rather than "simplifying" the existing code (as in "it is now simpler to understand the program as a whole"), it simplifies its maintenance, makes it more evolvable, etc.
    Many developers will find the initial code easier to understand than the final one, even though the final code is conceptually "cleaner". That is because, to understand the latter, you need to implement a "stack" of calls in your head.

  • @NickLis-ob7fl
    @NickLis-ob7fl ปีที่แล้ว

    Arjan, tell about your vim config and vscode settings. pls

  • @fredesch3158
    @fredesch3158 ปีที่แล้ว

    0:37 I don't know if my sense of humor is broken, but I laughed more than I should've with this joke LOL

  • @billfujimoto5905
    @billfujimoto5905 ปีที่แล้ว

    I'm a novice level programmer so I must admit I was confused by the functions 'power_speaker' and 'get_status' in iot_controller.py. These functions depend on the IOTFacade class which has methods of the same name which are called within the functions. At first it felt kinda circular but then I realized the functions are just wrappers around the IOTFacade methods. I'm slowly understanding this is part of decoupling the lower level functionality from the gui. I wish I had known about this a year ago... but it's never too late.

  • @mikeciul8599
    @mikeciul8599 ปีที่แล้ว

    I noticed that even in the facade, the power_speaker method creates the Connection and then immediately uses it. How would you test that? Where I work, we have a lot of code that looks like that - it creates a boto3 client and then immediately uses it. It's hell to test!

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

    I have a feeling that, using TDD, you would get to the Facade implementation without even thinking about it

  • @mikeciul8599
    @mikeciul8599 ปีที่แล้ว

    What's the difference between a facade and an adapter?

  • @tehdusto
    @tehdusto ปีที่แล้ว

    Anybody know this IDE that Arjan is using?

  • @user-rr6zh2iw8h
    @user-rr6zh2iw8h ปีที่แล้ว

    Controller in MVC isnt for business logic. Controller if for handling inputs and converting it for models or views. Business logic is a separate layer

  • @Naz-pk4ll
    @Naz-pk4ll ปีที่แล้ว +2

    haha you are a legend

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thanks Naz, happy you’re enjoying the content!

  • @darasingh503
    @darasingh503 ปีที่แล้ว

    You are grat sir i like you 😍 🥰

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

      Thanks Dara, Glad that you liked the videos.

  • @bertilhatt
    @bertilhatt ปีที่แล้ว

    I love the suggestion, but like many of your refactoring videos, it put things into different parts of the code - it’s all logical, but probably confusing for people without the context that there has been a refactoring. Would it make sense to include in your video an effort to *document* the changes?
    I think having a short introduction to what the service does would be great - so many repos don’t have that, inexplicably, and an architectural overview too.

  • @djl3009
    @djl3009 ปีที่แล้ว

    @6:01 " * Well... kinda. There's no consensus on what MVC is exactly. I'll do a detailed video about that soon!" ....would that be, a much welcomed, part 2 of your previous MVC video? -- th-cam.com/video/ihtIcGkTFBU/w-d-xo.html -- looking forward to it!

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

      It's going to be a different example, but I'll talk about not only MVC but also varieties like MVP and MVVM.

  • @tahashakibania3061
    @tahashakibania3061 ปีที่แล้ว

    Thanks for the video. But I have to be honest. halfway through the video I completely forgot what we were actually trying to achieve. I think you could explain all of these using a much simpler code example, in a much shorter video.

  • @OleZZ222
    @OleZZ222 ปีที่แล้ว

    Does anybody know the color theme he uses? Cant find any FAQ or something

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

      Isn't it standard VS Code? I have the same colors and didn't have to do anything special.

    • @OleZZ222
      @OleZZ222 ปีที่แล้ว

      @@rupen42 You're right! Thank you!

  • @julluj5624
    @julluj5624 ปีที่แล้ว

    Is this how real world projects looks like?

  • @tthompson5707
    @tthompson5707 ปีที่แล้ว

    Remember to use your overlord powers for good not evil :)

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

    Personally, this made the benefits of MVC obvious, but it was still hard to see the benefit of the facade. I think this is because you showed how you could change the controller without changing the gui, but there was no concrete example of what you’ve now decoupled with the facade. I’m sure if I watch it slower and think hard about your words then I would figure it out. But that’s not typically what I enjoy about your videos.
    Still a nice and lighthearted and fun experience. Just a little harder to learn from in my opinion.

  • @MorselHoarder
    @MorselHoarder ปีที่แล้ว

    5:40 The only things evil about you are your horrific dad jokes 😂

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

      It’s only going to get worse, haha 😂

  • @frogstud
    @frogstud ปีที่แล้ว

    You look like obi wan 😱

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

    The evil software development overlord on TH-cam is now working on developing iot. What's next? A python module for your hobby Terminator project?

  • @MJ-xl5jz
    @MJ-xl5jz ปีที่แล้ว

    I wish you provided shorter and faster examples...

  • @Antonio-yc2kx
    @Antonio-yc2kx ปีที่แล้ว

    😏 p̲r̲o̲m̲o̲s̲m̲

  • @ayhamsaffar8407
    @ayhamsaffar8407 ปีที่แล้ว +8

    PySimpleGUI >>>> TKinter