Factory Method Design Pattern | C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to tech club dots today we are going to discuss what is factory method design pattern and there are many important features that i am going to discuss today in this and one of the important part is i will also discuss how we can implement the open close principle of solid with the help of factory method design pattern so let's move ahead without wasting time to check out the agenda that what points we are going to cover in this so we will cover what factory method design pattern is and why factory method design pattern is important to understand before abstract factory design pattern which is going to be the next session okay and this is continuation part before this we have already discussed what is simple factory design pattern okay and uh for this i will also cover the use case of factory method design pattern including its advantages and code coverage i will also demonstrate one of the very special case of factory method design pattern and basic understanding of object oriented programming will be very helpful i strongly suggest you i strongly suggest you to watch the types of design pattern and simple factory design pattern and the link is given in the description of this video so as we have already covered simple factory in our previous session so for now we will discuss factory method design pattern and yes the pros and cons of the simple factory definitely really matters to understand factory method design pattern these three patterns are related but simple factory pattern does not likely fall into gang of four design catalog so we started with the simple factory one and in today's session we will discuss factory method design pattern okay so let's move ahead without wasting time so i'm going to discuss the very very important points only that you should not miss let's cover these points quickly so definition says an interface for creating an object but let sub classes decide which class to instantiate this is a very important point and i am going to repeat this sentence again and again during the session factory method pattern lets a class differ instantiation to sub classes okay and don't worry if these points are not clear in the and the practical part just after few minutes you will see how all those things are working in a practical way if i talk about the real example in an application you may have different databases users and one user may use oracle and other user may use sql server so whenever you need to insert a data into your database you need to create either a connection of sql connection or an oracle connection and only then you can proceed you will agree with me if you put the code into if else or switch statement then you need to repeat a lot which is not easily maintainable and obviously not comes under best practices so you need to support a new type of connection with some modifications so this type of problems can be easily resolved with the help of factory method pattern and by providing the abstract creator class to define the basic structure that is very helpful and as for the definition the instantiation process will be carried out through the subclasses only and that derives from this abstract class don't worry i will showcase you where we are using abstract classes and all its features so the and one more important point is the factory method pattern allows a client to get the object without specifying the class name okay so client will not specify the directly class name it is a job of factory method to create an object of the intended class and return it to the client and in the real world example the decision to instantiate a particular family of object may depend on some configuration settings or some business logic or some programmatic conditions okay so let's move ahead to check what it really looks like so in the previous part i'm just giving a brief introduction so in simple factory design pattern what we have seen we have i simple factory which is abstract class and this abstract class implemented by simplefactory which has a method create animal okay we implemented this method and on the other hand we have this hierarchy where we have one animal you can say i animal interface which was implemented by tiger and dog classes okay and the common actions are sp and action and this is a client one that has main method which was consuming the feature of this simple factory but now how it's converted into factory method and or you can say how it is related with a factory method let's have a look how it's converted into new model so as you can see now instead of simple factory we are using i animal factory so this is our main factory and further you can see these two are the concrete factories we call them concrete factories dog factory is here and tiger factories here and both have its own implementation of create animal and create animal okay so this is one hierarchy and on the other side another hierarchy is the same as we have in simple factory design pattern okay so we have animal we have dawg we have classes we have common actions and again we have the client so this for the classification of concrete factories came here in fact design pattern so i'm just taking example of animal but you can relate it with any of the real world example that i've just shown you the database example one or you can relate it with the you know if you are if your subject is related to manufacturing in industry you can say car so where we have a client and we have a one car factory that is responsible for creating saroon car these are the concrete factories and the hatchback car is another concrete factory okay and on the other hand another another hierarchy is we have i car and that is further implemented by saloon car and hatchback car so common features can be start engine and check gasoline so these two are the common methods that can have so i'm just displaying it just you can relate with any of the subjects that we used in the real world okay so so what actually we need to do i mean if we compare it with the simple factory design pattern so we will create our new animal factory okay so this is the make animal this one is the special beautification feature don't worry i will discuss this one at this one as well but the main point of animal uh factory is its abstract method which is responsible for creating animal okay and it will be implemented by dog factory as well which is a concrete factory and a tiger factory as well which is another concrete factory now it's your time let me switch to visual studio to demonstrate in a practical way how it is actually working okay this is my visual studio and this is the same solution on which i was working in my last session with simple factory pattern so as you can see i have created one folder in this this is a asp.net core web application okay and what i am doing here in this folder i have created all the related classes of simple factory uh pattern and don't worry the source code link is given in the description of this of this video i strongly recommend you to take that code and do as much experiment that you can okay so what we did earlier we have animal interface similarly that can speak an action these two features of animal and we have further classification of dog and for the classification of tiger and both are implemented i animal this point both have a common uh background we have animal dog and tiger but now further in earlier part when i was using a simple factory so you can see we specify one abstract method which was create animal in that we are passing one index and that index is responsible for what type of object we want this is a part of simple factory implementation so if you pass zero it was returning dog object if you pass one it was returning tiger object but now come to the factory method one so we replace the simple factory with animal factory okay and instead of using a one factory creating a further classified factory or you can say concrete factories dog and tiger so let's have a look on those so this is my animal factory again we have the same method great animal now we are not passing any argument in it okay and that's the beauty of the abstract method and further in dog factory which is a concrete factory in this we are implementing i animal factory and yes we have one method that we need to implement so we are creating here create animal and now this method is returning the dog object and in the similar fashion in tiger factory you can see we are implementing i animal factory and we are giving the implementation for create animal and we are retaining tiger object okay so this is a work of factory to return the required object okay now come to the startup part from where we will call all these things okay so let's come here so as you can see what we are doing to calling all this stuff so we are simply writing a text using just this piece of code to write the output to the response okay so i'm writing simple text okay this is a factory demo and then what i am doing here i am creating a one animal factory type of variable and in that i am instantiating my dog factory okay now dog factory is created then further what i am doing here i am creating a animal which is my concrete product of my animal factory okay and now in it what i am calling i am calling dog factory dot great animal okay this is my dog factory i am calling create animal and definitely i will get the new instance of dog that's it yes so we have a dog and now we can easily get the features of speed and action okay so let me run the solution how it's working as of now now you can see the output of our program so this is the factory method demo and in output we can see the effective method can create only dog animals dog animal created with its features and dog says walk and dogs before barking okay so this is the expected output and all this is coming from the factory okay let me show you so we haven't passed anything it's all the features of our dog factory which is creating the dog instance and in dog we are mentioning here the dog says this and dog prefers barking now let's move to the beautification part the important stuff that i want to show you is how a animal abstract factory can have some default features let's suppose if we have a car factory and we want to have some common features for example start engine and check gasoline okay in the same way we are saying if we are creating any animal then there should be some common actions that can be either speak or they can perform some action okay so if you want to have that implementation in our abstract factory itself so that will be very helpful so all the child classes which are going to uh implement their own actions they can do that but there are some parent rules that they cannot ignore because if you are driving a car then start engine and check gasoline is the prior requirement in the same way when we are creating animal speak and action are the primary requirements so let me uncomment this one so this is a very important piece of code here so you can see this is a simple i'm just trying to write on the response that's why i use this way in this method make animal what i'm doing here just writing it on to the response and you can read these three lines these are very important at this point it doesn't know whether it will be a dog a tiger okay it will be decided by the subclasses for example dog factory or tiger factory that we have already implemented but it knows it will speak and it will have preferred way of action okay so what we are doing here we are creating a one animal type variable and in that we are assigning create animal okay and we know i animal have two common two methods one is speak and actions that we are calling okay and everything will depend on what type of object we are calling and on the basis of that the required action we will see on our response so that's a beauty and you will see in the output of this window and let me make some changes on a startup as well you can see now we are creating a dog factory that is fine but now we are manually calling the speak and actions okay but i want a parent class should have control on this the s with the abstract features so let me uh comment these three lines of code okay and let me uncomment this beautification part which is nothing but the part where instead of calling directly create animal now i am calling a make animal okay so just in this way i just commented that one as well so we need factory instance and now i'm calling make animal see here we are not giving any class name okay so we are just saying dog factory will provide a dog animal so we can consider it if we have a table one is from iron and one is from wood so wood factory will provide the wooden table and the iron factory will provide the iron table okay so we have just uncommented this one we know the function of make animal so let me save the changes okay just keep this point here now we are not calling speak an action okay it will work automatically so let me run this application here we go now we can see dog factory can create only dog animals this is a beautification part and we can see i animal factory which is calling make animal you cannot ignore the parent rule okay and now we are not calling the speak or action but by default it is a part of factory that you will get okay so that's what the really really uh shining part of factory method pattern okay so let me close this and go back to the presentation because if these points are still not clear and you are thinking there are some similarities with simple factory design pattern so let me give you the clarification so let's go back to the presentation now okay so let's move ahead from here to check that further so we have seen the beautify work so what we have already seen in the modified implementation more reflective flexibilities are added and notice i animal effective class is an abstract class so you can take the advantage of using the abstract class you can provide more implementation it's according to your need and business logic that you can apply so suppose you want a subclass to follow the rule that can impose from its parent or base class so that we have seen by implementing this way okay so let's move ahead to so i have prepared few of the question may be that can solve many of your queries that you already have so let's pick the one so what are the advantages of using a factory and factory method like this so answer to your question is because you are separating the code that varies from code that does not vary this is very important right it looks simple but this is very important line because in other words advantage of using the simple factory design pattern are still present that's why i said these both are related simple factory and factory method this helps you to maintain the code easily and the code is not tightly coupled this is a very important point from the best practices point of view so you can add new classes such as login beer and so on at any time in the system without modifying the existing architecture that's a very plus point in other words i have followed the oh install it which is closed for modification but open for extension you have already seen the implementation you can take the code from github repo okay now we are not modifying but we can extend it by specify different type of animals as well now the next question is uh you should always mark the factory method with an abstract keyword so the subclass can implement them so is that understanding correct so i will say that answer is different no sometimes you may be interested in a default factory method if the creator has no classes okay and in that case definitely you cannot mark the factory method with an abstract keyword okay so i hope it clarifies and another question is it still appears to you or i mean the factory method pattern is not much different from the simple factory pattern is that understanding correct so let me tell you the key aim of factory method pattern is it is supplying you with the framework through which different subclasses can make different products okay that's a very important line and in the case of simple factory pattern you cannot vary the products in similar manner i mean to make this point more clear i strongly recommend you to watch the previous video of simple factory design pattern you can think of a simple factory pattern as one time deal but most important your creational part will not be closed for modification okay whenever you want to add something new you need to add an fs block or switch statement in the factory class of your simple factory pattern okay so that's all the points that i really want to share with you related to factory method and factory patterns so i hope you like this video if you have any suggestion if you still have any question just leave into the comment box i will reply on that as soon as possible and definitely your feedback is the only part that inspire me to create such more videos and i will see you in the next session don't forget to like and subscribe and share with your circle bye you
Info
Channel: TechWebDots
Views: 685
Rating: undefined out of 5
Keywords: Factory Method C#, Factory Method Design Pattern C#, Factory Design Patterns C#
Id: 5RrR6MaimT0
Channel Id: undefined
Length: 19min 24sec (1164 seconds)
Published: Sat May 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.