Skip to content

Commit b028756

Browse files
Creational Design Pattern
Added the code for design pattern that comes under Creational design pattern
0 parents  commit b028756

5 files changed

+222
-0
lines changed

Diff for: builderPattern.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// // Builder Design Pattern, which is a pattern used to help construct complex objects. It helps separate
2+
// // object construction from its representation which will help us reuse this to create different representations.
3+
// // here we instantiate a object with basic parameter and all the code for its other pattern and added subsequemtly.
4+
// // the builder pattern is generally needed most when we need a way to help simplify constructions of complex objects,
5+
// // so the best time to introduce this into your code is when you're hitting that point or when they are becoming large.
6+
// It lays out the following steps:
7+
8+
//1. The base class contains the business logic
9+
10+
// 2. It also receives the object that was created and proceeds to set the values
11+
// Separate the code that is responsible for creating objects into builders--which ultimately are also just objects/classes.
12+
13+
// All of these builders will be responsible for defining the steps to construct the complex objects.
14+
// Can use an optional class which is called the Director
15+
// Directors are involved in defining methods ensuring that steps are executed in a specific order to build the commonly constructed objects.
16+
17+
class Animal{
18+
constructor(type,species,name,wild,food) {
19+
this.type = type;
20+
this.species = species;
21+
this.name = name;
22+
this.wild = wild ;
23+
this.food = food ;
24+
25+
}
26+
27+
eat(target) {
28+
console.log(`Eating target: ${target.name}`)
29+
}
30+
}
31+
32+
33+
34+
class AnimalBuilder{
35+
36+
constructor(species, type){
37+
this.species = species;
38+
this.type =type;
39+
}
40+
41+
setName(name){
42+
this.name = name;
43+
return this;
44+
}
45+
46+
isWild(wild){
47+
this.wild = wild;
48+
return this;
49+
}
50+
eats(food){
51+
this.food = food;
52+
return this;
53+
}
54+
55+
build(){
56+
if(!this.name){
57+
throw new Error('Name is missing');
58+
}
59+
60+
return new Animal(this.type, this.species, this.name,this.wild,this.food);
61+
}
62+
63+
64+
}
65+
66+
67+
//so now without the builder class we will previously call, here the issue is as the parameter increases its complexity
68+
//increases as we have to always do a two and fro while instantiating the source code plus there could be easily mistakes
69+
70+
const rabbit = new Animal('rabbit', 'land', 'momo', null,'herbivore');
71+
72+
73+
// with builder patterm
74+
75+
const lion = new AnimalBuilder('lion', 'land')
76+
.setName('lion').isWild( true).eats('carnivore').build();
77+
78+
79+
//The builder pattern might seem similar to the abstract factory pattern but one difference is that the builder pattern creates
80+
//an object step by step whereas the abstract factory pattern returns the object in one go.
81+
82+
83+
84+

Diff for: constructor_pattern.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function Animal( species, animalName, food ) {
2+
this.species= species;
3+
this.animalName= animalName;
4+
this.food = food;
5+
this.eat = function () {
6+
return this.animalName+ " eats" + this.food ;
7+
};
8+
}
9+
// We can create new instances of the animal
10+
const lion = new Animal('Felidae', 'lion', 'meet');
11+
12+
In Es6 way using class
13+
class Animal{
14+
constructor(species, animalName, food ){
15+
this._species= species;
16+
this._animalName= animalName;
17+
this._food = food;
18+
this.eat = function () {
19+
return this.animalName+ " eats" + this.food ;
20+
};
21+
22+
}
23+
}
24+
var rabbit = new Animal('Leporidae','rabbit', 'grass');

Diff for: factoryPattern.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class VehicleFactory{
2+
3+
constructor(){
4+
5+
this.createVehicle = function(configs){
6+
7+
let vehicle;
8+
const {type} = configs;
9+
10+
switch(type){
11+
12+
case "Car" :
13+
vehicle = new Car(configs);
14+
break;
15+
16+
case "Truck":
17+
vehicle = new Truck(configs);
18+
break;
19+
20+
default :
21+
return null;
22+
}
23+
24+
vehicle.genericInformation = () => {
25+
console.log("heavy vehicle");
26+
}
27+
28+
return vehicle;
29+
}
30+
31+
}
32+
}
33+
34+
class Car {
35+
36+
constructor(options){
37+
// some defaults
38+
this.type = "Car";
39+
this.doors = options.doors || 4;
40+
this.state = options.state || "brand new";
41+
this.color = options.color || "silver";
42+
}
43+
44+
carForMe = (option) => {
45+
46+
const {coPassenger, safetyLevel, sunRoof} = options;
47+
48+
if(coPassenger >=2 && safetyLevel <=5 && sunRoof) return "You need a fast and furious car";
49+
50+
else
51+
return "You are in for a cutesy small car. :) "
52+
}
53+
54+
55+
}
56+
57+
class Truck {
58+
59+
constructor(options){
60+
this.type="Truck";
61+
this.state = options.state || "used";
62+
this.wheelSize = options.wheelSize || "large";
63+
this.color = options.color || "blue";
64+
}
65+
}

Diff for: prototypePaatern.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
1.// By using Object.create.Object.create creates an object which has a specified prototype and optionally contains specified properties as wel
2+
3+
4+
const car ={
5+
6+
name: "Maruti",
7+
8+
drive: function () {
9+
console.log( "Weeee. I'm driving!" );
10+
},
11+
12+
panic: function () {
13+
console.log( "Wait. How do you stop this thing?" );
14+
}
15+
};
16+
17+
const options = {
18+
"id": {
19+
value: '11',
20+
enumerable: true
21+
},
22+
23+
"model": {
24+
value: "Ford",
25+
enumerable: true
26+
}
27+
}
28+
const yourCar = Object.create(car, options);
29+
30+

Diff for: singleTon.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Logger {
2+
constructor(name = "") {
3+
if (!!Logger.instance) {
4+
return Logger.instance;
5+
}
6+
7+
Logger.instance = this;
8+
9+
this.name = name;
10+
11+
return this;
12+
}
13+
14+
log(val) {
15+
console.log(val);
16+
}
17+
}
18+
19+

0 commit comments

Comments
 (0)