-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs_builder.js
77 lines (61 loc) · 2.61 KB
/
cs_builder.js
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
// Builder Pattern.
// The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects.
// A factory is simply a wrapper function around the constructor. The principle difference is that a factory method pattern require the entire object to be built in a single call, will all the parameters pass in on a single line. Then final object will be return.
// A builder pattern, whereas is a wrapper object around all the possible parameters you might want to pass to a constructor. This allows you to use setter method to build your own parameter list. So generally it involves calling a number of SET functions and then get the final object. The Builder is only needed when an object cannot be produced in one step.The Builder pattern abstracts the algorithm for construction by including the concept of a director. The director is responsible for itemizing the steps and calls on builders to fulfill them.
// so we have two things here, Directors and Builder.
function FastCPU() {
this.performOperation = function() {
console.log("Operation will perform quickly");
}
}
function SlowCPU() {
this.performOperation = function() {
console.log("Operation will perform slowly");
}
}
function ExpensiveMotherBoard() {
this.storeData = function() {
console.log("There is a lot of RAM to store the data");
}
}
function CheapMotherBoard() {
this.storeData = function() {
console.log("Little RAM. Swap file is used");
}
}
// Builders.
function HighBudgetMachineBuilder() {
this.getCPU = function() { return new FastCPU(); }
this.getMotherBoard = function() {
return new ExpensiveMotherBoard();
}
}
function LowBudgetMachineBuilder() {
this.getCPU = function() { return new SlowCPU(); }
this.getMotherBoard = function() {
return new CheapMotherBoard();
}
}
// Director
function Director() {
this.assembleMachine = function(builder) {
var cpu = builder.getCPU(),
board = builder.getMotherBoard();
return {
cpu: cpu,
board: board,
test: function test() {
this.cpu.performOperation();
this.board.storeData();
}
}
}
}
// Finally Create the object from Directors help.
var director = new Director(),
highBuilder = new HighBudgetMachineBuilder(),
lowBuilder = new LowBudgetMachineBuilder();
var highMachine = director.assembleMachine(highBuilder);
var lowMachine = director.assembleMachine(lowBuilder);
highMachine.test();
lowMachine.test();