-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractFactoryPattern.js
More file actions
171 lines (148 loc) · 4.32 KB
/
Copy pathAbstractFactoryPattern.js
File metadata and controls
171 lines (148 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"use strict";
/**
* Abstract Factory Pattern - provides an interface for creating families of
* related or dependent objects without specifying their concrete classes. It
* acts like a salesman who creates a generic order form, find clients, take
* orders and send the orders to a group of factories to make the products.
*
* Example - Say you have online shops that spread out in different regions.
* The shops in different region will sell the same item but different sizing
* and cutting to have the best fit for people in the region.
*
*/
function forceRequiredMethods(_class, methods) {
for (let eachMethod of methods) {
if(_class[eachMethod] === undefined || typeof _class[eachMethod] !== "function") {
throw new TypeError(eachMethod + " is required and has to be overriden.");
}
}
}
class Website {
placeOrder(region) {
let shop = "";
if(region === "europe") {
shop = new EuropeanShop(new EuropeanWarehouse());
} else if(region === "asia") {
shop = new AsianShop(new AsianWarehouse());
}
return shop;
}
getPackage(region) {
let customerPackage = this.placeOrder(region);
customerPackage.fetchItem();
customerPackage.checkItem();
customerPackage.cleanItem();
customerPackage.packageItem();
return customerPackage;
}
}
class Shop {
constructor() {
if(new.target === Shop) {
throw new TypeError("This is an abstract class");
}
let _class = this;
forceRequiredMethods(_class, ["fetchItem"]);
}
checkItem() {
console.log(`Checking ${this.sunglass.getType()} for any defects`);
console.log(`Checking ${this.dress.getType()} for any defects and hide it`);
}
cleanItem() {
console.log(`Cleaning up ${this.sunglass.getType()} using dettol.`);
console.log(`Cleaning up ${this.dress.getType()} using bleach.`);
}
packageItem() {
console.log(`Package ${this.sunglass.getType()} nicely`);
console.log(`Package ${this.dress.getType()} nicely`);
}
}
class EuropeanShop extends Shop {
constructor(warehouse) {
super();
this.warehouse = warehouse;
this.region = "Europe";
}
fetchItem() {
console.log("Fetching item from the European Warehouse");
this.sunglass = this.warehouse.getSunglass();
this.dress = this.warehouse.getDress();
}
}
class AsianShop extends Shop {
constructor(warehouse) {
super();
this.warehouse = warehouse;
this.region = "Asia";
}
fetchItem() {
console.log("Fetching item from the Asian Warehouse");
this.sunglass = this.warehouse.getSunglass();
this.dress = this.warehouse.getDress();
}
}
class Warehouse {
constructor() {
if(new.target === Warehouse) {
throw new TypeError("This is an abstract class");
}
let _class = this;
forceRequiredMethods(_class, ["getSunglass", "getDress"])
}
}
class EuropeanWarehouse extends Warehouse {
getSunglass() {
return new EuropeanSunglass();
}
getDress() {
return new EuropeanLimitedEditionDress();
}
}
class AsianWarehouse extends Warehouse {
getSunglass() {
return new AsianSunglass();
}
getDress() {
return new AsianLimitedEditionDress();
}
}
class Sunglass {
constructor() {
if(new.target === Sunglass) {
throw new TypeError("This is an abstract class");
}
let _class = this;
forceRequiredMethods(_class, ["getType"])
}
}
class EuropeanSunglass {
getType() {
return "European Sunglass";
}
}
class AsianSunglass {
getType() {
return "Asian Sunglass";
}
}
class LimitedEditionDress {
constructor() {
if(new.target === LimitedEditionDress) {
throw new TypeError("This is an abstract class");
}
let _class = this;
forceRequiredMethods(_class, ["getType"])
}
}
class EuropeanLimitedEditionDress {
getType() {
return "European Really Expensive Limited Edition Dress";
}
}
class AsianLimitedEditionDress {
getType() {
return "Asian Really Limited Expensive Edition Dress";
}
}
let website = new Website();
website.getPackage("asia");