Factory, Abstract Factory, Factory Method - Design Pattern (C#)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everybody today we're going to be talking about factory patterns factory pattern abstract factory and factory method generally all factory patterns aim to do one single thing it is to take a creation process of something wherever you have it located just from all those places take it and put it into one single place where you can control how stuff gets created there are different ways to do this for different scenarios today i'm going to give you a couple of examples and generally it shouldn't matter too much differences between factory and abstract factory and factory method as long as you understand the core concept okay so let's dive in and take a look imagine we have a navigation bar like on the website and then we have a drop down menu all right in both the navigation bar we can have a button and on the drop down menu we can have a button right imagine any application this could be a mobile application this could be a desktop application the point is there the same button is in two places okay so here we have the situation all right now let's blow it up we have more than just a navigation bar more than just a drop menu we have a hundred thousands of places where this button may be located at the moment we have two default buttons and if we want to change our default button to something else like a red button we would have to you know go ahead and change it everywhere would have to go through a thousand places and change it this is a nightmare this is not what you want to be doing this is for example a standard factory example we take the process of creation we put it inside a factory which is just going to return a button we no longer need to do this we take the button factory and we just go ahead and create a button now instead of changing it in a thousand places we only need to change it in one place okay we're on it and we got the red button so if you're ever looking or for like a symptom when when do i need to use a factory pattern be it whatever whichever one you want to use of the ones that we're gonna go over it is generally am i creating something in two places identically if so bring it in one place all right in this case the button is internal we don't need anything from outside controlling how it looks like just keep it in-house doesn't matter usually this could be like a helper method sort of thing in your application okay the simplest thing possible all right let's move to the abstract factory okay same scenario navigation bar drop down menu we are rendering these for specifically or an apple phone all right so we take the factory and we create a button okay so for both of these on the navigation bar in the drop down menu we have an ios button rendered now i do have my apple class already inheriting from ui factory and this is just because i've set up the example like that but imagine we didn't have something like that okay and this was just a button factory okay so what you want to do in order to allow something externally to control how your application looks like for example if a very popular one is dark mode and light mode right it's just two different color schemes if we could basically have a theme interface and then light mode and dark mode we just basically switch between the two implementations and all elements change colors all right same principle here here we're only confined to the apple example we go ahead and give it an interface now we're no longer confined to just the apple example although we can still use it we can then just go ahead grab the android implementation and put it in there right so this the abstract factory is again we sort of took everything that we could we segregated it right but because in the factory example it was only used internally and we don't need something externally to control how our application works we keep it in-house now this is where we need to offset or abstract how we want to create these things this concept is also known as dependency inversion this is where we define what we need but the thing that is going to do the actual creation is the thing that is supplied okay so we say that we have a theme this is the template for the theme and the actual implementations for the themes are all different right and i can have different colors set and they can just be like sort of thrown into onto your color palette with different colors in there and you can pick from them okay so that kind of thing is going on here we're injecting think about injection think about composition dependency inversion okay let's take a look at the factory method now this one was a little bit hard for me to grasp at first because i've never actually used this consciously all right we will come back and look at the abstract factory and see how factory methods are actually used there although again as i said in the beginning it doesn't matter so much if you exactly know which factory pattern you're using understand if you're just getting getting rid of duplication and giving more structure to your code by making sure the way that you create objects is in in an organized place you're kind of already doing it so in this example we have a navigation bar we have a drop down menu we run the both and we both we get default button from both of them now we have an abstract element where we have the factory method create button right so it's just an abstract class that we can implement okay so this is going to be quite important we have an abstract thing that we can implement just a method this means we can have multiples of them each individual one of these is a factory method okay we then have the navigation bar and we have the drop down menu these are our building blocks with which we can organize this thing so uh what can we do further to sort of enhance or basically how do we use our building blocks in this case well here what we can do is we can basically do something like this let's say android navigation bar android drop down line up and then have android in these two places and now we have essentially four objects to pick from and there we go so if you think about it we have added tools to our arsenal and instead of injection we are now using inheritance to enforce the structure of cree of having a button created for these elements now to kind of break down the difference between inheritance and injection composition and still inheritance right these two areas and when do you want to use which one composition is ultimately flexibility and inheritance is structure you need to balance these two in order to get software if you go full composition you just have a set of random things flying around which you have to put together you basically end up having infinite utility if you have if you go full structure you're rigid you can't change so generally you want to balance two things together now to come back to the abstract factory example where we are indeed using a factory method in the abstract factory it is on our interface we have a factory method that we want to overwrite right and that's essentially it factor factory method is a factory method if we have multiple factory methods on an abstract factory that is still a factory method a simple factory that just creates something that you just need in your code base as a utility you can just use that right so hopefully you understand this concept a little better and the different ways that you can basically grab the way that you create things and aggregate it in one place be it with a factory internally with an abstract factory injected externally or in the last example where i showed the use of factory methods where you enforce structure as well as defer or abstract the creation of the thing you need whoa hold up look this is the stuff i used to torture myself on the weekends now it takes time to digest this and package it up into these videos so if you did enjoy the content please like subscribe if you want to see more i'll leave a comment if you have any questions and if you want to be part of the community that i'm building make sure to join my discord server i also stream on twitch wednesdays and sundays six six o'clock london time i have also opened up a merch store so if you do want to support me don't just donate buy from there links to all of that and my other social media are in the description hope to see you again and have a good day
Info
Channel: Raw Coding
Views: 100,339
Rating: undefined out of 5
Keywords: csharp, factory, abstract factory, factory method, design pattern, tutorial, explaned, example, oop, c#
Id: xN7EFHU_rXA
Channel Id: undefined
Length: 9min 28sec (568 seconds)
Published: Wed Mar 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.