Factory Method Design Pattern

Поділитися
Вставка
  • Опубліковано 5 чер 2024
  • #designpatterns #dotnet #gangoffour
    Let’s take a look at the Factory Method design Pattern. The Factory Method Design Pattern is one of the fundamental Creational Patterns catalogued in the famous Gang of Four Design Patterns: Elements of Reusable Object-Oriented Software book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
    It enables us to separate the code responsible for the creation of an object from where such objects are used. Specifically, we will have a factory method that will take data at runtime and decide which concrete type to produce, delegating such behavior to subclasses of an abstract creator class.Our example involves a program that creates and uses different types of objects that deliver food by implementing a common interface. Thanks to a factory method, our client code remains agnostic to the implementation details.
    In this series, we’ll be going through each of the patterns covered in the “Gang of Four” Design Patterns - Elements of Reusable Object-Oriented Software. Each video will use a real-world example, taking inspiration from food-based businesses. I’m including typical integrations in the form of mock databases, AMQP queues, email services, and loggers, as well as several features from C# 9.0. All of the code for this series is open source and available at the GitHub link below.
    Check out my flagship Udemy course - 12-hour full-stack app build with automation tests, Vue.js, .NET Core, and SQL
    ▬▬▬▬▬▬ 🎒 Udemy Course 🎒 ▬▬▬▬▬▬
    Check out my flagship Udemy course - 12-hour full-stack app build with automation tests, Vue.js, .NET Core, and SQL
    ►►► bit.ly/wesd-udemy
    ▬▬▬▬▬▬ 👋 Get in Touch! 👋 ▬▬▬▬▬▬
    Facebook Group ► bit.ly/productive-dev-fb
    My Twitter ► bit.ly/wesd-twitter
    My LinkedIn ► bit.ly/wesd-linkedin
    ▬▬▬▬▬▬ 🕘 T I M E S T A M P S 🕘 ▬▬▬▬▬▬
    0:00 - Introduction to the Factory Method
    2:03 - Motivation / Diagrams
    16:13 - Real-World Code
    ▬▬▬▬▬▬ 📚 TOPICS OVERVIEW 📚 ▬▬▬▬▬▬
    ☁️ Design Patterns ☁️
    ► Object Oriented Design
    ► Gang of Four
    ► Making Code Easy to Change
    ► OOP
    ► C#
    ► .NET 5.0
    ▬▬▬▬▬▬ 🔗 LINKS ▬▬▬▬▬▬
    ► GitHub repo for this project:
    github.com/wesdoyle/design-pa...
  • Наука та технологія

КОМЕНТАРІ • 40

  • @WesDoyle
    @WesDoyle  3 роки тому +15

    (Re-uploaded to fix audio!) What design patterns do you want to see next?

    • @philipeformagio8213
      @philipeformagio8213 3 роки тому +4

      I think the Observer Pattern would be a good call. It's not hard to understand and can be widely applied in many scenarios.

    • @nader.rajabi
      @nader.rajabi 3 роки тому +1

      Thanks Wes, Singleton would be great

    • @daretosucceed
      @daretosucceed 3 роки тому +1

      Please make it understandable for beginners too

    • @daretosucceed
      @daretosucceed 3 роки тому +2

      A little more explaination will do the trick

    • @diabader6251
      @diabader6251 3 роки тому +1

      We need event bus in the future plz

  • @Kadiros777
    @Kadiros777 2 роки тому +3

    So at the beginning I want to thank you for this tutorial, it helped me to understand this concept better BUT I still don't understand couple of things here.
    1. If you have to create kind of mapping for user input ("bicycle" => new BicycleDeliveryCreator(....) etc.) then you violate Open-Closed principle because in the future when new delivery types will be added then you have to modify this mapping.
    2. If we already have this switch case there why we just dont replace it with switch case which will create objects directly ("bicycle" => new Bicycle(...)). Later on we could also take this switch case which will directly create objects, move it to another class, call it DeliveryFactory and this will be Simple Factory Pattern. Then object creation is separated so we achieve our goal, we don't have redundant "creator/factory" classes and life is easier.
    3. What if I want to pass data to object contructor, let's say pass color of bicycle to Bicycle contructor. What do I have to do to acomplish this there? Is this possible?

  • @bogdanvasenshev6475
    @bogdanvasenshev6475 2 роки тому +1

    Thank you! That is the most interesting example i have seen so far

  • @kettenbach
    @kettenbach 3 роки тому +4

    Nice! I'm old school .NET 1.1-3.0, that C# 9 return switch statement syntax looked super clean. Makes me want to start writing C# again. 👍😀

    • @WesDoyle
      @WesDoyle  3 роки тому +1

      Nice, good to hear Chris! I like the way the language has evolved over the last few years. All the best and good luck!

  • @estebanbarajas6407
    @estebanbarajas6407 3 роки тому +1

    Awesome Wes, love the video as usual. Lots of great information! Keep them coming :)

  • @etmendz
    @etmendz 3 роки тому +2

    Absolutely a fan! GoF always relevant. Somewhere in your future could be a version of this series, almost exact, but with generics. Kudos! :-)

    • @WesDoyle
      @WesDoyle  3 роки тому

      That's a great idea! Thanks Edmundo

  • @stylesg7818
    @stylesg7818 2 роки тому +1

    Thank you. Liking this approach.

  • @sorinvasiliu
    @sorinvasiliu 2 роки тому +1

    Awesome explanation! Thank you for sharing gg!

  • @minimalistdeveloper
    @minimalistdeveloper 3 роки тому +1

    Thank you for fixing the audio. It is certainly night and day.

  • @sumeetbhanjadeo5994
    @sumeetbhanjadeo5994 3 роки тому

    Great video as usual. Just one question that build delivery creator static method in the client code will have to be updated for any new delivery type.......is that ok..or a violation of OCP

  • @THE_NE0
    @THE_NE0 2 роки тому +1

    Very beneficial !

  • @NoahNobody
    @NoahNobody Рік тому

    I don't want to alarm you, but I think someone has opened a portal to another dimension in the corner of your living room.

  • @athlentral-1491
    @athlentral-1491 2 роки тому

    Awesome work on these design pattern videos. I would assume that in real life scenarios, you would add your dependencies in .Net core DI container instead of the composition start place?

  • @alfonsocano574
    @alfonsocano574 Рік тому +1

    Quick question: the implementation of BuildDeliveryCreator is against solid open-close principle isn’t? For example, if you want to add Motorcycle you have to change this class instead of simple create the implementation for this new delivery method type.
    If what I said makes sense to you, Is there any way you can dynamically instantiate the class based on the input?
    Thanks for the explanation and example 🙌

    • @jonispatented
      @jonispatented Рік тому +1

      In a way, yes, it's against OCP, but somewhere, it *has* to be. When you make a new class, such as Motorcycle, you have to put the instantiation logic for it *somewhere*, or it will never be instantiated. The main purpose of the OCP is to reduce the number of places in your code that you have to add this logic to, ideally, down to just one place so that you don't forget it or miss it somewhere. The OCP can't possibly be followed at all times, which makes it the most difficult-to-grasp design principle of them all.

  • @kresnofatihimani5073
    @kresnofatihimani5073 2 роки тому

    what I noticed was factory pattern doesn't appear to support passing properties to the to-be-created object. If i want a different prop for the object, i would have to create a new factory that creates that object with that property. is this correct?

  • @diabader6251
    @diabader6251 3 роки тому +1

    Thanks

  • @kettenbach
    @kettenbach 3 роки тому +2

    Classic book. 😍🙏

    • @WesDoyle
      @WesDoyle  3 роки тому

      Indeed! Still a great resource

  • @vinguyen4257
    @vinguyen4257 3 роки тому +1

    Can you do singleton pattern next?

  • @jo0ls
    @jo0ls 3 роки тому

    I know all that, but not how you got the truck canopy to be red.

  • @hernanvelazquez1421
    @hernanvelazquez1421 2 роки тому +1

    Nice video, I think it would be better if you spend more time in how you were building the code (step by step design) from the beggining and spend less time in teory and talking. That will increase the video quality in my opinion, thanks!

    • @WesDoyle
      @WesDoyle  2 роки тому

      Perfect, thanks for the feedback. Thank you!

  • @mudassirhasan9022
    @mudassirhasan9022 3 роки тому

    Design pattern Interview questions have been nightmare for me

  • @pawelsalford6373
    @pawelsalford6373 3 роки тому

    Could show how to simply code it without This Design Pattern?

    • @wensleypodrono4824
      @wensleypodrono4824 2 роки тому

      I think this video is more about the design pattern rather than the code itself