Factory design pattern, Super class having multiple subclasses, create object of the subclass based on the input/requirement.
We create Factory class that take the responsibility of creating object of the subclass.
Super class will represent things so, you have to design it will be abstract class or interface.
We create Factory class that take the responsibility of creating object of the subclass.
Super class will represent things so, you have to design it will be abstract class or interface.
Things to consider before making object using client class.
1) Object should be created with loose coupling.
2) Create object based on input dynamically.
1) Object should be created with loose coupling.
2) Create object based on input dynamically.
Implement with code, here is the scenario.
1) We have Interface of Employee declaring a method salary.
2) Now make two subclasses names WebDeveloper, AndroidDeveloper that defining the salary method and return employee salary.
3) We have master abstract class named as FactoryEmployee having static method getEmployee that receive an string type argument. based on argument there is an if condition to create object of spacific class of WebDeveloper or AndroidDeveloper.
2) Now make two subclasses names WebDeveloper, AndroidDeveloper that defining the salary method and return employee salary.
3) We have master abstract class named as FactoryEmployee having static method getEmployee that receive an string type argument. based on argument there is an if condition to create object of spacific class of WebDeveloper or AndroidDeveloper.
4) Now We have an DeveloperClient class which want the salary information of the WebDeveloper or
DeveloperClient class. Now from DeveloperClient class method call FactoryEmployee class method getEmployee with argument let say "web developer".
DeveloperClient class. Now from DeveloperClient class method call FactoryEmployee class method getEmployee with argument let say "web developer".
5) Create Object
Now in FactoryEmplyee method there are if condition make on argument specific condition will true and return the new object of class WebDeveloper or AndroidDeveloper.
Benefits
No need to create objects(tightly coupled) only create based on requirement.
Focus on creating object for interface bcz we don't have concern with Employee(child classess/subclasses) classes. DeveloperClient class ask FactoryEmployee to give information of web developer class.
You just tel the factory class to create object, now further that his responsibility how he will create object.
Loose cpupling, robust code (No reasons to break code at run time.)
Real Time Application where this design pattern can be use.
1) Payment Method
User can be asked to choose payment method before making online payment transaction.
1) Payment Method
User can be asked to choose payment method before making online payment transaction.
2) Database connection.
Database connection can be establish based on type/nature of data you want to store.
Database connection can be establish based on type/nature of data you want to store.
3) Notification alert subscription.
User may ask to get notification via SMS, Email or via Push Notification.
User may ask to get notification via SMS, Email or via Push Notification.
4) Vehicle manufacturing system.
Bike, Cars, Truck manufectured in the company now you can get information of specific vehicle information.
Comments
Post a Comment