Factory Design Pattern
Factory Design Pattern works as per it's name. As one Factory can generate various types of items but of a specific category. Like, Car Factory ( Nissan Car Factory ) can create cars like, Micra, Sunny, Evalia but it can't create Duster. Because,Duster can be created by Renault Car Factory.
So, with the same concept, A company can create a various types of Employees. Like, Developer, Admin , Developer Manager. But, only Developer can be Dev Manager.
Here is the Complete example of creating the Company with all the Employee details.
Class Company.java
/** * Parent Class of all Class. Employee is Base class of all types of Employee. * * @author Hari * */ abstract class Employee { public abstract void getMyDetails(); } /** * Admin is child class of the Employee class. * * @author Hari */ class Admin extends Employee { public void getMyDetails() { System.out.println("I'm admin"); } } /** * Developer is another child class of Employee Class * * @author Hari */ class Developer extends Employee { public void getMyDetails() { System.out.println("I'm Developer"); } } /** * DevManager is the Class derived from Developer.s * * @author Hari * */ class DevManager extends Developer { public void getMyDetails() { super.getMyDetails(); System.out.println("I'm Manager"); } } /** * Enum Type id defined to get the argument as specific type of the class * * @author Hari * */ enum EmployeeType { ADMIN, DEVELOPER, DEV_MANAGER } /** * Company Class is Factory. Company can create any type of the Employee. * * @author Hari * */ public class Company { /** * Method to create a Employee. * * @param employeeType * @return */ public static Employee hireEmployee(EmployeeType employeeType) { Employee e = null; switch (employeeType) { case ADMIN: e = new Admin(); break; case DEVELOPER: e = new Developer(); break; case DEV_MANAGER: e = new DevManager(); break; } return e; } }
Comments
Post a Comment