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... - Наука та технологія
(Re-uploaded to fix audio!) What design patterns do you want to see next?
I think the Observer Pattern would be a good call. It's not hard to understand and can be widely applied in many scenarios.
Thanks Wes, Singleton would be great
Please make it understandable for beginners too
A little more explaination will do the trick
We need event bus in the future plz
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?
Thank you! That is the most interesting example i have seen so far
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. 👍😀
Nice, good to hear Chris! I like the way the language has evolved over the last few years. All the best and good luck!
Awesome Wes, love the video as usual. Lots of great information! Keep them coming :)
Absolutely a fan! GoF always relevant. Somewhere in your future could be a version of this series, almost exact, but with generics. Kudos! :-)
That's a great idea! Thanks Edmundo
Thank you. Liking this approach.
Awesome explanation! Thank you for sharing gg!
Thank you for fixing the audio. It is certainly night and day.
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
Very beneficial !
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.
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?
yes
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 🙌
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.
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?
Thanks
No problem
Classic book. 😍🙏
Indeed! Still a great resource
Can you do singleton pattern next?
I know all that, but not how you got the truck canopy to be red.
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!
Perfect, thanks for the feedback. Thank you!
Design pattern Interview questions have been nightmare for me
Could show how to simply code it without This Design Pattern?
I think this video is more about the design pattern rather than the code itself