Factory Pattern - DESIGN PATTERNS (C#/.NET)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the factory pattern one of my favorite design patterns but what is it why should i use it and how do i implement it put simply the factory pattern is the idea of using a method to instantiate an object rather than a constructor which explains why this design pattern is labeled as a creational design pattern but why would you want to do this what's wrong with constructors well this factory method that creates an object can be wrapped in a class and if you wrap it in a class then you can leverage the benefits of polymorphism and pretty much swap out which type of object gets instantiated with only one line of code and we're going to demonstrate how that works in just a second it also helps us achieve the s in solid principles the single responsibility principle because if you have a class that needs to create some kind of object you can give it this factory rather than giving it all of the dependencies that it needs to create the object so all it does is take the factory create the object it doesn't need to know about anything else so let's head into our demo and see why this is so beneficial so here in my program.cs i have these two scripts here that get ran we have a create online store script and an update online store script let's take a look at those so in our create online store script we run this and we ask the user for the name of a store and then we create a store so as you can see in this case we're creating a basic online store with a constructor we're not using a factory let's first talk about the single responsibility principle so this create online store script let's think about its responsibility it gets the name of a store and it creates a store based on that it shouldn't need to know about what kind of store it's creating or the dependencies that the store needs all it needs to do is get the name of the store and create the store so ideally we would have some kind of factory that had a method called create online store and it would take the store name that it gets from the user it wouldn't need to know about all this it wouldn't need to take all this in the constructor the other issue we have is having this constructor here in the first place so if we wanted to change this to a different online store say in this case i have a fast online store let's do that we would have to actually change it here and if we want to change it here as you can see this fast online store takes different services so then we'd also have to change the services that we get into this create online script so so many things need to change in this class just to be able to change the online store that we create ideally if i wanted a different online store to be created here i wouldn't have to change anything i would just pass in a different factory which we are going to see in just a second but the last issue we have is that we also have this update online store script and as you can see this also creates a basic online store so if i wanted to change everything to a fast online store not only would i have to change this create online store script i would have to change the update online store scripts now i'm changing double the classes so ideally i would just want to change nothing so now we're ready to actually fix this with the factory pattern so let's go into our online stores folder we're going to add a new item and this is going to be the basic online store factory let's create that and we'll make it public and it's simply going to have one method on it and that's going to be our factory method so make that public it's going to return our i online store and we can just call this create online store and it's going to have some parameters now i want to talk about this because the parameters that we put on this factory method really depend on your client so in our case our client is giving us a store name so it makes sense for our basic online store factory to take in that name as the factory parameter but let's say our create online store script gave us back this payment service like we asked the user what kind of payment service would you want to use well in that case it would make sense for our basic online store factory to also take in an argument for that payment service but in our case our client is only providing us with a name so that's all we're going to take in this factory so that's one thing to keep in mind the parameters that you put on your factory method really depends on what the client is giving you so we're getting our name and now all we're going to do is return a new basic online store and we'll take that name but we also need these services and where are they going to come from well they're going to come from our constructors so we're going to get a payment service and a shipping service from our constructor and put those in the fields so let's actually generate those and i can highlight these and generate a constructor perfect so here's our factory very simple and i think that's the issue with this pattern it's just so simple but you really need to understand the benefits just by using the factory method so let's go ahead and update our scripts instead of relying on all of these services we can just take in our basic online store factory we'll call that the online store factory generate a field for that and then we can get rid of all of our services and now instead of this constructor down here all we need is our online store factory to create an online store with the name we get from the user so as you can see now our responsibilities are in place for the script we get the name from the user and we create the online store that's all this script needs to do it's all it needs to know so let me go ahead and actually update our update online store script right here just change these things around i'll just copy all of this and move it into here rename this constructor and there we go so now we're using our factory and now let me update my program.cs here so these don't take these services anymore they take the factory so let's create that factory right here and we can instantiate it with our services that we already have and then simply pass in our factory so that solves the issue regarding the single responsibility principle but what if i wanted this create online store factory method to give me back a fast online store that i have over here well in that case we're going to need to depend on an interface rather than this hard implementation of the basic online store factory and by doing that we can rely on polymorphism to create a fast online story instead by implementing this interface differently so let's see how we can do that let's go into our basic online store factory and we can extract an interface and we'll call this the i online store factory and let's take a look at that looks good so what i'm going to do is depend on this interface and all my scripts so instead of this hard implementation we'll do the i online store factory update that in both scripts and we can also update it where we assign this variable as well and this is an example of the dependency inversion principle another principle in solid because now we're depending on this interface so now if i want this create online story factory method to return a different online store all i have to do is implement a different version of this interface that will return the online store that i want so let's do that and the online store that we want is a fast online store and that's going to be our factory so let's implement that interface generate those methods we'll make this class public and now all i'm going to do in here is return a new fast online store this also takes our name and it takes a payment service a shipping service and this acceleration service so that our order can be fast so let's generate all these and generate a constructor for that and there we go looks perfect so back in my program.cs all i need to do to make these scripts make a fast online store is change this factory right here so we can change this to the fast online store factory and it's also going to need our order acceleration service let's get that and we can pass that in here so i enter a store name we look at our store it's a fast online store and now if i want this to be a basic online store just change the factory change the service that gets passed in and now i have my basic online store so that is the factory pattern simply using a method to instantiate an object rather than a constructor and this gives us flexibility by allowing us to use polymorphism through our factory abstraction and it also supports the single responsibility principle because the classes that use the factory no longer need to know about specific implementations and how to instantiate those implementations i will say one potential drawback to this pattern is that there is a little bit overhead in having to create all these factors so we had to add this interface and implement all of these factory classes but i still think this little bit of overhead is definitely outweighed by the flexibility and benefits from using this pattern now there is actually an alternative that i use sometimes to not have to create all these factors and i plan to show that off in a future episode but this tutorial is going in a little bit too long so i'm going to wrap it up here that is the factory pattern hopefully you guys learned something about this pattern and can use it to solve problems in your application and structure it more effectively thank you guys for watching if you have any questions criticisms or concerns be sure to leave them below in the comments section but other than that leave a like or subscribe for more thank you
Info
Channel: SingletonSean
Views: 5,796
Rating: undefined out of 5
Keywords: wpf, mvvm, easy, switch, views, visual, studio, binding, viewmodel, property, class, interface, learn, how to, architecture, pattern, new, content, main, programming, tutorial, full, stack, entity, model, access, object, .net, c#, service, layer, project, clean, data, store, source, error, handling, status, throw, catch, try, host, generic, dependency, injection, di, framework, add, remove, update, database, design, patterns, factory, method, uml, diagram, code, best, practices, gof, gang, of, four, better, solid, principle, single, responsibility, inversion
Id: TgCAN-lGEPo
Channel Id: undefined
Length: 9min 45sec (585 seconds)
Published: Sat Sep 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.