A perfect example of the meaningful application of the Factory Design Pattern. I have seen dozens of videos. This is the only one that clearly shows the benefits of the Factory Pattern.
Amazing guy! Good explantion! Me, as well, was watching a lot of videos and yours was the best and simple to understand. I already have applay this in a project here. Thanks!
Hi I’m learning design patterns I like your videos but can you possibly make a video of when I should maybe use factory method vs abstract. I understand there isn’t a simple answer but maybe a “Look for these signs answer”
Great video! I was watching one of your other videos explaining abstract factory methods in C#, but halfway through it was marked as private, so I didn't get to see the rest. Will it come back up?
The Factory method allows you to decouple creation logic from your client code. In this way, if you add, for example, a new payment method you only need to add a new class, e.g. ApplePayPayment and add support for it in your factory method. Your client code, which could be your main service code, can then stay exactly the same. In other words, it is closed for modification, but the Factory method makes it open for extension.
What do you do if CreditCard has a method GetStatement that returns a CreditCardStatementModel object, but GetStatement for paypal returns a PayPalStatementModel object with different fields? Since methods have the same name but a different return type, you couldn't implement a common interface.
Hi Kyle, you can introduce a base or abstract StatementModel. Then CreditCardStatementModel and PayPalStatementModel can inherit from or extend StatementModel. Then, let the GetStatement method return StatementModel, and through Polymorphism you can "cast" it back to the concrete types in the concrete classes.
In this case we can use abstract class instead of using an interface. which can have an abstract method named "Statement" which will return a statement. other classes like GooglePay, PayPal and CreditCard will override and implement the same "Statement" method but will return different statements. I hope, I explained it correctly.
The client code, however, remains unchanged and follows the open-closed principle. Many would argue that adding the enums and extending the factory method only, still remain within the boundaries of the open-closed principle. If you want to comply on all levels, you will have to use reflection, possibly compromising on performance.
A perfect example of the meaningful application of the Factory Design Pattern. I have seen dozens of videos. This is the only one that clearly shows the benefits of the Factory Pattern.
Thank you!
May be tons of videos/articles I watched, but this explains way simpler way. Great work 👍🏼
Thank you for the great feedback Unnat. This is our greatest goal, to simply complexity.
*for that I subscribed*
Amazing guy! Good explantion!
Me, as well, was watching a lot of videos and yours was the best and simple to understand.
I already have applay this in a project here.
Thanks!
Thank you for the awesome feedback. We love to simplify things.
Excellent tutorial .
Glad you liked it!
The best tutorial I have seen so far. cheers mate
Thank you!
Just simple and excellent. Please keep it up.
Thank you so much. Simplicity is our greatest goal!
Amazing
Thanks!
Just loved your explanation!! more energy to you
Thank you! The same to you!
Hi I’m learning design patterns I like your videos but can you possibly make a video of when I should maybe use factory method vs abstract. I understand there isn’t a simple answer but maybe a “Look for these signs answer”
Thanks for the request. I will consider it.
Great video! I was watching one of your other videos explaining abstract factory methods in C#, but halfway through it was marked as private, so I didn't get to see the rest. Will it come back up?
So simple . Thanks a lot !
Thanks Preslav!
Great video. What font are you using on vsCode?
I use the Material Theme on Linux Ubuntu 22.04.
What extension is he using to get that dropdown menu for creating new C# documents quickly?
DOTNET DEV KIT EXTENSION
And C# Extensions by JosKreativ
Thanks
You're welcome.
How does your code promotes open closed principle ?
The Factory method allows you to decouple creation logic from your client code. In this way, if you add, for example, a new payment method you only need to add a new class, e.g. ApplePayPayment and add support for it in your factory method. Your client code, which could be your main service code, can then stay exactly the same. In other words, it is closed for modification, but the Factory method makes it open for extension.
@@campbelltech Good answer! 👌
@@campbelltech wouldn't use also need to add a new enum called ApplePay and modify the client to call the factory with the new enum?
I think your method "create" should be "Create". Like your method "Pay", or like Console.Writeline.
What do you do if CreditCard has a method GetStatement that returns a CreditCardStatementModel object, but GetStatement for paypal returns a PayPalStatementModel object with different fields? Since methods have the same name but a different return type, you couldn't implement a common interface.
Hi Kyle, you can introduce a base or abstract StatementModel. Then CreditCardStatementModel and PayPalStatementModel can inherit from or extend StatementModel. Then, let the GetStatement method return StatementModel, and through Polymorphism you can "cast" it back to the concrete types in the concrete classes.
In this case we can use abstract class instead of using an interface. which can have an abstract method named "Statement" which will return a statement. other classes like GooglePay, PayPal and CreditCard will override and implement the same "Statement" method but will return different statements. I hope, I explained it correctly.
You violate the open/closed principle. Everytime u have to add a new payment method to the factory and enum.
The client code, however, remains unchanged and follows the open-closed principle. Many would argue that adding the enums and extending the factory method only, still remain within the boundaries of the open-closed principle. If you want to comply on all levels, you will have to use reflection, possibly compromising on performance.