Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v11.10.1
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
language: node_js

node_js:
- '11'

branches:
only:
- master
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

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

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

<img src="https://raw.githubusercontent.com/zoltantothcom/Design-Patterns-JavaScript/master/static/screenshot.png?sanitize=true&raw=true" alt="Design Patterns - game results screenshot" />
[<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/)

- [About](#about)
- [How To Run Locally](#how-to-run-locally)
Expand Down
131 changes: 131 additions & 0 deletions src/static/patterns/behavioral_chainOfResponsibility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
const CHAIN_OF_RESPONSIBILITY = {
id: 'chain_of_responsibility',
name: 'Chain of Responsibility',
type: 'behavioral',
hint: 'delegates commands to a chain of processing objects',
codeES5: `function ShoppingCart() {
this.products = [];

this.addProduct = function(p) {
this.products.push(p);
};
}

function Discount() {
this.calc = function(products) {
var ndiscount = new NumberDiscount();
var pdiscount = new PriceDiscount();
var none = new NoneDiscount();

ndiscount.setNext(pdiscount);
pdiscount.setNext(none);

return ndiscount.exec(products);
};
}

function NumberDiscount() {
this.next = null;
this.setNext = function(fn) {
this.next = fn;
};

this.exec = function(products) {
var result = 0;
if (products.length > 3) result = 0.05;

return result + this.next.exec(products);
};
}

function PriceDiscount() {
this.next = null;
this.setNext = function(fn) {
this.next = fn;
};
this.exec = function(products) {
var result = 0;
var total = products.reduce(function(a, b) {
return a + b;
});

if (total >= 500) result = 0.1;

return result + this.next.exec(products);
};
}

function NoneDiscount() {
this.exec = function() {
return 0;
};
}

module.exports = [ShoppingCart, Discount];`,
codeES6: `class ShoppingCart {
constructor() {
this.products = [];
}

addProduct(p) {
this.products.push(p);
}
}

class Discount {
calc(products) {
let ndiscount = new NumberDiscount();
let pdiscount = new PriceDiscount();
let none = new NoneDiscount();
ndiscount.setNext(pdiscount);
pdiscount.setNext(none);
return ndiscount.exec(products);
}
}

class NumberDiscount {
constructor() {
this.next = null;
}

setNext(fn) {
this.next = fn;
}

exec(products) {
let result = 0;
if (products.length > 3) result = 0.05;

return result + this.next.exec(products);
}
}

class PriceDiscount {
constructor() {
this.next = null;
}

setNext(fn) {
this.next = fn;
}

exec(products) {
let result = 0;
let total = products.reduce((a, b) => a + b);

if (total >= 500) result = 0.1;

return result + this.next.exec(products);
}
}

class NoneDiscount {
exec() {
return 0;
}
}

export { ShoppingCart, Discount };`
};

export default CHAIN_OF_RESPONSIBILITY;
111 changes: 111 additions & 0 deletions src/static/patterns/behavioral_command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const COMMAND = {
id: 'command',
name: 'Command',
type: 'behavioral',
hint: 'creates objects which encapsulate actions and parameters',
codeES5: `function Cockpit(instruction) {
this.instruction = instruction;
}
Cockpit.prototype.execute = function() {
this.instruction.execute();
};

function Turbine() {
this.speed = 0;
this.state = false;
}

Turbine.prototype.on = function() {
this.state = true;
this.speed = 100;
};

Turbine.prototype.off = function() {
this.speed = 0;
this.state = false;
};

Turbine.prototype.speedDown = function() {
if (!this.state) return;

this.speed -= 100;
};

Turbine.prototype.speedUp = function() {
if (!this.state) return;

this.speed += 100;
};

function OnInstruction(turbine) {
this.turbine = turbine;
}
OnInstruction.prototype.execute = function() {
this.turbine.on();
};

function OffInstruction(turbine) {
this.turbine = turbine;
}
OffInstruction.prototype.execute = function() {
this.turbine.off();
};

function SpeedUpInstruction(turbine) {
this.turbine = turbine;
}
SpeedUpInstruction.prototype.execute = function() {
this.turbine.speedUp();
};

function SpeedDownInstruction(turbine) {
this.turbine = turbine;
}
SpeedDownInstruction.prototype.execute = function() {
this.turbine.speedDown();
};

module.exports = [Cockpit, Turbine, OnInstruction, OffInstruction, SpeedUpInstruction, SpeedDownInstruction];`,
codeES6: `class Cockpit {
constructor(instruction) {
this.instruction = instruction;
}
execute() {
this.instruction.execute();
}
}

class Turbine {
constructor() {
this.state = false;
}
on() {
this.state = true;
}
off() {
this.state = false;
}
}

class OnInstruction {
constructor(turbine) {
this.turbine = turbine;
}
execute() {
this.turbine.on();
}
}

class OffInstruction {
constructor(turbine) {
this.turbine = turbine;
}
execute() {
this.turbine.off();
}
}

export { Cockpit, Turbine, OnInstruction, OffInstruction };`
};

export default COMMAND;
68 changes: 68 additions & 0 deletions src/static/patterns/behavioral_interpreter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const INTERPRETER = {
id: 'interpteter',
name: 'Interpreter',
type: 'behavioral',
hint: 'implements a specialized language',
codeES5: `function Sum(left, right) {
this.left = left;
this.right = right;
}

Sum.prototype.pattern = function() {
return this.left.pattern() + this.right.pattern();
};

function Min(left, right) {
this.left = left;
this.right = right;
}

Min.prototype.pattern = function() {
return this.left.pattern() - this.right.pattern();
};

function Num(val) {
this.val = val;
}

Num.prototype.pattern = function() {
return this.val;
};

module.exports = [Num, Min, Sum];`,
codeES6: `class Sum {
constructor(left, right) {
this.left = left;
this.right = right;
}

pattern() {
return this.left.pattern() + this.right.pattern();
}
}

class Min {
constructor(left, right) {
this.left = left;
this.right = right;
}

pattern() {
return this.left.pattern() - this.right.pattern();
}
}

class Num {
constructor(val) {
this.val = val;
}

pattern() {
return this.val;
}
}

export { Num, Min, Sum };`
};

export default INTERPRETER;
40 changes: 40 additions & 0 deletions src/static/patterns/behavioral_iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const ITERATOR = {
id: 'iterator',
name: 'Iterator',
type: 'behavioral',
hint:
'accesses the elements of an object sequentially without exposing its underlying representation',
codeES5: `function Pattern(el) {
this.index = 0;
this.elements = el;
}

Pattern.prototype = {
next: function() {
return this.elements[this.index++];
},
hasNext: function() {
return this.index < this.elements.length;
}
};

module.exports = Pattern;`,
codeES6: `class Pattern {
constructor(el) {
this.index = 0;
this.elements = el;
}

next() {
return this.elements[this.index++];
}

hasNext() {
return this.index < this.elements.length;
}
}

export default Pattern;`
};

export default ITERATOR;
Loading