Factory Design Pattern - Advanced Python Tutorial #7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back to the python tutorial series for advanced programmers in today's video we're going to talk about the factory design pattern so let us get right into it so the factory design pattern is just one of many object-oriented design patterns and the goal of such an object-oriented design pattern is usually to just increase the modularity to increase the so-called separation of concerns which basically means that we want to have the individual pieces of the program the individual classes the individual modules mind their own business do their own job and be very good at that job and the factory design pattern is one such pattern that allows us to do that and it's very useful when we want to decide during runtime what particular instance we want to create so let's say we have for example an abstract person interface an abstract person class and then we want to have students and teachers and they're both persons obviously but we don't want to decide in the code i want to create a teacher or i want to create a student we want to make it dynamically during runtime so i know i want to create a person object but i don't know while i'm writing the code if i want to have a student here or a teacher we want to decide during runtime and if we want to do that we can use the factory pattern to do that but one thing that you need to keep in mind here is that those design patterns are usual are usually just useful if you use them in larger projects so you don't want to use them in your 50 line python script you want to use them in big applications where you have multiple classes and multiple python files and modules working together because otherwise they're just increasing this is a the con side of of such a design pattern they're increasing complexity they're decreasing readability they're making the code more difficult to understand and this is a large price to pay if you don't get anything out of it so let's just look at at an example here let's say we have an abstract class person so for this we need to say from abc which stands for abstract class import abc meta and abstract abstract static method there you go and now we're going to create a person interface so we're going to say class person and one notation that is best practice or the convention is if something is an interface because in python we cannot just say interface person if something is an interface we call it i whatever so i person in this case and here we're going to say meta equals abc meta which basically means that this is an abstract class we cannot create any instances of it we're going to see what that means in a second and then we're going to say abstract static method and we can say for example deaf person method this is just a basic method that every person has to implement of course this is just an abstract example here we're not you could you could say for example this is the person aging or the person studying or the person doing something this is just a basic function and here we're just going to add a comment that this is an interface method because the interface uh the what an interface basically is is just a definition of the signature so we know okay we're going to have those functions those methods but we're not going to say what they actually do so this is just mentioning that each person has to implement the person method so we're going to see that this doesn't work because if i now say p1 equals i person for example like that and then p1 dot person method or something um now i write this we're going to see that this is not possible uh because it's an abstract class we cannot just go ahead and uh actually one thing i made a mistake here mata class is abc meta then it's not going to work uh this basically means that we're creating abstract class and the definition of an abstract class is that we cannot create instances of it so uh you're going to see here that we get the error abstract class i person with abstract methods we cannot instantiate this and we're going to see the terminal if i now go ahead and run python oh what's happening here uh okay i i used to paste here let me just clear this real quick there you go python3 main.py you can see i can't instantiate the abstract class i person um so that's exactly what we want actually and now we're going to have another class student which is going to uh to inherit from this iperson interface is going to implement that iperson interface and we're going to override this method now if i don't do that if i just say i don't know def uh init and i have a basic constructor here with i don't know self dot name equals basic student name it's a default name then if i try to say s1 equals student and or actually just that we're not going to be able to create an instance of student because student did not overwrite the abstract static method person method we need to do that otherwise we cannot treat this as a non-abstract class so after the constructor we're going to say def person method with a self keyword here actually do we need to self keyword yeah i mean we don't need it i'm not sure if we need it but we're going to just say print i am a student like that so this is the basic student class which is also a person and then we're going to have another one which is a teacher and it's also going to be a person and we're going to also have an init method so a constructor basically self.name equals basic teacher name and then we have person method self again and now we say print i am a teacher this is basically just for us to see uh if the object is a teacher or a student and now what we can do here of course is in python we can just go ahead uh and create those objects by saying s1 equals student student and then i can say s1 person method and here i can say t1 equals teacher t1 dot person method so we can now go into the terminal here and run this again you can see i'm a student i'm a teacher so that's a very basic stuff what we now want to have is we want to decide during runtime uh we know that we want to create a person method a person class sorry a person object but we don't know if this object shall be a student or a teacher and we want to decide that dynamically during runtime so one way to do that this is the the not so beautiful way to do it is to just say i don't know choice equals input type or something and then we can just say okay if the choice is student then just go ahead and create a student and so on but that's not the factory design pattern the factory design pattern is to now have um a a factory class which actually builds person objects so we actually want to say class person factory and this person factory is going to build the person that we want to build so we're going to say death build person or get person or produced person call it whatever you want i'm not sure what the best practice definition or the best practice name here is and what we're going to pass here is the type so we're going to pass the type of the person by saying person type and this is in our case just going to be a string and by the way this of course is a static method so we're going to say build person and what we're going to do here is we're just going to say okay if the person type is student then we're going to return a new student then if the person type is teacher we're going to return a new teacher object and if none of those two happen we're going to have an invalid type so we we can either raise an exception here we can just say something like which is not best practice obviously we can say invalid type and for example we could return negative one this is what we could do we can also uh come on we can also uh have an exception here we can also throw an exception or raise an exception and say okay this was an invalid type so handle this exception whatever i'm going to do it just like that here and now we can do is we can actually go ahead and create this main section here and we can say uh that we can have the choice again so we can say choice input uh what type of person do you want to create and backslash n here um and then we can just create the person depending on that input so we can actually go ahead and say um person sorry person factory dot build person depending on the choice and then once we have that person actually we need to save it in an object here so we need to say person equals that and then we can say person dot person method and you're going to see that if we now run this i can enter student for example and i'm going to get i'm a student i can enter teacher and get i'm getting i'm a teacher and i can enter something else and we're going to get invalid type and an attribute error and so on but as you can see we can dynamically decide if we want to build a student or a teacher during runtime so we don't have to know in advance we can do it on the fly and the factory pattern is very useful if we want to do this quite often because then we can just reuse this factory we can just say factory build person and then pass the type so that's it for this video if you enjoyed i hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 18,091
Rating: undefined out of 5
Keywords: python, programming, advanced, tutorial, coding, professional, expert, level, software engineering, python expert, python advanced tutorial, python advanced, object-oriented, intermediate, design, pattern, design pattern, factory design pattern, factory, method
Id: -a1PFtooGo4
Channel Id: undefined
Length: 11min 23sec (683 seconds)
Published: Mon Feb 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.