Factory Design Pattern in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is rupesh yadav and today we'll be learning factory design pattern using c++ this is widely used design pattern so let's get going these are the contents what is factory design pattern how to implement factory design pattern example code walk through which is in c++ and some advantages of factory design pattern so first of all what is design pattern, in software industries design pattern is a general reputable solution to a commonly occurring problem in software design what that means? lets thing that at some point you need a behavior that only one object should exist in whole system so there is a famous design pattern for that and it's called single so if you know what is your problem and there is a predefined and well tested solution to that problem you can straight away go to that solution ok so basically there are three types of design patterns first is creational design pattern there are six type of creational pattern second is behavioral design patent third one is structural design pattern so let's go ahead so these are some points about design pattern ok they give the developer a selection of tried and tested solution to work with ok there are tested solution like I said singleton design pattern it is well tested and it is well documented that what this design pattern does what you can achieve with this design pattern so in order to implement some design pattern you know that ok this design parent can do this and it this cannot do all this stuff so you know that ok what this is design pattern can do ok that's that that's this point they are language neutral and so you can be so it can be applied to any language that support object-oriented it's pretty straightforward they have a proven track record as they're already widely used and thus reduce the technical risk to the project so if suppose you're implementing some design pattern and you know that ok these are the pros and cons of this design pattern so and let's say you are implementing some design patterns say one and there is some problem with the design pattern because you will be implementing some design pattern or some strategy which it won't be able to interact with OR there is some problem to connect these two or something like that so you can track in starting phase that ok I shouldn't implement this isn't pattern or not so this is where it comes handy they are highly flexible and can be used in practically any type of application or domain so i'll move forward now so coming back to our topic factory design pattern FDP it creates object for you rather than you initiating object directly so what that means is you can just call one function that will behave like a factory function and you will tell that function that what kind of object i want in terms of anything like you can pass a string that string will get decipher and what this string is telling accordingly that function will return the object you can just simply pass integer i have used integer in my example second point is factory design pattern is also known as virtual constructor but in c++ we don't have virtual constructor and ok i'll tell you why it's not there so in order to call virtual function you should have object on that object only you call someone virtual function ok and if you want constructors as virtual then you need an object to call constructors but we do have that so that's the reason we don't have virtual constructors in c++ ok so we move forward yeah so how to implement factory design pattern it's very simple just define an interface on abstract class for creating an object but let the sub-class decide which class to initiate ok I think confused ok i'll explain this with the help of this diagram and the code so as you can see we have this abstract class called Toy we are inheriting this into Car, Bike and plane and we are using these objects in Toy Factory I mean to say will be returning either of these three toys in when will be passing the type and here is our client ok FDP demo this is the client this is our main function will be calling this factory function from our main and we will pass what the type we want and this function will return the toy in terms of car bike or plane ok these are some functions like prepare the parts and combine parts assemble parts apply label and show product ok so we can directly go to the code now ok yeah so as you can see it is our client code i have used while loop and have taken type as an integer user will give integer as a type like 1 2 3 4 and the system will generate object according to enter type OR 0 for exit so this message will pop up and user will enter the type okay that could be lots of ways i have used this way so and this is the main thing who ok so this is a function where we are passing this type and this function is returning the type object it's that simple guys so let's look at the definition of this function this is the function this is the factory method we can say ok so this type is coming in this function we are checking what type it is we have taken temporary toy variable we are populating that accordingly and then and there is a default it is not either of these three so now once we have populated it we will generate that object or we will do the operation whatever is required to generate that object and will return that object once we returned object we will get to the client for again now we will check if it is null that means we didn't get the object if it is not null then we'll show the product and we will delete that i am deleting it just because we are doing this is iteration ok so now let's look at the object fine this is the object of class hierarchy but I showed you in the that video in that diagram now will run this project and I'll and type 1 then we'll get the car parts and two then bike parts and 3 for plane parts and let's say we are giving 4 then that is invalid part so as you can see that it is preparing at run-time we have not be defined anything like we'll be preparing one car part one bike part or one plane part OR anything which is a static we are doing this dynamically as you can see i am creating object at run-time not at compile time ok so this is the beauty of this design pattern we can create objects at run-time okay so this is the advantages so less code changes if we change object creation process what that means if we are changing the object creation process like I should go back here this is object creation these are the function with the help of these functions only this object will get created ok you will combine parts prepare the parts assemble the parts so this is the process ok which is required to complete this car object so once this object is ready you can just return back so here as you can see i have written the steps in this create toy factory method so if you want to change something you have to change here only let's say you you think just for an example let's say you want to put that here or assemble part or combined part ok or you want to do some preprocessing here ok so that you can do so that support that point we create object without exposing creation allows you to the client yeah that is also true because we are not showing anything this as you can see the client is just telling that I want this object this type subject and the client is getting that object client doesn't know anything about the creation of that object so that is also the beauty of this design pattern and last point is we get the benefit of virtual constructor obviously we get the benefit of the virtual constructor we get object creation at runtime okay yeah yeah thank you guys thank you for watching and please like my video if you like this and subscribe to my channel and that would help you to get updated and if i'm uploading any video you will get notification for that
Info
Channel: CppNuts
Views: 86,988
Rating: 4.6539164 out of 5
Keywords: factory design pattern, design patterns, factory pattern, design pattern, software engineering, design, software, coding, c++ interview questions, jobs, pattern, factory, software interview questions, java, c++, programming, code, design pattern in c++, college, google, microsoft, student, freshers
Id: XyNWEWUSa5E
Channel Id: undefined
Length: 10min 55sec (655 seconds)
Published: Thu Jan 05 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.