Abstract Factory - Design Pattern
Creational: Abstract Factory Pattern
Can you explain the abstract factory design pattern?
View Answer:
Interview Response: It's also a good idea to be familiar with the Abstract Factory design. Which seeks to encapsulate a set of independent factories with a similar purpose, and it separates the details of implementing a set of objects from their general usage.
Diagram:


The objects participating in this pattern are:
AbstractFactory -- not used in JavaScript
- declares an interface for creating products
ConcreteFactory -- In example code: EmployeeFactory, VendorFactory
- a factory object that manufactures new products
- the create() method returns new products
Products -- In example code: Employee, Vendor
- the factory-created product instances
AbstractProduct -- not used in JavaScript
- declares an interface for the created products
note
Though the definition particularly mentions that an interface needs to be defined, we don’t have interfaces in Vanilla JavaScript. Therefore, we must implement it in a way that JavaScript translates into an interface.
Code Example #1:
function Employee(name) {
this.name = name;
this.say = function () {
console.log('I am employee ' + name);
};
}
function EmployeeFactory() {
this.create = function (name) {
return new Employee(name);
};
}
function Vendor(name) {
this.name = name;
this.say = function () {
console.log('I am vendor ' + name);
};
}
function VendorFactory() {
this.create = function (name) {
return new Vendor(name);
};
}
function run() {
let persons = [];
let employeeFactory = new EmployeeFactory();
let vendorFactory = new VendorFactory();
persons.push(employeeFactory.create('Joan DiSilva'));
persons.push(employeeFactory.create("Tim O'Neill"));
persons.push(vendorFactory.create('Gerald Watson'));
persons.push(vendorFactory.create('Nicole McNight'));
for (let i = 0, len = persons.length; i < len; i++) {
persons[i].say();
}
}
run();
/*
OUTPUT:
I am employee Joan DiSilva
I am employee Tim O'Neill
I am vendor Gerald Watson
I am vendor Nicole McNight
*/
Code Example #2:
function Car() {
this.name = 'Car';
this.wheels = 4;
}
function Tractor() {
this.name = 'Tractor';
this.wheels = 4;
}
function Bike() {
this.name = 'Bike';
this.wheels = 2;
}
const vehicleFactory = {
createVehicle: function (type) {
switch (type.toLowerCase()) {
case 'car':
return new Car();
case 'tractor':
return new Tractor();
case 'bike':
return new Bike();
default:
return null;
}
},
};
var car = vehicleFactory.createVehicle('Car');
console.log(car);
/*
output:
Car { name: "Car", wheels: 4 }
*/
The Abstract Factory pattern belongs to which pattern family?
View Answer:
Interview Response: The abstract factory pattern belongs to the creational design pattern category.
Why would you want to delegate the responsibility of object construction to others rather than directly calling a constructor function with the new keyword?
View Answer:
Interview Response: This is because constructor functions have little control over the complete construction process. You may need to delegate authority to a factory with broader knowledge. This method encompasses cases where the creation process incorporates object caching, object sharing or re-use, complicated logic, applications that keep object and type counts, and objects that interact with various resources or devices. If your application requires additional control over the object creation process, we recommend the Factory Pattern.
What are the objects participants used in the Abstract Factory Pattern?
View Answer:
Interview Response: The objects participants used in the Abstract Factory Pattern include the AbstractFactory, ConcreteFactory, Products, and AbstractProduct.
Technical Response: The objects participants used in the Abstract Factory Pattern include the AbstractFactory, ConcreteFactory, Products, and AbstractProduct. The AbstractFactory, not used in JavaScript, declares an interface for creating products. The ConcreteFactory is a factory object that manufactures new products, and products are simply instances created by the factory. The AbstractFactory participant, not used in JavaScript, declares an interface for the produced products.
note
Though the definition particularly mentions that an interface needs to be defined, we don’t have interfaces in Vanilla JavaScript. Therefore, we must implement it in a way that JavaScript translates into an interface.
What are some of the benefits of using the Abstract factory pattern?
View Answer:
Interview Response: The abstract factory pattern offers various advantages, which we can describe in the following fashion.
- We ensure the compatibility of items produced by the same factory class.
- Open-closed Concept: Clean code, since we introduce new product families without affecting the current code structure, ensuring the open-closed concept.
- Cleaner code since the single responsibility principle (SRP) gets followed because the obligation for generating the concrete product gets passed to the concrete creator class rather than the client class.
What are some of the disadvantages of implementing the Abstract Factory pattern?
View Answer:
Interview Response: The main drawback of the abstract factory pattern, like most design patterns, is an increase in complexity in the code and an increase in the number of classes required for the code. However, this disadvantage is well known when applying design patterns, for it is the price to pay for gaining abstraction in the code.