Solutions to commonly occuring problems in software design. Can be considered as templates that can be used to solve different problems
-
Methods for creating objects in a specific situation withour revealing the creation method.
-
These are concerned about class/object composition and relationship between objects. They let us to add new features / functionalities so that restructuring some parts of system do not affect the rest.
-
Concerned with the communication between dissimilar objects in system. They streamline the communication and make the information synchronised between objects.
-
Used for solving the archetectural problems wihtin a given context in software architecture.
- Template to create objects.
- Used in complex situations where the type of object required varies and needs to be specified in each case.
- It does not use the new keyword to instantiate the objects.
- It provides a generic interface that delegates the object creation responsibility to corresponding subclass.
Design-Patterns-Javascript/creational/factory_pattern.js
Lines 1 to 44 in 4a24bb3
const iceCreamFactory = new IceCreamFactory();
const chocolate = iceCreamFactory.createIcecream("chocolate");
const mint = iceCreamFactory.createIcecream("mint");
const strawberry = iceCreamFactory.createIcecream("strawberry");
console.log(chocolate.message());
console.log(mint.message());
console.log(strawberry.message());
You chose the chocolate flavour
You chose the mint flavour
You chose the strawberry flavour
- Use constructor present in class to create specific type of object.
Design-Patterns-Javascript/creational/constructor_pattern.js
Lines 1 to 10 in 4a24bb3
const human = new Human("Harshit", 29, "engineer");
human.describe();
Harshit is a 29 years old engineer
Design-Patterns-Javascript/creational/constructor_pattern.js
Lines 14 to 25 in 4a24bb3
const person = new Person("Harshit", 29, "engineer");
person.describe();
Harshit is a 29 years old engineer
libraries
plugins
- It restricts the instantiation of a class to a single object.
- On next try, existing instance is returned, no new instance is returned.
example
: printer to be used in office. Single instance is required so that everyone can use it, instead of having a new instance for each employee.
Design-Patterns-Javascript/creational/singleton_pattern.js
Lines 1 to 17 in 4a24bb3
var object1 = Printer.getInstance(2);
console.log(object1);
object1.display();
var object2 = Printer.getInstance(3);
console.log(object2);
object2.display();
console.log(object1 === object2);
Printer { display: [Function (anonymous)] }
You are connected to the printer. You want to print 2 pages.
Printer { display: [Function (anonymous)] }
You are connected to the printer. You want to print 2 pages.
true
services
: different service instances in application should be single instance.databses
: single connection in an applicationconfiguration
- Helps in building complex objects using simpler objects.
- It provides flexible and step by step approach to construct objects.
- It keeps representation and process of creation shielded
Design-Patterns-Javascript/creational/builder_pattern.js
Lines 1 to 12 in 4a24bb3
Design-Patterns-Javascript/creational/builder_pattern.js
Lines 13 to 35 in 4a24bb3
Design-Patterns-Javascript/creational/builder_pattern.js
Lines 36 to 59 in 4a24bb3
var meal = new Meal();
var mealBuilder = new MealBuilder("chicken", "curly fries", "coke");
var chickenBurgerMeal = meal.make(mealBuilder);
chickenBurgerMeal.display();
Your meal has a chicken, curly fries on the side and a coke
- When complex objects are to be made.
- example, DOM elements - plenty of nodes and attributes.