Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
- The example provides details on the Employee and Department in a company using Nodejs.
- Department and Employee class has ID and Name as the attributes.
- To create new Employee and Department two classes are initialized EmployeeFactory and DepartmentFactory.
- We are making use of concrete classes EmployeeFactory and DepartmentFactory which return the common object of Company. Since Abstract class does not exists in javascript we make use of the above classes replicating Company interface.
- A common FactoryProvider class is used based on the client inputs and respective factory classes are initialized.
- The result provides the department ID and name along with the Employee Id and name without digging in their concrete classes.
app.js Department.js DepartmentFactory.js Employee.js EmployeeFactory.js FactoryProvider.js
- The client is independent of how we create and compose the objects in the system
- The system consists of multiple families of objects which are designed to be used together.
- We need a run-time value to construct a particular dependency