-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleFactoryPattern.js
More file actions
77 lines (69 loc) · 1.78 KB
/
Copy pathSimpleFactoryPattern.js
File metadata and controls
77 lines (69 loc) · 1.78 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
"use strict";
/**
* Simple Factory Pattern - decouples object creation to an interface,
* encapsulating complexity of code into another object and that object's
* main role is just for generating different types of components
*
* Example - User in different locale have different language and currency.
* A user factory can be used in this case to supervise the creation of
* the different type of users
*
*/
class UserFactory {
createUser(locale) {
if(locale == "us") {
return new AmericanUser();
} else if(locale == "china") {
return new ChineseUser();
} else if(locale == "japan") {
return new JapaneseUser();
} else if(locale == "uk") {
return new BritishUser();
} else if(locale == "australia") {
return new AustralianUser();
} else {
console.log("Please specify a locale");
return;
}
}
}
class User {
constructor(language, currency) {
this.language = language;
this.currency = currency;
}
getProfile() {
return {
language: this.language,
currency: this.currency
}
}
}
class AmericanUser extends User {
constructor() {
super("English", "USD");
}
}
class ChineseUser extends User {
constructor() {
super("Chinese", "RMB");
}
}
class JapaneseUser extends User {
constructor() {
super("Japanese", "JPY");
}
}
class BritishUser extends User {
constructor() {
super("English", "GBP");
}
}
class AustralianUser extends User {
constructor() {
super("English", "AUD");
}
}
var userFactory = new UserFactory();
var americanUser = userFactory.createUser("us");
console.log(americanUser.getProfile())