diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6c25410 --- /dev/null +++ b/.gitignore @@ -0,0 +1,31 @@ +### Node template +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git +node_modules + +# Created by .ignore support plugin (hsz.mobi) diff --git a/Adapter/Duck.js b/Adapter/Duck.js deleted file mode 100644 index 73416f9..0000000 --- a/Adapter/Duck.js +++ /dev/null @@ -1,9 +0,0 @@ -var Duck = function(){ - -}; -Duck.prototype.fly = function(){ - throw new Error("This method must be overwritten!"); -}; -Duck.prototype.quack = function(){ - throw new Error("This method must be overwritten!"); -}; diff --git a/Adapter/MallardDuck.js b/Adapter/MallardDuck.js deleted file mode 100644 index 69ee871..0000000 --- a/Adapter/MallardDuck.js +++ /dev/null @@ -1,10 +0,0 @@ -var MallardDuck = function(){ - Duck.apply(this); -}; -MallardDuck.prototype = new Duck(); -MallardDuck.prototype.fly = function(){ - console.log("Can fly long distances!"); -}; -MallardDuck.prototype.quack = function(){ - console.log("Quack! Quack!"); -}; \ No newline at end of file diff --git a/Adapter/Turkey.js b/Adapter/Turkey.js deleted file mode 100644 index f29e0b7..0000000 --- a/Adapter/Turkey.js +++ /dev/null @@ -1,9 +0,0 @@ -var Turkey = function(){ - -}; -Turkey.prototype.fly = function(){ - throw new Error("This method must be overwritten!"); -}; -Turkey.prototype.gobble = function(){ - throw new Error("This method must be overwritten!"); -}; diff --git a/Adapter/TurkeyAdapter.js b/Adapter/TurkeyAdapter.js deleted file mode 100644 index 42535bf..0000000 --- a/Adapter/TurkeyAdapter.js +++ /dev/null @@ -1,16 +0,0 @@ -var TurkeyAdapter = function(oTurkey){ - Duck.apply(this); - this.oTurkey = oTurkey; -}; -TurkeyAdapter.prototype = new Duck(); -TurkeyAdapter.prototype.quack = function(){ - this.oTurkey.gobble(); -}; -TurkeyAdapter.prototype.fly = function(){ - var nFly = 0; - var nLenFly = 5; - for(; nFly < nLenFly;){ - this.oTurkey.fly(); - nFly = nFly + 1; - } -}; diff --git a/Adapter/WildTurkey.js b/Adapter/WildTurkey.js deleted file mode 100644 index f083ecf..0000000 --- a/Adapter/WildTurkey.js +++ /dev/null @@ -1,10 +0,0 @@ -var WildTurkey = function(){ - Turkey.apply(this); -}; -WildTurkey.prototype = new Turkey(); -WildTurkey.prototype.fly = function(){ - console.log("Fly short distance!"); -}; -WildTurkey.prototype.gobble = function(){ - console.log("Gobble! Gobble!"); -}; \ No newline at end of file diff --git a/Adapter/index.html b/Adapter/index.html index 508da1d..69fe56a 100644 --- a/Adapter/index.html +++ b/Adapter/index.html @@ -1,20 +1,19 @@ - + Adapter Pattern - - - - - - - -
-

Source

+ + +
+

Source

-var oMallardDuck = new MallardDuck();
-var oWildTurkey = new WildTurkey();
-var oTurkeyAdapter = new TurkeyAdapter(oWildTurkey);
+import MallardDuck from './MallardDuck';
+import WildTurkey from './WildTurkey';
+import TurkeyAdapter from './TurkeyAdapter';
+
+let oMallardDuck = new MallardDuck();
+let oWildTurkey = new WildTurkey();
+let oTurkeyAdapter = new TurkeyAdapter(oWildTurkey);
 
 oMallardDuck.fly();
 oMallardDuck.quack();
@@ -25,26 +24,15 @@ 

Source

oTurkeyAdapter.fly(); oTurkeyAdapter.quack();
-
-
-

Console

- -

ADAPTER

-
- - - + + + diff --git a/Adapter/scripts/Duck.js b/Adapter/scripts/Duck.js new file mode 100644 index 0000000..b259e62 --- /dev/null +++ b/Adapter/scripts/Duck.js @@ -0,0 +1,12 @@ +class Duck { + constructor() {} + + fly() { + throw new Error('This method must be overwritten!'); + } + quack() { + throw new Error('This method must be overwritten!'); + } +} + +export default Duck; \ No newline at end of file diff --git a/Adapter/scripts/MallardDuck.js b/Adapter/scripts/MallardDuck.js new file mode 100644 index 0000000..88815b3 --- /dev/null +++ b/Adapter/scripts/MallardDuck.js @@ -0,0 +1,12 @@ +import Duck from './Duck'; + +class MallardDuck extends Duck { + fly() { + console.log('Can fly long distances!'); + } + quack() { + console.log('Quack! Quack!'); + } +} + +export default MallardDuck; \ No newline at end of file diff --git a/Adapter/scripts/Turkey.js b/Adapter/scripts/Turkey.js new file mode 100644 index 0000000..2140750 --- /dev/null +++ b/Adapter/scripts/Turkey.js @@ -0,0 +1,10 @@ +class Turkey { + fly() { + throw new Error('This method must be overwritten!'); + } + gobble() { + throw new Error('This method must be overwritten'); + } +} + +export default Turkey; \ No newline at end of file diff --git a/Adapter/scripts/TurkeyAdapter.js b/Adapter/scripts/TurkeyAdapter.js new file mode 100644 index 0000000..8117798 --- /dev/null +++ b/Adapter/scripts/TurkeyAdapter.js @@ -0,0 +1,19 @@ +import Duck from './Duck'; + +class TurkeyAdapter extends Duck{ + constructor(oTurkey) { + super(oTurkey); + this.oTurkey = oTurkey; + } + fly() { + for(let index = 0, maxFly = 5; index < maxFly; index++) { + this.oTurkey.fly(); + } + } + + quack() { + this.oTurkey.gobble(); + } +} + +export default TurkeyAdapter; \ No newline at end of file diff --git a/Adapter/scripts/WildTurkey.js b/Adapter/scripts/WildTurkey.js new file mode 100644 index 0000000..6c2c9a8 --- /dev/null +++ b/Adapter/scripts/WildTurkey.js @@ -0,0 +1,12 @@ +import Turkey from './Turkey'; + +class WildTurkey extends Turkey { + fly() { + console.log('Fly short distance!'); + } + gobble() { + console.log('Gobble!, Gobble!'); + } +} + +export default WildTurkey; \ No newline at end of file diff --git a/Adapter/scripts/main.js b/Adapter/scripts/main.js new file mode 100644 index 0000000..11d0872 --- /dev/null +++ b/Adapter/scripts/main.js @@ -0,0 +1,16 @@ +import MallardDuck from './MallardDuck'; +import WildTurkey from './WildTurkey'; +import TurkeyAdapter from './TurkeyAdapter'; + +let oMallardDuck = new MallardDuck(); +let oWildTurkey = new WildTurkey(); +let oTurkeyAdapter = new TurkeyAdapter(oWildTurkey); + +oMallardDuck.fly(); +oMallardDuck.quack(); + +oWildTurkey.fly(); +oWildTurkey.gobble(); + +oTurkeyAdapter.fly(); +oTurkeyAdapter.quack(); \ No newline at end of file diff --git a/Chaining/Chainable.js b/Chaining/Chainable.js deleted file mode 100644 index 3a6c765..0000000 --- a/Chaining/Chainable.js +++ /dev/null @@ -1,15 +0,0 @@ -var Chainable = function () { - this.nNumber = 0; -}; -Chainable.prototype.add = function (nNumber) { - this.nNumber += nNumber; - return this; -}; -Chainable.prototype.multiply = function (nNumber) { - this.nNumber *= nNumber; - return this; -}; -Chainable.prototype.toString = function() -{ - return this.nNumber.toString(); -}; \ No newline at end of file diff --git a/Chaining/index.html b/Chaining/index.html index d2a2e0d..9804986 100644 --- a/Chaining/index.html +++ b/Chaining/index.html @@ -2,7 +2,6 @@ Chain Pattern -
@@ -19,10 +18,6 @@

Console

Chain

- + \ No newline at end of file diff --git a/Chaining/scripts/Chainable.js b/Chaining/scripts/Chainable.js new file mode 100644 index 0000000..e4fb41b --- /dev/null +++ b/Chaining/scripts/Chainable.js @@ -0,0 +1,21 @@ +class Chainable { + constructor() { + this.number = 0; + } + + add(number) { + this.number += number; + return this; + } + + multiply(number) { + this.number *= number; + return this; + } + + toString() { + return this.number.toString(); + } +} + +export default Chainable; \ No newline at end of file diff --git a/Chaining/scripts/main.js b/Chaining/scripts/main.js new file mode 100644 index 0000000..29b6ffd --- /dev/null +++ b/Chaining/scripts/main.js @@ -0,0 +1,5 @@ +import Chainable from './Chainable'; + +var oChainable = new Chainable(); +// This must return "21" +console.log(oChainable.add(3).add(4).multiply(3).toString()); \ No newline at end of file diff --git a/Command/1/Command.js b/Command/1/Command.js deleted file mode 100644 index 136d0b6..0000000 --- a/Command/1/Command.js +++ /dev/null @@ -1,6 +0,0 @@ -var Command = function(){ - -}; -Command.prototype.execute = function(){ - throw new Error("This method must be overwritten!"); -}; diff --git a/Command/1/Light.js b/Command/1/Light.js deleted file mode 100644 index 5debee8..0000000 --- a/Command/1/Light.js +++ /dev/null @@ -1,11 +0,0 @@ -var Light = function(){ - this.bOn = false; -}; -Light.prototype.on = function(){ - this.bOn = true; - console.log("Light is on"); -}; -Light.prototype.off = function(){ - this.bOn = false; - console.log("Light is off"); -}; diff --git a/Command/1/LightOnCommand.js b/Command/1/LightOnCommand.js deleted file mode 100644 index 1e9c1a6..0000000 --- a/Command/1/LightOnCommand.js +++ /dev/null @@ -1,8 +0,0 @@ -var LightOnCommand = function(oLight){ - Command.apply(this); - this.oLight = oLight; -}; -LightOnCommand.prototype = new Command(); -LightOnCommand.prototype.execute = function(){ - this.oLight.on(); -}; \ No newline at end of file diff --git a/Command/1/SimpleRemoteControl.js b/Command/1/SimpleRemoteControl.js deleted file mode 100644 index c3467c4..0000000 --- a/Command/1/SimpleRemoteControl.js +++ /dev/null @@ -1,9 +0,0 @@ -var SimpleRemoteControl = function(){ - this.oCommand = null; -}; -SimpleRemoteControl.prototype.setCommand = function(oCommand){ - this.oCommand = oCommand; -}; -SimpleRemoteControl.prototype.buttonWasPressed = function(){ - this.oCommand.execute(); -}; diff --git a/Command/1/index.html b/Command/1/index.html index d1bebf5..07464c7 100644 --- a/Command/1/index.html +++ b/Command/1/index.html @@ -2,18 +2,18 @@ Command Pattern - - - -

Source

-var oSimpleRemote = new SimpleRemoteControl();
-var oLight = new Light();
-var oLightCommand = new LightOnCommand(oLight);
+import Light from './Light';
+import LightOnCommand from './LightOnCommand';
+import SimpleRemoteControl from './SimpleRemoteControl';
+
+let oSimpleRemote = new SimpleRemoteControl();
+let oLight = new Light();
+let oLightCommand = new LightOnCommand(oLight);
 
 oSimpleRemote.setCommand(oLightCommand);
 oSimpleRemote.buttonWasPressed();
@@ -25,13 +25,6 @@ 

Console

COMMAND

- + diff --git a/Command/1/scripts/Command.js b/Command/1/scripts/Command.js new file mode 100644 index 0000000..ad68715 --- /dev/null +++ b/Command/1/scripts/Command.js @@ -0,0 +1,7 @@ +class Command { + execute() { + throw new Error('This method must be overwritten!'); + } +} + +export default Command; diff --git a/Command/1/scripts/Light.js b/Command/1/scripts/Light.js new file mode 100644 index 0000000..177741b --- /dev/null +++ b/Command/1/scripts/Light.js @@ -0,0 +1,16 @@ +class Light { + constructor() { + this._on = false; + } + on() { + this._on = true; + console.log('Light is on'); + } + + off() { + this._on = false; + console.log('Light is off'); + } +} + +export default Light; diff --git a/Command/1/scripts/LightOnCommand.js b/Command/1/scripts/LightOnCommand.js new file mode 100644 index 0000000..3678de2 --- /dev/null +++ b/Command/1/scripts/LightOnCommand.js @@ -0,0 +1,13 @@ +import Command from './Command'; + +class LightOnCommand extends Command{ + constructor(light) { + super(); + this.light = light; + } + execute() { + this.light.on(); + } +} + +export default LightOnCommand; diff --git a/Command/1/scripts/SimpleRemoteControl.js b/Command/1/scripts/SimpleRemoteControl.js new file mode 100644 index 0000000..44139c1 --- /dev/null +++ b/Command/1/scripts/SimpleRemoteControl.js @@ -0,0 +1,13 @@ +class SimpleRemoteControl { + constructor() { + this.command = null; + } + setCommand(command) { + this.command = command; + } + buttonWasPressed() { + this.command.execute(); + } +} + +export default SimpleRemoteControl; diff --git a/Command/1/scripts/main.js b/Command/1/scripts/main.js new file mode 100644 index 0000000..d427dd3 --- /dev/null +++ b/Command/1/scripts/main.js @@ -0,0 +1,10 @@ +import Light from './Light'; +import LightOnCommand from './LightOnCommand'; +import SimpleRemoteControl from './SimpleRemoteControl'; + +let oSimpleRemote = new SimpleRemoteControl(); +let oLight = new Light(); +let oLightCommand = new LightOnCommand(oLight); + +oSimpleRemote.setCommand(oLightCommand); +oSimpleRemote.buttonWasPressed(); diff --git a/Command/2/Command.js b/Command/2/Command.js deleted file mode 100644 index e64e11d..0000000 --- a/Command/2/Command.js +++ /dev/null @@ -1,9 +0,0 @@ -var Command = function(){ - -}; -Command.prototype.execute = function(){ - throw new Error("This method must be overwritten!"); -}; -Command.prototype.undo = function(){ - throw new Error("This method must be overwritten!"); -}; diff --git a/Command/2/Light.js b/Command/2/Light.js deleted file mode 100644 index 5debee8..0000000 --- a/Command/2/Light.js +++ /dev/null @@ -1,11 +0,0 @@ -var Light = function(){ - this.bOn = false; -}; -Light.prototype.on = function(){ - this.bOn = true; - console.log("Light is on"); -}; -Light.prototype.off = function(){ - this.bOn = false; - console.log("Light is off"); -}; diff --git a/Command/2/LightOnCommand.js b/Command/2/LightOnCommand.js deleted file mode 100644 index bce941a..0000000 --- a/Command/2/LightOnCommand.js +++ /dev/null @@ -1,11 +0,0 @@ -var LightOnCommand = function(oLight){ - Command.apply(this); - this.oLight = oLight; -}; -LightOnCommand.prototype = new Command(); -LightOnCommand.prototype.execute = function(){ - this.oLight.on(); -}; -LightOnCommand.prototype.undo = function(){ - this.oLight.off(); -}; diff --git a/Command/2/SimpleRemoteControl.js b/Command/2/SimpleRemoteControl.js deleted file mode 100644 index 1c0b9aa..0000000 --- a/Command/2/SimpleRemoteControl.js +++ /dev/null @@ -1,12 +0,0 @@ -var SimpleRemoteControl = function(){ - this.oCommand = null; -}; -SimpleRemoteControl.prototype.setCommand = function(oCommand){ - this.oCommand = oCommand; -}; -SimpleRemoteControl.prototype.buttonWasPressed = function(){ - this.oCommand.execute(); -}; -SimpleRemoteControl.prototype.buttonUndoWasPressed = function(){ - this.oCommand.undo(); -}; \ No newline at end of file diff --git a/Command/2/index.html b/Command/2/index.html index 0ba32a0..2479cc3 100644 --- a/Command/2/index.html +++ b/Command/2/index.html @@ -2,22 +2,22 @@ Command Pattern - - - -

Source

-var oSimpleRemote = new SimpleRemoteControl();
-var oLight = new Light();
-var oLightCommand = new LightOnCommand(oLight);
+import SimpleRemoteControl from './SimpleRemoteControl';
+import Light from './Light';
+import LightOnCommand from './LightOnCommand';
 
-oSimpleRemote.setCommand(oLightCommand);
-oSimpleRemote.buttonWasPressed();
-oSimpleRemote.buttonUndoWasPressed();
+let simpleRemoteControl = new SimpleRemoteControl();
+let light = new Light();
+let lightCommand = new LightOnCommand(light);
+
+simpleRemoteControl.setCommand(lightCommand);
+simpleRemoteControl.buttonWasPressed();
+simpleRemoteControl.buttonUndoWasPressed();
       
@@ -26,14 +26,6 @@

Console

COMMAND

- + diff --git a/Command/2/scripts/Command.js b/Command/2/scripts/Command.js new file mode 100644 index 0000000..b5791da --- /dev/null +++ b/Command/2/scripts/Command.js @@ -0,0 +1,10 @@ +class Command { + execute() { + throw new Error('This method must be overwritten!'); + } + undo() { + throw new Error('This method must be overwritten!'); + } +} + +export default Command; diff --git a/Command/2/scripts/Light.js b/Command/2/scripts/Light.js new file mode 100644 index 0000000..7f9fd43 --- /dev/null +++ b/Command/2/scripts/Light.js @@ -0,0 +1,15 @@ +class Light { + constructor() { + this._on = false; + } + on() { + this._on = true; + console.log('Light is on'); + } + off(){ + this._on = false; + console.log('Light is off'); + } +} + +export default Light; diff --git a/Command/2/scripts/LightOnCommand.js b/Command/2/scripts/LightOnCommand.js new file mode 100644 index 0000000..525dc4f --- /dev/null +++ b/Command/2/scripts/LightOnCommand.js @@ -0,0 +1,17 @@ +import Command from './Command'; + +class LightOnCommand extends Command{ + constructor(light) { + super(); + this.light = light; + return this; + } + execute() { + this.light.on(); + } + undo() { + this.light.off(); + } +} + +export default LightOnCommand; diff --git a/Command/2/scripts/SimpleRemoteControl.js b/Command/2/scripts/SimpleRemoteControl.js new file mode 100644 index 0000000..112c718 --- /dev/null +++ b/Command/2/scripts/SimpleRemoteControl.js @@ -0,0 +1,16 @@ +class SimpleRemoteControl { + constructor() { + this.command = null; + } + setCommand(command) { + this.command = command; + } + buttonWasPressed() { + this.command.execute(); + } + buttonUndoWasPressed() { + this.command.undo(); + } +} + +export default SimpleRemoteControl; diff --git a/Command/2/scripts/main.js b/Command/2/scripts/main.js new file mode 100644 index 0000000..809f0be --- /dev/null +++ b/Command/2/scripts/main.js @@ -0,0 +1,11 @@ +import SimpleRemoteControl from './SimpleRemoteControl'; +import Light from './Light'; +import LightOnCommand from './LightOnCommand'; + +let simpleRemoteControl = new SimpleRemoteControl(); +let light = new Light(); +let lightCommand = new LightOnCommand(light); + +simpleRemoteControl.setCommand(lightCommand); +simpleRemoteControl.buttonWasPressed(); +simpleRemoteControl.buttonUndoWasPressed(); diff --git a/Composite/CafeMenu.js b/Composite/CafeMenu.js deleted file mode 100644 index 004235e..0000000 --- a/Composite/CafeMenu.js +++ /dev/null @@ -1,4 +0,0 @@ -var CafeMenu = function(){ - Menu.apply(this); -}; -CafeMenu.prototype = new Menu(); \ No newline at end of file diff --git a/Composite/DinnerMenu.js b/Composite/DinnerMenu.js deleted file mode 100644 index 4ecc6e8..0000000 --- a/Composite/DinnerMenu.js +++ /dev/null @@ -1,4 +0,0 @@ -var DinnerMenu = function(){ - Menu.apply(this); -}; -DinnerMenu.prototype = new Menu(); \ No newline at end of file diff --git a/Composite/Mattress.js b/Composite/Mattress.js deleted file mode 100644 index 8acfde1..0000000 --- a/Composite/Mattress.js +++ /dev/null @@ -1,6 +0,0 @@ -var Mattress = function(aMenus){ - this.aMenus = aMenus; -}; -Mattress.prototype.printMenu = function(){ - this.aMenus.print(); -}; \ No newline at end of file diff --git a/Composite/Menu.js b/Composite/Menu.js deleted file mode 100644 index 46a0e5f..0000000 --- a/Composite/Menu.js +++ /dev/null @@ -1,51 +0,0 @@ -var Menu = function(sName, sDescription){ - MenuComponent.apply(this); - this.aMenuComponents = []; - this.sName = sName; - this.sDescription = sDescription; - this.createIterator = function(){ - throw new Error("This method must be overwritten!"); - }; -}; -Menu.prototype = new MenuComponent(); -Menu.prototype.add = function(oMenuComponent){ - this.aMenuComponents.push(oMenuComponent); -}; -Menu.prototype.remove = function(oMenuComponent){ - var aMenuItems = []; - var nMenuItem = 0; - var nLenMenuItems = this.aMenuComponents.length; - var oItem = null; - - for(; nMenuItem < nLenMenuItems;){ - oItem = this.aMenuComponents[nMenuItem]; - if(oItem !== oMenuComponent){ - aMenuItems.push(oItem); - } - nMenuItem = nMenuItem + 1; - } - this.aMenuComponents = aMenuItems; -}; -Menu.prototype.getChild = function(nIndex){ - return this.aMenuComponents[nIndex]; -}; -Menu.prototype.getName = function(){ - return this.sName; -}; -Menu.prototype.getDescription = function(){ - return this.sDescription; -}; -Menu.prototype.print = function(){ - console.log(this.getName() + ": " + this.getDescription()); - console.log("--------------------------------------------"); - - var nMenuComponent = 0; - var nLenMenuComponents = this.aMenuComponents.length; - var oMenuComponent = null; - - for(; nMenuComponent < nLenMenuComponents;){ - oMenuComponent = this.aMenuComponents[nMenuComponent]; - oMenuComponent.print(); - nMenuComponent = nMenuComponent + 1; - } -}; diff --git a/Composite/MenuComponent.js b/Composite/MenuComponent.js deleted file mode 100644 index 0d8ea3c..0000000 --- a/Composite/MenuComponent.js +++ /dev/null @@ -1,26 +0,0 @@ -var MenuComponent = function(){ -}; -MenuComponent.prototype.getName = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.getDescription = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.getPrice = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.isVegetarian = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.print = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.add = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.remove = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.getChild = function(){ - throw new Error("This method must be overwritten!"); -}; \ No newline at end of file diff --git a/Composite/MenuItem.js b/Composite/MenuItem.js deleted file mode 100644 index eb85f5c..0000000 --- a/Composite/MenuItem.js +++ /dev/null @@ -1,23 +0,0 @@ -var MenuItem = function(sName, sDescription, bVegetarian, nPrice){ - MenuComponent.apply(this); - this.sName = sName; - this.sDescription = sDescription; - this.bVegetarian = bVegetarian; - this.nPrice = nPrice; -}; -MenuItem.prototype = new MenuComponent(); -MenuItem.prototype.getName = function(){ - return this.sName; -}; -MenuItem.prototype.getDescription = function(){ - return this.sDescription; -}; -MenuItem.prototype.getPrice = function(){ - return this.nPrice; -}; -MenuItem.prototype.isVegetarian = function(){ - return this.bVegetarian; -}; -MenuItem.prototype.print = function(){ - console.log(this.getName() + ": " + this.getDescription() + ", " + this.getPrice() + "euros"); -}; diff --git a/Composite/PancakeHouseMenu.js b/Composite/PancakeHouseMenu.js deleted file mode 100644 index 67e85ab..0000000 --- a/Composite/PancakeHouseMenu.js +++ /dev/null @@ -1,4 +0,0 @@ -var PancakeHouseMenu = function(){ - Menu.apply(this); -}; -PancakeHouseMenu.prototype = new Menu(); diff --git a/Composite/index.html b/Composite/index.html index eda0647..1075fd3 100644 --- a/Composite/index.html +++ b/Composite/index.html @@ -14,10 +14,14 @@

Source

-var oPanCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast");
-var oDinnerMenu = new Menu("Dinner Menu", "Lunch");
-var oCoffeeMenu = new Menu("Cafe Menu", "Dinner");
-var oAllMenus = new Menu("ALL MENUS", "All menus combined");
+import Menu from './Menu';
+import MenuItem from './MenuItem';
+import Mattress from './Mattress';
+
+let oPanCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast");
+let oDinnerMenu = new Menu("Dinner Menu", "Lunch");
+let oCoffeeMenu = new Menu("Cafe Menu", "Dinner");
+let oAllMenus = new Menu("ALL MENUS", "All menus combined");
 
 oAllMenus.add(oPanCakeHouseMenu);
 oAllMenus.add(oDinnerMenu);
@@ -27,8 +31,10 @@ 

Source

oCoffeeMenu.add(new MenuItem("Express", "Coffee from machine", false, 0.99)); -var oMattress = new Mattress(oAllMenus); +let oMattress = new Mattress(oAllMenus); +console.log("---------------------------------------------"); oMattress.printMenu(); +console.log("---------------------------------------------");
@@ -37,24 +43,6 @@

Console

COMPOSITE

- + diff --git a/Composite/scripts/CafeMenu.js b/Composite/scripts/CafeMenu.js new file mode 100644 index 0000000..000e27d --- /dev/null +++ b/Composite/scripts/CafeMenu.js @@ -0,0 +1,5 @@ +import Menu from './Menu'; + +class CafeMenu extends Menu{} + +export default CafeMenu; diff --git a/Composite/scripts/DinnerMenu.js b/Composite/scripts/DinnerMenu.js new file mode 100644 index 0000000..e3e05a8 --- /dev/null +++ b/Composite/scripts/DinnerMenu.js @@ -0,0 +1,5 @@ +import Menu from './Menu'; + +class DinnerMenu extends Menu {} + +export default DinnerMenu; diff --git a/Composite/scripts/Mattress.js b/Composite/scripts/Mattress.js new file mode 100644 index 0000000..d21a29d --- /dev/null +++ b/Composite/scripts/Mattress.js @@ -0,0 +1,10 @@ +class Mattress { + constructor(aMenus) { + this.aMenus = aMenus; + } + printMenu() { + this.aMenus.print(); + } +} + +export default Mattress; diff --git a/Composite/scripts/Menu.js b/Composite/scripts/Menu.js new file mode 100644 index 0000000..e2c1c6e --- /dev/null +++ b/Composite/scripts/Menu.js @@ -0,0 +1,39 @@ +import MenuComponent from './MenuComponent'; + +class Menu extends MenuComponent { + constructor(name, description) { + super(); + this.menuComponents = []; + this.name = name; + this.description = description; + this.createIterator = function () { + throw new Error('This method must be overwritten!'); + } + } + add(menuComponent) { + this.menuComponents.push(menuComponent); + } + remove(menuComponent) { + this.menuComponents = this.menuComponents.filter(component => { + return component !== component; + }); + } + getChild(index) { + return this.menuComponents[index]; + } + getName() { + return this.name; + } + getDescription() { + return this.description; + } + print() { + console.log(this.getName() + ": " + this.getDescription()); + console.log("--------------------------------------------"); + this.menuComponents.forEach(component => { + component.print(); + }); + } +} + +export default Menu; diff --git a/Composite/scripts/MenuComponent.js b/Composite/scripts/MenuComponent.js new file mode 100644 index 0000000..75cf5d0 --- /dev/null +++ b/Composite/scripts/MenuComponent.js @@ -0,0 +1,28 @@ +class MenuComponent { + getName() { + throw new Error('This method must be overwritten!'); + } + getDescription() { + throw new Error('This method must be overwritten!'); + } + getPrice() { + throw new Error('This method must be overwritten!'); + } + isVegetarian() { + throw new Error('This method must be overwritten!'); + } + print() { + throw new Error('This method must be overwritten!'); + } + add() { + throw new Error('This method must be overwritten!'); + } + remove() { + throw new Error('This method must be overwritten!'); + } + getChild() { + throw new Error('This method must be overwritten!'); + } +} + +export default MenuComponent; diff --git a/Composite/scripts/MenuItem.js b/Composite/scripts/MenuItem.js new file mode 100644 index 0000000..102f7c3 --- /dev/null +++ b/Composite/scripts/MenuItem.js @@ -0,0 +1,29 @@ +import MenuComponent from './MenuComponent'; + +class MenuItem extends MenuComponent { + constructor(name, description, isVegetarian, price) { + super(); + this.name = name; + this.description = description; + this._isVegetarian = isVegetarian; + this.price = price; + return this; + } + getName() { + return this.name; + } + getDescription() { + return this.description; + } + getPrice() { + return this.price; + } + isVegetarian() { + return this._isVegetarian; + } + print() { + console.log(this.getName() + ": " + this.getDescription() + ", " + this.getPrice() + "euros"); + } +} + +export default MenuItem; diff --git a/Composite/scripts/PancakeHouseMenu.js b/Composite/scripts/PancakeHouseMenu.js new file mode 100644 index 0000000..14f04a0 --- /dev/null +++ b/Composite/scripts/PancakeHouseMenu.js @@ -0,0 +1,5 @@ +import Menu from './Menu'; + +class PancakeHouseMenu extends Menu{} + +export default PancakeHouseMenu; diff --git a/Composite/scripts/main.js b/Composite/scripts/main.js new file mode 100644 index 0000000..066af6f --- /dev/null +++ b/Composite/scripts/main.js @@ -0,0 +1,21 @@ +import Menu from './Menu'; +import MenuItem from './MenuItem'; +import Mattress from './Mattress'; + +let oPanCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast"); +let oDinnerMenu = new Menu("Dinner Menu", "Lunch"); +let oCoffeeMenu = new Menu("Cafe Menu", "Dinner"); +let oAllMenus = new Menu("ALL MENUS", "All menus combined"); + +oAllMenus.add(oPanCakeHouseMenu); +oAllMenus.add(oDinnerMenu); + +oDinnerMenu.add(new MenuItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", true, 3.89)); +oDinnerMenu.add(oCoffeeMenu); + +oCoffeeMenu.add(new MenuItem("Express", "Coffee from machine", false, 0.99)); + +let oMattress = new Mattress(oAllMenus); +console.log("---------------------------------------------"); +oMattress.printMenu(); +console.log("---------------------------------------------"); diff --git a/CompositeIterator/1/CafeMenu.js b/CompositeIterator/1/CafeMenu.js deleted file mode 100644 index 004235e..0000000 --- a/CompositeIterator/1/CafeMenu.js +++ /dev/null @@ -1,4 +0,0 @@ -var CafeMenu = function(){ - Menu.apply(this); -}; -CafeMenu.prototype = new Menu(); \ No newline at end of file diff --git a/CompositeIterator/1/CompositeIterator.js b/CompositeIterator/1/CompositeIterator.js deleted file mode 100644 index bb579b6..0000000 --- a/CompositeIterator/1/CompositeIterator.js +++ /dev/null @@ -1,37 +0,0 @@ -var CompositeIterator = function(oIterator){ - Iterator.apply(this); - this.aStack = [oIterator]; -}; -CompositeIterator.prototype = new Iterator(); -CompositeIterator.prototype.hasNext = function(){ - var oIterator = null; - if(this.aStack.length === 0){ - return false; - }else { - oIterator = this.aStack[this.aStack.length]; - if(oIterator.hasNext() === false){ - this.aStack.pop(); - return this.hasNext(); - }else{ - return true; - } - } -}; -CompositeIterator.prototype.next = function(){ - var oIterator = null; - var oMenuComponent = null; - if(this.hasNext()){ - oIterator = this.aStack[this.aStack.length]; - oMenuComponent = oIterator.next(); - if(oMenuComponent instanceof Menu){ - this.aStack.push(oMenuComponent.createIterator()); - } - return oMenuComponent; - }else{ - return null; - } -}; - -CompositeIterator.prototype.remove = function(){ - throw new Error("We are not supporting remove, just traversal"); -}; \ No newline at end of file diff --git a/CompositeIterator/1/ConvertToIterator.js b/CompositeIterator/1/ConvertToIterator.js deleted file mode 100644 index f520d6f..0000000 --- a/CompositeIterator/1/ConvertToIterator.js +++ /dev/null @@ -1,188 +0,0 @@ -var ConvertToIterator = function(oObject, bStopAutoInit) -{ - if (typeof oObject === "undefined" || oObject === null) - { - return null; - } - - this.nLastIndex = -1; - - this.aKeys = []; - - this.length = 0; - - this.aArray = oObject; - - this.bIsArray = this.isArray(this.aArray); - - if(!bStopAutoInit) - { - this._setKeysAndLength(); - } -}; -ConvertToIterator.prototype.isArray = function(aArray) -{ - return Object.prototype.toString.call(aArray) === "[object Array]"; -}; -ConvertToIterator.prototype._setKeysAndLength = function() -{ - var sKey = ''; - - if (this.bIsArray) - { - this.length = this.aArray.length; - }else - { - for (sKey in this.aArray) - { - if (this.aArray.hasOwnProperty(sKey)) - { - this.aKeys.push(sKey); - this.length++; - } - } - this.aKeys.sort(); - } -}; -ConvertToIterator.prototype._getIndexArray = function() -{ - return this.nLastIndex; -}; -ConvertToIterator.prototype._getIndexObject = function() -{ - return this.aKeys[this.nLastIndex]; -}; -ConvertToIterator.prototype.getIndex = function() -{ - if(this.bIsArray) - { - this.getIndex = this._getIndexArray; - }else - { - this.getIndex = this._getIndexObject; - } - this._getIndexObject = this._getIndexArray = null; - return this.getIndex(); -}; -ConvertToIterator.prototype._getCurrentArray = function() -{ - return this.aArray[this.nLastIndex]; -}; -ConvertToIterator.prototype._getCurrentObject = function() -{ - return this.aArray[this.aKeys[this.nLastIndex]]; -}; -ConvertToIterator.prototype.current = function() -{ - if (this.bIsArray) - { - this.current = this._getCurrentArray; - }else - { - this.current = this._getCurrentObject; - } - - return this.current(); -}; -ConvertToIterator.prototype.hasNext = function() -{ - return (this.nLastIndex + 1) < this.length; -}; -ConvertToIterator.prototype.hasPrev = function() -{ - return (this.nLastIndex - 1) > -1; -}; -ConvertToIterator.prototype._nextArray = function() -{ - if (this.hasNext()) - { - return this.aArray[++this.nLastIndex]; - } - return false; -}; -ConvertToIterator.prototype._nextObject = function() -{ - if (this.hasNext()) - { - return this.aArray[this.aKeys[++this.nLastIndex]]; - } - return false; -}; -ConvertToIterator.prototype.next = function() -{ - if(this.length === 0) - { - return false; - } - if (this.bIsArray) - { - this.next = this._nextArray; - this._nextArray = null; - } - else - { - this.next = this._nextObject; - this._nextObject = null; - } - return this.next(); -}; -ConvertToIterator.prototype._prevArray = function() -{ - if (this.hasPrev()) - { - return this.aArray[--this.nLastIndex]; - } - return false; -}; -ConvertToIterator.prototype._prevObject = function() -{ - if (this.hasPrev()) - { - return this.aArray[this.aKeys[--this.nLastIndex]]; - } - return false; -}; -ConvertToIterator.prototype.prev = function() -{ - if(this.length === 0) - { - return false; - } - if (this.bIsArray) - { - this.prev = this._prevArray; - this._prevArray = null; - } - else - { - this.prev = this._prevObject; - this._prevObject = null; - } - return this.prev(); -}; -ConvertToIterator.prototype.movePointerTo = function(nIndex) -{ - if(nIndex > -1 && nIndex < this.length) - { - this.nLastIndex = nIndex; - } -}; -ConvertToIterator.prototype.startPosition = function() -{ - this.movePointerTo(0); -}; -ConvertToIterator.prototype.endPosition = function() -{ - this.movePointerTo(this.length); -}; -ConvertToIterator.prototype.first = function() -{ - this.startPosition(); - return this.current(); -}; -ConvertToIterator.prototype.last = function() -{ - this.endPosition(); - return this.current(); - -}; \ No newline at end of file diff --git a/CompositeIterator/1/DinnerMenu.js b/CompositeIterator/1/DinnerMenu.js deleted file mode 100644 index 4ecc6e8..0000000 --- a/CompositeIterator/1/DinnerMenu.js +++ /dev/null @@ -1,4 +0,0 @@ -var DinnerMenu = function(){ - Menu.apply(this); -}; -DinnerMenu.prototype = new Menu(); \ No newline at end of file diff --git a/CompositeIterator/1/Mattress.js b/CompositeIterator/1/Mattress.js deleted file mode 100644 index 8acfde1..0000000 --- a/CompositeIterator/1/Mattress.js +++ /dev/null @@ -1,6 +0,0 @@ -var Mattress = function(aMenus){ - this.aMenus = aMenus; -}; -Mattress.prototype.printMenu = function(){ - this.aMenus.print(); -}; \ No newline at end of file diff --git a/CompositeIterator/1/Menu.js b/CompositeIterator/1/Menu.js deleted file mode 100644 index 37a7007..0000000 --- a/CompositeIterator/1/Menu.js +++ /dev/null @@ -1,58 +0,0 @@ -var Menu = function(sName, sDescription){ - MenuComponent.apply(this); - this.oIterator = null; - this.aMenuComponents = []; - this.sName = sName; - this.sDescription = sDescription; - this.createIterator = function(){ - throw new Error("This method must be overwritten!"); - }; -}; -Menu.prototype = new MenuComponent(); -Menu.prototype.add = function(oMenuComponent){ - this.aMenuComponents.push(oMenuComponent); -}; -Menu.prototype.remove = function(oMenuComponent){ - var aMenuItems = []; - var nMenuItem = 0; - var nLenMenuItems = this.aMenuComponents.length; - var oItem = null; - - for(; nMenuItem < nLenMenuItems;){ - oItem = this.aMenuComponents[nMenuItem]; - if(oItem !== oMenuComponent){ - aMenuItems.push(oItem); - } - nMenuItem = nMenuItem + 1; - } - this.aMenuComponents = aMenuItems; -}; -Menu.prototype.getChild = function(nIndex){ - return this.aMenuComponents[nIndex]; -}; -Menu.prototype.getName = function(){ - return this.sName; -}; -Menu.prototype.getDescription = function(){ - return this.sDescription; -}; -Menu.prototype.print = function(){ - console.log(this.getName() + ": " + this.getDescription()); - console.log("--------------------------------------------"); - - var nMenuComponent = 0; - var nLenMenuComponents = this.aMenuComponents.length; - var oMenuComponent = null; - - for(; nMenuComponent < nLenMenuComponents;){ - oMenuComponent = this.aMenuComponents[nMenuComponent]; - oMenuComponent.print(); - nMenuComponent = nMenuComponent + 1; - } -}; -Menu.prototype.createIterator = function(){ - if(this.oIterator === null){ - this.oIterator = new CompositeIterator(new ConvertToIterator(this.aMenuComponents)); - } - return this.oIterator; -}; diff --git a/CompositeIterator/1/MenuComponent.js b/CompositeIterator/1/MenuComponent.js deleted file mode 100644 index 04cf316..0000000 --- a/CompositeIterator/1/MenuComponent.js +++ /dev/null @@ -1,29 +0,0 @@ -var MenuComponent = function(){ -}; -MenuComponent.prototype.getName = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.getDescription = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.getPrice = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.isVegetarian = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.print = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.add = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.remove = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.getChild = function(){ - throw new Error("This method must be overwritten!"); -}; -MenuComponent.prototype.createIterator = function(){ - throw new Error("This method must be overwritten!"); -}; diff --git a/CompositeIterator/1/MenuItem.js b/CompositeIterator/1/MenuItem.js deleted file mode 100644 index 1b98fc7..0000000 --- a/CompositeIterator/1/MenuItem.js +++ /dev/null @@ -1,26 +0,0 @@ -var MenuItem = function(sName, sDescription, bVegetarian, nPrice){ - MenuComponent.apply(this); - this.sName = sName; - this.sDescription = sDescription; - this.bVegetarian = bVegetarian; - this.nPrice = nPrice; -}; -MenuItem.prototype = new MenuComponent(); -MenuItem.prototype.getName = function(){ - return this.sName; -}; -MenuItem.prototype.getDescription = function(){ - return this.sDescription; -}; -MenuItem.prototype.getPrice = function(){ - return this.nPrice; -}; -MenuItem.prototype.isVegetarian = function(){ - return this.bVegetarian; -}; -MenuItem.prototype.print = function(){ - console.log(this.getName() + ": " + this.getDescription() + ", " + this.getPrice() + "euros"); -}; -MenuItem.prototype.createIterator = function(){ - return new NullIterator(); -}; \ No newline at end of file diff --git a/CompositeIterator/1/NullIterator.js b/CompositeIterator/1/NullIterator.js deleted file mode 100644 index b602b48..0000000 --- a/CompositeIterator/1/NullIterator.js +++ /dev/null @@ -1,12 +0,0 @@ -var NullIterator = function(){ - Iterator.apply(this); - this.next = function(){ - return null; - }; - this.hasNext = function(){ - return false; - }; - this.remove = function(){ - throw new Error("You can't remove nothing!"); - }; -}; diff --git a/CompositeIterator/1/PancakeHouseMenu.js b/CompositeIterator/1/PancakeHouseMenu.js deleted file mode 100644 index 67e85ab..0000000 --- a/CompositeIterator/1/PancakeHouseMenu.js +++ /dev/null @@ -1,4 +0,0 @@ -var PancakeHouseMenu = function(){ - Menu.apply(this); -}; -PancakeHouseMenu.prototype = new Menu(); diff --git a/CompositeIterator/1/index.html b/CompositeIterator/1/index.html index 74b608d..a8c6573 100644 --- a/CompositeIterator/1/index.html +++ b/CompositeIterator/1/index.html @@ -2,22 +2,15 @@ CompositeIterator Pattern - - - - - - - - - - -

Source

+import Menu from './Menu';
+import MenuItem from './MenuItem';
+import Mattress from './Mattress';
+
 var oPanCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast");
 var oDinnerMenu = new Menu("Dinner Menu", "Lunch");
 var oCoffeeMenu = new Menu("Cafe Menu", "Dinner");
@@ -26,13 +19,19 @@ 

Source

oAllMenus.add(oPanCakeHouseMenu); oAllMenus.add(oDinnerMenu); -oDinnerMenu.add(new MenuItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", true, 3.89)); +oDinnerMenu.add(new MenuItem("Pasta", + "Spaghetti with Marinara Sauce, and a slice of sourdough bread", + true, + 3.89)); oDinnerMenu.add(oCoffeeMenu); oCoffeeMenu.add(new MenuItem("Express", "Coffee from machine", false, 0.99)); var oMattress = new Mattress(oAllMenus); +console.log("---------------------------------------------"); oMattress.printMenu(); +console.log("---------------------------------------------"); +
@@ -41,27 +40,6 @@

Console

COMPOSITE ITERATOR

- + diff --git a/CompositeIterator/1/scripts/CafeMenu.js b/CompositeIterator/1/scripts/CafeMenu.js new file mode 100644 index 0000000..af9458d --- /dev/null +++ b/CompositeIterator/1/scripts/CafeMenu.js @@ -0,0 +1,5 @@ +import Menu from './Menu'; + +class CafeMenu extends Menu {} + +export default CafeMenu; diff --git a/CompositeIterator/1/scripts/CompositeIterator.js b/CompositeIterator/1/scripts/CompositeIterator.js new file mode 100644 index 0000000..880e347 --- /dev/null +++ b/CompositeIterator/1/scripts/CompositeIterator.js @@ -0,0 +1,39 @@ +import Iterator from './Iterator'; + +class CompositeIterator extends Iterator { + constructor(iterator) { + super(); + this.stack = [iterator]; + } + hasNext() { + let iterator = null; + let result = false; + if(this.stack.length !== 0){ + iterator = this.stack[this.stack.length]; + if(iterator.hasNext() === false){ + this.stack.pop(); + result = this.hasNext(); + }else{ + result = true; + } + } + return result; + } + next() { + var result = null; + if(this.hasNext()){ + let iterator = this.stack[this.stack.length]; + let menuComponent = iterator.next(); + if(menuComponent instanceof Menu){ + this.stack.push(menuComponent.createIterator()); + } + result = menuComponent; + } + return result; + } + remove() { + throw new Error("We are not supporting remove, just traversal"); + } +} + +export default CompositeIterator; diff --git a/CompositeIterator/1/scripts/ConvertToIterator.js b/CompositeIterator/1/scripts/ConvertToIterator.js new file mode 100644 index 0000000..a55460a --- /dev/null +++ b/CompositeIterator/1/scripts/ConvertToIterator.js @@ -0,0 +1,133 @@ +function getIndexArray(instance) { + return instance.lastIndex; +} +function getIndexObject(instance) { + return instance.keys[instance.lastIndex]; +} + +function getCurrentArray(instance) { + return instance.array[this.lastIndex]; +} + +function getCurrentObject(instance) { + return instance.array[instance.keys[instance.lastIndex]]; +} + +function nextArray(instance) { + var result = null; + if (instance.hasNext()) { + result = instance.array[++instance.lastIndex]; + } + return result; +} + +function nextObject(instance) { + var result = null; + if (instance.hasNext()) { + result = instance.array[instance.keys[++instance.lastIndex]]; + } + return result; +} + +function prevArray(instance) { + var result = null; + if (instance.hasPrev()) { + result = instance.array[--instance.lastIndex]; + } + return result; +} + +function prevObject(instance) { + var result = null; + if (instance.hasPrev()) { + result = instance.array[instance.keys[--instance.lastIndex]]; + } + return result; +} + +class ConvertToIterator { + constructor(object, stopAutoInit) { + if (object != null) { + this.lastIndex = -1; + this.keys = []; + this.length = 0; + this.array = object; + this.isArray = Array.isArray(object); + + if (!stopAutoInit) { + this.keys = Object.keys(object).sort(); + this.length = this.keys.length; + } + } + } + getIndex() { + if (this.isArray) { + this.getIndex = getIndexArray; + } else { + this.getIndex = getIndexObject; + } + return this.getIndex(this); + } + current() { + if (this.isArray) { + this.current = getCurrentArray; + } else { + this.current = getCurrentObject; + } + + return this.current(this); + } + hasNext() { + return (this.lastIndex + 1) < this.length; + } + hasPrev() { + return (this.lastIndex - 1) > -1; + } + next() { + var result = null; + if (this.length !== 0) { + if (this.isArray) { + this.next = nextArray; + } + else { + this.next = nextObject; + } + result = this.next(this); + } + return result; + } + prev() { + var result = null; + if (this.length !== 0) { + if (this.bIsArray) { + this.prev = prevArray; + } + else { + this.prev = prevObject; + } + result = this.prev(this); + } + return result; + } + movePointerTo(index) { + if (index > -1 && index < this.length) { + this.lastIndex = index; + } + } + startPosition() { + this.movePointerTo(0); + } + endPosition() { + this.movePointerTo(this.length); + } + first() { + this.startPosition(); + return this.current(); + } + last() { + this.endPosition(); + return this.current(); + } +} + +export default ConvertToIterator; diff --git a/CompositeIterator/1/scripts/DinnerMenu.js b/CompositeIterator/1/scripts/DinnerMenu.js new file mode 100644 index 0000000..3b2fd01 --- /dev/null +++ b/CompositeIterator/1/scripts/DinnerMenu.js @@ -0,0 +1,5 @@ +import Menu from './Menu'; + +class DinnerMenu extends Menu{} + +export default DinnerMenu; diff --git a/CompositeIterator/1/Iterator.js b/CompositeIterator/1/scripts/Iterator.js similarity index 57% rename from CompositeIterator/1/Iterator.js rename to CompositeIterator/1/scripts/Iterator.js index a5df685..1be069e 100644 --- a/CompositeIterator/1/Iterator.js +++ b/CompositeIterator/1/scripts/Iterator.js @@ -1,11 +1,13 @@ -var Iterator = function(){ - this.hasNext = function(){ +class Iterator { + hasNext() { throw new Error("This method must be overwritten!"); - }; - this.next = function(){ + } + next() { throw new Error("This method must be overwritten!"); - }; - this.remove = function(){ + } + remove() { throw new Error("This method must be overwritten!"); - }; -}; + } +} + +export default Iterator; diff --git a/CompositeIterator/1/scripts/Mattress.js b/CompositeIterator/1/scripts/Mattress.js new file mode 100644 index 0000000..03e2763 --- /dev/null +++ b/CompositeIterator/1/scripts/Mattress.js @@ -0,0 +1,10 @@ +class Mattress { + constructor(menus) { + this.menus = menus; + } + printMenu() { + this.menus.print(); + } +} + +export default Mattress; diff --git a/CompositeIterator/1/scripts/Menu.js b/CompositeIterator/1/scripts/Menu.js new file mode 100644 index 0000000..53df7a8 --- /dev/null +++ b/CompositeIterator/1/scripts/Menu.js @@ -0,0 +1,49 @@ +import MenuComponent from './MenuComponent'; +import CompositeIterator from './CompositeIterator'; +import ConvertToIterator from './ConvertToIterator'; + +class Menu extends MenuComponent { + constructor(name, description) { + super(); + this.iterator = null; + this.menuComponents = []; + this.name = name; + this.description = description; + this.createIterator = function () { + throw new Error("This method must be overwritten!"); + }; + } + add(menuComponent) { + this.menuComponents.push(menuComponent); + } + remove(menuComponent) { + this.menuComponents = this.menuComponents.filter(component => { + return component !== menuComponent; + }); + } + getChild(index) { + return this.menuComponents[index]; + } + getName() { + return this.name; + } + getDescription() { + return this.description; + } + print() { + console.log(this.getName() + ": " + this.getDescription()); + console.log("--------------------------------------------"); + + this.menuComponents.forEach(component => { + component.print(); + }); + } + createIterator(){ + if(this.iterator === null){ + this.iterator = new CompositeIterator(new ConvertToIterator(this.menuComponents)); + } + return this.iterator; + }; +} + +export default Menu; diff --git a/CompositeIterator/1/scripts/MenuComponent.js b/CompositeIterator/1/scripts/MenuComponent.js new file mode 100644 index 0000000..9ee90ed --- /dev/null +++ b/CompositeIterator/1/scripts/MenuComponent.js @@ -0,0 +1,39 @@ +class MenuComponent { + getName() { + throw new Error("This method must be overwritten!"); + } + + getDescription() { + throw new Error("This method must be overwritten!"); + } + + getPrice() { + throw new Error("This method must be overwritten!"); + } + + isVegetarian() { + throw new Error("This method must be overwritten!"); + } + + print() { + throw new Error("This method must be overwritten!"); + } + + add() { + throw new Error("This method must be overwritten!"); + } + + remove() { + throw new Error("This method must be overwritten!"); + } + + getChild() { + throw new Error("This method must be overwritten!"); + } + + createIterator() { + throw new Error("This method must be overwritten!"); + } +} + +export default MenuComponent; diff --git a/CompositeIterator/1/scripts/MenuItem.js b/CompositeIterator/1/scripts/MenuItem.js new file mode 100644 index 0000000..d0bff4d --- /dev/null +++ b/CompositeIterator/1/scripts/MenuItem.js @@ -0,0 +1,32 @@ +import MenuComponent from './MenuComponent'; +import NullIterator from './NullIterator'; + +class MenuItem extends MenuComponent { + constructor(name, description, isVegetarian, price) { + super(); + this.name = name; + this.description = description; + this._isVegetarian = isVegetarian; + this.price = price; + } + getName() { + return this.sName; + } + getDescription() { + return this.sDescription; + } + getPrice() { + return this.nPrice; + } + isVegetarian() { + return this.bVegetarian; + } + print() { + console.log(this.getName() + ": " + this.getDescription() + ", " + this.getPrice() + "euros"); + } + createIterator() { + return new NullIterator(); + } +} + +export default MenuItem; diff --git a/CompositeIterator/1/scripts/NullIterator.js b/CompositeIterator/1/scripts/NullIterator.js new file mode 100644 index 0000000..9a80c9b --- /dev/null +++ b/CompositeIterator/1/scripts/NullIterator.js @@ -0,0 +1,15 @@ +import Iterator from './Iterator'; + +class NullIterator extends Iterator{ + next() { + return null; + } + hasNext() { + return false; + } + remove() { + throw new Error("You can't remove nothing!"); + } +} + +export default NullIterator; diff --git a/CompositeIterator/1/scripts/PancakeHouseMenu.js b/CompositeIterator/1/scripts/PancakeHouseMenu.js new file mode 100644 index 0000000..9e82409 --- /dev/null +++ b/CompositeIterator/1/scripts/PancakeHouseMenu.js @@ -0,0 +1,5 @@ +import Menu from './Menu'; + +class PancakeHouseMenu extends Menu {}; + +export default PancakeHouseMenu; diff --git a/CompositeIterator/1/scripts/main.js b/CompositeIterator/1/scripts/main.js new file mode 100644 index 0000000..e560817 --- /dev/null +++ b/CompositeIterator/1/scripts/main.js @@ -0,0 +1,24 @@ +import Menu from './Menu'; +import MenuItem from './MenuItem'; +import Mattress from './Mattress'; + +var oPanCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast"); +var oDinnerMenu = new Menu("Dinner Menu", "Lunch"); +var oCoffeeMenu = new Menu("Cafe Menu", "Dinner"); +var oAllMenus = new Menu("ALL MENUS", "All menus combined"); + +oAllMenus.add(oPanCakeHouseMenu); +oAllMenus.add(oDinnerMenu); + +oDinnerMenu.add(new MenuItem("Pasta", + "Spaghetti with Marinara Sauce, and a slice of sourdough bread", + true, + 3.89)); +oDinnerMenu.add(oCoffeeMenu); + +oCoffeeMenu.add(new MenuItem("Express", "Coffee from machine", false, 0.99)); + +var oMattress = new Mattress(oAllMenus); +console.log("---------------------------------------------"); +oMattress.printMenu(); +console.log("---------------------------------------------");