Skip to content

Commit 0e407de

Browse files
Merge pull request #32 from zoltantothcom/dev
Link screenshot image to live url in README
2 parents 48658c8 + 0c19907 commit 0e407de

27 files changed

+1616
-1593
lines changed

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v11.10.1

.travis.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
language: node_js
22

3-
node_js:
4-
- '11'
5-
63
branches:
74
only:
85
- master

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
Design Patterns - a game to get familiar with the design patterns implemented in JavaScript, test your knowledge or simply for fun.
66

7-
### :zap: [PLAY HERE](http://design-patterns-javascript.surge.sh/)
7+
### :zap: [PLAY HERE](https://design-patterns-javascript.surge.sh/)
88

9-
<img src="https://raw.githubusercontent.com/zoltantothcom/Design-Patterns-JavaScript/master/static/screenshot.png?sanitize=true&raw=true" alt="Design Patterns - game results screenshot" />
9+
[<img src="https://raw.githubusercontent.com/zoltantothcom/Design-Patterns-JavaScript/master/static/screenshot.png?sanitize=true&raw=true" alt="Design Patterns - game results screenshot" />](https://design-patterns-javascript.surge.sh/)
1010

1111
- [About](#about)
1212
- [How To Run Locally](#how-to-run-locally)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
const CHAIN_OF_RESPONSIBILITY = {
2+
id: 'chain_of_responsibility',
3+
name: 'Chain of Responsibility',
4+
type: 'behavioral',
5+
hint: 'delegates commands to a chain of processing objects',
6+
codeES5: `function ShoppingCart() {
7+
this.products = [];
8+
9+
this.addProduct = function(p) {
10+
this.products.push(p);
11+
};
12+
}
13+
14+
function Discount() {
15+
this.calc = function(products) {
16+
var ndiscount = new NumberDiscount();
17+
var pdiscount = new PriceDiscount();
18+
var none = new NoneDiscount();
19+
20+
ndiscount.setNext(pdiscount);
21+
pdiscount.setNext(none);
22+
23+
return ndiscount.exec(products);
24+
};
25+
}
26+
27+
function NumberDiscount() {
28+
this.next = null;
29+
this.setNext = function(fn) {
30+
this.next = fn;
31+
};
32+
33+
this.exec = function(products) {
34+
var result = 0;
35+
if (products.length > 3) result = 0.05;
36+
37+
return result + this.next.exec(products);
38+
};
39+
}
40+
41+
function PriceDiscount() {
42+
this.next = null;
43+
this.setNext = function(fn) {
44+
this.next = fn;
45+
};
46+
this.exec = function(products) {
47+
var result = 0;
48+
var total = products.reduce(function(a, b) {
49+
return a + b;
50+
});
51+
52+
if (total >= 500) result = 0.1;
53+
54+
return result + this.next.exec(products);
55+
};
56+
}
57+
58+
function NoneDiscount() {
59+
this.exec = function() {
60+
return 0;
61+
};
62+
}
63+
64+
module.exports = [ShoppingCart, Discount];`,
65+
codeES6: `class ShoppingCart {
66+
constructor() {
67+
this.products = [];
68+
}
69+
70+
addProduct(p) {
71+
this.products.push(p);
72+
}
73+
}
74+
75+
class Discount {
76+
calc(products) {
77+
let ndiscount = new NumberDiscount();
78+
let pdiscount = new PriceDiscount();
79+
let none = new NoneDiscount();
80+
ndiscount.setNext(pdiscount);
81+
pdiscount.setNext(none);
82+
return ndiscount.exec(products);
83+
}
84+
}
85+
86+
class NumberDiscount {
87+
constructor() {
88+
this.next = null;
89+
}
90+
91+
setNext(fn) {
92+
this.next = fn;
93+
}
94+
95+
exec(products) {
96+
let result = 0;
97+
if (products.length > 3) result = 0.05;
98+
99+
return result + this.next.exec(products);
100+
}
101+
}
102+
103+
class PriceDiscount {
104+
constructor() {
105+
this.next = null;
106+
}
107+
108+
setNext(fn) {
109+
this.next = fn;
110+
}
111+
112+
exec(products) {
113+
let result = 0;
114+
let total = products.reduce((a, b) => a + b);
115+
116+
if (total >= 500) result = 0.1;
117+
118+
return result + this.next.exec(products);
119+
}
120+
}
121+
122+
class NoneDiscount {
123+
exec() {
124+
return 0;
125+
}
126+
}
127+
128+
export { ShoppingCart, Discount };`
129+
};
130+
131+
export default CHAIN_OF_RESPONSIBILITY;
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const COMMAND = {
2+
id: 'command',
3+
name: 'Command',
4+
type: 'behavioral',
5+
hint: 'creates objects which encapsulate actions and parameters',
6+
codeES5: `function Cockpit(instruction) {
7+
this.instruction = instruction;
8+
}
9+
Cockpit.prototype.execute = function() {
10+
this.instruction.execute();
11+
};
12+
13+
function Turbine() {
14+
this.speed = 0;
15+
this.state = false;
16+
}
17+
18+
Turbine.prototype.on = function() {
19+
this.state = true;
20+
this.speed = 100;
21+
};
22+
23+
Turbine.prototype.off = function() {
24+
this.speed = 0;
25+
this.state = false;
26+
};
27+
28+
Turbine.prototype.speedDown = function() {
29+
if (!this.state) return;
30+
31+
this.speed -= 100;
32+
};
33+
34+
Turbine.prototype.speedUp = function() {
35+
if (!this.state) return;
36+
37+
this.speed += 100;
38+
};
39+
40+
function OnInstruction(turbine) {
41+
this.turbine = turbine;
42+
}
43+
OnInstruction.prototype.execute = function() {
44+
this.turbine.on();
45+
};
46+
47+
function OffInstruction(turbine) {
48+
this.turbine = turbine;
49+
}
50+
OffInstruction.prototype.execute = function() {
51+
this.turbine.off();
52+
};
53+
54+
function SpeedUpInstruction(turbine) {
55+
this.turbine = turbine;
56+
}
57+
SpeedUpInstruction.prototype.execute = function() {
58+
this.turbine.speedUp();
59+
};
60+
61+
function SpeedDownInstruction(turbine) {
62+
this.turbine = turbine;
63+
}
64+
SpeedDownInstruction.prototype.execute = function() {
65+
this.turbine.speedDown();
66+
};
67+
68+
module.exports = [Cockpit, Turbine, OnInstruction, OffInstruction, SpeedUpInstruction, SpeedDownInstruction];`,
69+
codeES6: `class Cockpit {
70+
constructor(instruction) {
71+
this.instruction = instruction;
72+
}
73+
execute() {
74+
this.instruction.execute();
75+
}
76+
}
77+
78+
class Turbine {
79+
constructor() {
80+
this.state = false;
81+
}
82+
on() {
83+
this.state = true;
84+
}
85+
off() {
86+
this.state = false;
87+
}
88+
}
89+
90+
class OnInstruction {
91+
constructor(turbine) {
92+
this.turbine = turbine;
93+
}
94+
execute() {
95+
this.turbine.on();
96+
}
97+
}
98+
99+
class OffInstruction {
100+
constructor(turbine) {
101+
this.turbine = turbine;
102+
}
103+
execute() {
104+
this.turbine.off();
105+
}
106+
}
107+
108+
export { Cockpit, Turbine, OnInstruction, OffInstruction };`
109+
};
110+
111+
export default COMMAND;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const INTERPRETER = {
2+
id: 'interpteter',
3+
name: 'Interpreter',
4+
type: 'behavioral',
5+
hint: 'implements a specialized language',
6+
codeES5: `function Sum(left, right) {
7+
this.left = left;
8+
this.right = right;
9+
}
10+
11+
Sum.prototype.pattern = function() {
12+
return this.left.pattern() + this.right.pattern();
13+
};
14+
15+
function Min(left, right) {
16+
this.left = left;
17+
this.right = right;
18+
}
19+
20+
Min.prototype.pattern = function() {
21+
return this.left.pattern() - this.right.pattern();
22+
};
23+
24+
function Num(val) {
25+
this.val = val;
26+
}
27+
28+
Num.prototype.pattern = function() {
29+
return this.val;
30+
};
31+
32+
module.exports = [Num, Min, Sum];`,
33+
codeES6: `class Sum {
34+
constructor(left, right) {
35+
this.left = left;
36+
this.right = right;
37+
}
38+
39+
pattern() {
40+
return this.left.pattern() + this.right.pattern();
41+
}
42+
}
43+
44+
class Min {
45+
constructor(left, right) {
46+
this.left = left;
47+
this.right = right;
48+
}
49+
50+
pattern() {
51+
return this.left.pattern() - this.right.pattern();
52+
}
53+
}
54+
55+
class Num {
56+
constructor(val) {
57+
this.val = val;
58+
}
59+
60+
pattern() {
61+
return this.val;
62+
}
63+
}
64+
65+
export { Num, Min, Sum };`
66+
};
67+
68+
export default INTERPRETER;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const ITERATOR = {
2+
id: 'iterator',
3+
name: 'Iterator',
4+
type: 'behavioral',
5+
hint:
6+
'accesses the elements of an object sequentially without exposing its underlying representation',
7+
codeES5: `function Pattern(el) {
8+
this.index = 0;
9+
this.elements = el;
10+
}
11+
12+
Pattern.prototype = {
13+
next: function() {
14+
return this.elements[this.index++];
15+
},
16+
hasNext: function() {
17+
return this.index < this.elements.length;
18+
}
19+
};
20+
21+
module.exports = Pattern;`,
22+
codeES6: `class Pattern {
23+
constructor(el) {
24+
this.index = 0;
25+
this.elements = el;
26+
}
27+
28+
next() {
29+
return this.elements[this.index++];
30+
}
31+
32+
hasNext() {
33+
return this.index < this.elements.length;
34+
}
35+
}
36+
37+
export default Pattern;`
38+
};
39+
40+
export default ITERATOR;

0 commit comments

Comments
 (0)