├── .babelrc
├── .gitignore
├── Adapter
├── index.html
└── scripts
│ ├── Duck.js
│ ├── MallardDuck.js
│ ├── Turkey.js
│ ├── TurkeyAdapter.js
│ ├── WildTurkey.js
│ └── main.js
├── Bridge
├── index.html
└── scripts
│ ├── Adventure4X4Car.js
│ ├── Car.js
│ ├── Color.js
│ ├── FamilyCar.js
│ ├── GreyColor.js
│ ├── MatteBlackColor.js
│ ├── RedColor.js
│ ├── UrbanCar.js
│ └── main.js
├── Builder
├── index.html
└── scripts
│ ├── Item.js
│ ├── Meal.js
│ ├── MealBuilder.js
│ ├── Packing.js
│ ├── items
│ ├── Burger.js
│ ├── Drink.js
│ ├── SideDishes.js
│ ├── burgers
│ │ ├── BeefBurger.js
│ │ ├── KobeBurger.js
│ │ └── VeganBurger.js
│ ├── drinks
│ │ ├── Champagne.js
│ │ ├── Coke.js
│ │ └── Water.js
│ └── side-dishes
│ │ ├── Crudettes.js
│ │ ├── Fries.js
│ │ └── Salad.js
│ ├── main.js
│ └── packing
│ ├── Bottle.js
│ ├── BoxUp.js
│ └── Wrapper.js
├── Chaining
├── index.html
└── scripts
│ ├── Chainable.js
│ └── main.js
├── Command
├── 1
│ ├── index.html
│ └── scripts
│ │ ├── Command.js
│ │ └── main.js
├── 2
│ ├── index.html
│ └── scripts
│ │ ├── Command.js
│ │ ├── LightOnCommand.js
│ │ ├── SimpleRemoteControl.js
│ │ └── main.js
└── common
│ ├── Command.js
│ ├── Light.js
│ ├── LightOnCommand.js
│ └── SimpleRemoteControl.js
├── Composite
├── index.html
└── scripts
│ ├── CafeMenu.js
│ ├── LunchMenu.js
│ ├── Mattress.js
│ ├── Menu.js
│ ├── MenuComponent.js
│ ├── MenuItem.js
│ ├── PancakeHouseMenu.js
│ └── main.js
├── CompositeIterator
├── 1
│ ├── index.html
│ └── scripts
│ │ └── main.js
├── 2
│ ├── index.html
│ └── scripts
│ │ ├── Mattress.js
│ │ └── main.js
└── common
│ ├── CafeMenu.js
│ ├── Iterator.js
│ ├── LunchMenu.js
│ ├── Mattress.js
│ ├── Menu.js
│ ├── MenuComponent.js
│ ├── MenuItem.js
│ └── PancakeHouseMenu.js
├── Compound
├── 1
│ ├── index.html
│ └── scripts
│ │ └── main.js
├── 2
│ ├── index.html
│ └── scripts
│ │ ├── CountingDuckFactory.js
│ │ └── main.js
├── 3
│ ├── index.html
│ └── scripts
│ │ └── main.js
├── 4
│ ├── index.html
│ └── scripts
│ │ ├── Flock.js
│ │ ├── MallardDuck.js
│ │ ├── Observable.js
│ │ ├── Observer.js
│ │ ├── QuackObservable.js
│ │ ├── Quackable.js
│ │ ├── Quackologist.js
│ │ ├── RedheadDuck.js
│ │ ├── RubberDuck.js
│ │ └── main.js
└── common
│ ├── AbstractDuckFactory.js
│ ├── CountingDuckFactory.js
│ ├── DuckCall.js
│ ├── DuckFactory.js
│ ├── Flock.js
│ ├── Goose.js
│ ├── GooseAdapter.js
│ ├── MallardDuck.js
│ ├── QuackCounter.js
│ ├── Quackable.js
│ ├── RedheadDuck.js
│ └── RubberDuck.js
├── Decorator
├── index.html
└── scripts
│ ├── Beverage.js
│ ├── CondimentDecorator.js
│ ├── Espresso.js
│ ├── HouseBlend.js
│ ├── Mocha.js
│ ├── Whip.js
│ └── main.js
├── Facade
├── index.html
└── scripts
│ ├── HomeTheaterFacade.js
│ ├── Movie.js
│ ├── Nikita.js
│ ├── elements
│ ├── Amplifier.js
│ ├── CdPlayer.js
│ ├── DvdPlayer.js
│ ├── Playable.js
│ ├── PopcornPopper.js
│ ├── Projector.js
│ ├── Screen.js
│ ├── Switchable.js
│ ├── TheaterLights.js
│ └── Tuner.js
│ └── main.js
├── Factory
├── 1
│ ├── index.html
│ └── scripts
│ │ ├── Pizza.js
│ │ ├── main.js
│ │ ├── pizzas
│ │ ├── ChicagoStyle
│ │ │ ├── BasicPizza.js
│ │ │ ├── CheesePizza.js
│ │ │ ├── ClamPizza.js
│ │ │ ├── PepperoniPizza.js
│ │ │ └── VeggiePizza.js
│ │ └── NewYorkStyle
│ │ │ ├── CheesePizza.js
│ │ │ ├── ClamPizza.js
│ │ │ ├── PepperoniPizza.js
│ │ │ └── VeggiePizza.js
│ │ └── stores
│ │ ├── ChicagoPizzaStore.js
│ │ └── NewYorkPizzaStore.js
├── 2
│ ├── index.html
│ └── scripts
│ │ ├── Pizza.js
│ │ ├── PizzaIngredientFactory.js
│ │ ├── ingredientFactory
│ │ ├── ChicagoPizzaIngredientFactory.js
│ │ └── NewYorkPizzaIngredientFactory.js
│ │ ├── ingredients
│ │ ├── FreshClams.js
│ │ ├── Garlic.js
│ │ ├── MarinaraSauce.js
│ │ ├── Mushroom.js
│ │ ├── Onion.js
│ │ ├── RedPepper.js
│ │ ├── ReggianoCheese.js
│ │ ├── SlicedPepperoni.js
│ │ └── ThinCrustDough.js
│ │ ├── main.js
│ │ ├── pizzas
│ │ ├── CheesePizza.js
│ │ ├── ClamPizza.js
│ │ ├── PepperoniPizza.js
│ │ └── VeggiePizza.js
│ │ └── stores
│ │ ├── ChicagoPizzaStore.js
│ │ └── NewYorkPizzaStore.js
└── common
│ └── PizzaStore.js
├── Flyweight
├── index.html
└── scripts
│ ├── Forest.js
│ ├── Tree.js
│ ├── TreeFactory.js
│ ├── TreeType.js
│ └── main.js
├── Iterator
├── 1
│ ├── index.html
│ └── scripts
│ │ ├── LunchMenu.js
│ │ ├── Mattress.js
│ │ ├── Menu.js
│ │ ├── MenuItem.js
│ │ ├── PancakeHouseMenu.js
│ │ └── main.js
├── 2
│ ├── index.html
│ └── scripts
│ │ ├── LunchMenu.js
│ │ ├── Mattress.js
│ │ ├── PancakeHouseMenu.js
│ │ └── main.js
├── 3
│ ├── index.html
│ └── scripts
│ │ ├── LunchMenu.js
│ │ ├── Mattress.js
│ │ ├── PancakeHouseMenu.js
│ │ └── main.js
├── 4
│ ├── index.html
│ └── scripts
│ │ ├── CafeMenu.js
│ │ ├── LunchMenu.js
│ │ ├── Mattress.js
│ │ ├── PancakeHouseMenu.js
│ │ └── main.js
└── common
│ ├── Iterator.js
│ ├── Menu.js
│ └── MenuItem.js
├── Lazy
├── index.html
└── scripts
│ ├── Lazy.js
│ └── main.js
├── MVC
├── index.html
└── scripts
│ ├── FakeAjaxCall.js
│ ├── ListController.js
│ ├── ListView.js
│ ├── TodoModel.js
│ ├── jquery.js
│ └── main.js
├── Module Revealed
├── index.html
└── scripts
│ ├── ModuleRevealed.js
│ └── main.js
├── Module
├── index.html
└── scripts
│ ├── Module.js
│ └── main.js
├── Multi-Inheritance-ES6
├── index.html
└── scripts
│ ├── Duck.js
│ ├── Flyable.js
│ ├── Quackable.js
│ └── main.js
├── Namespace
├── index.html
└── scripts
│ ├── App.js
│ ├── Namespace.js
│ └── main.js
├── Nullify
├── examples
│ ├── Background_Links.txt
│ ├── problem
│ │ ├── index.html
│ │ └── scripts
│ │ │ └── main.js
│ └── solution
│ │ ├── index.html
│ │ └── scripts
│ │ └── main.js
├── index.html
└── scripts
│ ├── Nullify.js
│ └── main.js
├── Observer
├── index.html
└── scripts
│ ├── CurrentConditionsDisplay.js
│ ├── Displayable.js
│ ├── Observable.js
│ ├── Subject.js
│ ├── WeatherData.js
│ └── main.js
├── Prototype
├── index.html
└── scripts
│ ├── HumanBeing.js
│ └── main.js
├── Proxy
├── 1
│ ├── index.html
│ └── scripts
│ │ ├── PublicLibraryProxy.js
│ │ └── main.js
├── 2
│ ├── index.html
│ └── scripts
│ │ ├── PublicLibraryVirtualProxy.js
│ │ └── main.js
└── common
│ └── PublicLibrary.js
├── README.md
├── Singleton
├── 1
│ ├── index.html
│ └── scripts
│ │ ├── Singleton.js
│ │ └── main.js
├── 2
│ ├── index.html
│ └── scripts
│ │ ├── Singleton.js
│ │ └── main.js
├── 3
│ ├── index.html
│ └── scripts
│ │ ├── Singleton.js
│ │ └── main.js
├── 4
│ ├── index.html
│ └── scripts
│ │ ├── Singleton.js
│ │ └── main.js
└── 5
│ ├── index.html
│ └── scripts
│ ├── DatabaseConnection.js
│ └── main.js
├── State
├── 1
│ ├── index.html
│ └── scripts
│ │ ├── Download.js
│ │ ├── main.js
│ │ └── states
│ │ ├── DownloadFailedState.js
│ │ ├── DownloadPausedState.js
│ │ ├── DownloadedState.js
│ │ ├── DownloadingState.js
│ │ ├── ReadyState.js
│ │ └── State.js
└── 2
│ ├── index.html
│ └── scripts
│ ├── GumballMachine.js
│ ├── main.js
│ └── states
│ ├── HasQuarterState.js
│ ├── NoQuarterState.js
│ ├── SoldOutState.js
│ ├── SoldState.js
│ ├── State.js
│ └── WinnerState.js
├── Strategy
├── 1
│ ├── index.html
│ └── scripts
│ │ └── main.js
├── 2
│ ├── index.html
│ └── scripts
│ │ └── main.js
├── 3
│ ├── index.html
│ └── scripts
│ │ ├── DecoyDuck.js
│ │ ├── Duck.js
│ │ ├── Flyable.js
│ │ ├── MallardDuck.js
│ │ ├── Quackable.js
│ │ ├── RedheadDuck.js
│ │ ├── RubberDuck.js
│ │ └── main.js
├── 4
│ ├── index.html
│ └── scripts
│ │ ├── DecoyDuck.js
│ │ ├── Duck.js
│ │ ├── FlyBehavior.js
│ │ ├── FlyNoWay.js
│ │ ├── FlyWithWings.js
│ │ ├── MallardDuck.js
│ │ ├── MuteQuack.js
│ │ ├── Quack.js
│ │ ├── QuackBehavior.js
│ │ ├── RedheadDuck.js
│ │ ├── RubberDuck.js
│ │ ├── Squeak.js
│ │ └── main.js
├── 2_1
│ ├── index.html
│ └── scripts
│ │ ├── DecoyDuck.js
│ │ └── main.js
└── common
│ ├── Duck.js
│ ├── MallardDuck.js
│ ├── RedheadDuck.js
│ └── RubberDuck.js
├── Template
├── 1
│ ├── index.html
│ └── scripts
│ │ ├── CaffeineBeverage.js
│ │ ├── Coffee.js
│ │ ├── Tea.js
│ │ └── main.js
└── 2
│ ├── index.html
│ └── scripts
│ ├── CaffeineBeverage.js
│ ├── Coffee.js
│ ├── Tea.js
│ └── main.js
├── Try-Finally
├── examples
│ ├── problem
│ │ ├── index.html
│ │ └── scripts
│ │ │ └── main.js
│ └── solution
│ │ ├── index.html
│ │ └── scripts
│ │ └── main.js
├── index.html
└── scripts
│ ├── TryFinally.js
│ └── main.js
├── gulpfile.babel.js
├── package.json
└── statics
├── css
└── style.css
├── img
├── background.jpg
├── consola.png
└── source.jpg
└── js
├── jquery.js
└── utils.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["es2015"]
3 | }
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ### Node template
2 | # Logs
3 | logs
4 | *.log
5 | npm-debug.log*
6 |
7 | # Runtime data
8 | pids
9 | *.pid
10 | *.seed
11 |
12 | # Directory for instrumented libs generated by jscoverage/JSCover
13 | lib-cov
14 |
15 | # Coverage directory used by tools like istanbul
16 | coverage
17 |
18 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
19 | .grunt
20 |
21 | # node-waf configuration
22 | .lock-wscript
23 |
24 | # Compiled binary addons (http://nodejs.org/api/addons.html)
25 | build/Release
26 |
27 | # Dependency directory
28 | # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
29 | node_modules
30 |
31 | # Created by .ignore support plugin (hsz.mobi)
32 |
--------------------------------------------------------------------------------
/Adapter/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Adapter Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import MallardDuck from './MallardDuck';
11 | import WildTurkey from './WildTurkey';
12 | import TurkeyAdapter from './TurkeyAdapter';
13 |
14 | let oMallardDuck = new MallardDuck();
15 | let oWildTurkey = new WildTurkey();
16 | let oTurkeyAdapter = new TurkeyAdapter(oWildTurkey);
17 |
18 | oMallardDuck.fly();
19 | oMallardDuck.quack();
20 |
21 | oWildTurkey.fly();
22 | oWildTurkey.gobble();
23 |
24 | oTurkeyAdapter.fly();
25 | oTurkeyAdapter.quack();
26 |
27 |
28 |
29 |
Console
30 |
31 |
ADAPTER
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/Adapter/scripts/Duck.js:
--------------------------------------------------------------------------------
1 | class Duck {
2 | constructor() {}
3 |
4 | fly() {
5 | throw new Error('This method must be overwritten!');
6 | }
7 |
8 | quack() {
9 | throw new Error('This method must be overwritten!');
10 | }
11 | }
12 |
13 | export default Duck;
14 |
--------------------------------------------------------------------------------
/Adapter/scripts/MallardDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 |
3 | class MallardDuck extends Duck {
4 | fly() {
5 | console.log('Can fly long distances!');
6 | }
7 |
8 | quack() {
9 | console.log('Quack! Quack!');
10 | }
11 | }
12 |
13 | export default MallardDuck;
14 |
--------------------------------------------------------------------------------
/Adapter/scripts/Turkey.js:
--------------------------------------------------------------------------------
1 | class Turkey {
2 | fly() {
3 | throw new Error('This method must be overwritten!');
4 | }
5 |
6 | gobble() {
7 | throw new Error('This method must be overwritten');
8 | }
9 | }
10 |
11 | export default Turkey;
12 |
--------------------------------------------------------------------------------
/Adapter/scripts/TurkeyAdapter.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 |
3 | const MAX_FLIES = 5;
4 |
5 | class TurkeyAdapter extends Duck {
6 | constructor(oTurkey) {
7 | super(oTurkey);
8 | this.oTurkey = oTurkey;
9 | }
10 |
11 | fly() {
12 | for (let index = 0; index < MAX_FLIES; index++) {
13 | this.oTurkey.fly();
14 | }
15 | }
16 |
17 | quack() {
18 | this.oTurkey.gobble();
19 | }
20 | }
21 |
22 | export default TurkeyAdapter;
23 |
--------------------------------------------------------------------------------
/Adapter/scripts/WildTurkey.js:
--------------------------------------------------------------------------------
1 | import Turkey from './Turkey';
2 |
3 | class WildTurkey extends Turkey {
4 | fly() {
5 | console.log('Fly short distance!');
6 | }
7 |
8 | gobble() {
9 | console.log('Gobble!, Gobble!');
10 | }
11 | }
12 |
13 | export default WildTurkey;
14 |
--------------------------------------------------------------------------------
/Adapter/scripts/main.js:
--------------------------------------------------------------------------------
1 | import MallardDuck from './MallardDuck';
2 | import WildTurkey from './WildTurkey';
3 | import TurkeyAdapter from './TurkeyAdapter';
4 |
5 | let oMallardDuck = new MallardDuck();
6 | let oWildTurkey = new WildTurkey();
7 | let oTurkeyAdapter = new TurkeyAdapter(oWildTurkey);
8 |
9 | oMallardDuck.fly();
10 | oMallardDuck.quack();
11 |
12 | oWildTurkey.fly();
13 | oWildTurkey.gobble();
14 |
15 | oTurkeyAdapter.fly();
16 | oTurkeyAdapter.quack();
--------------------------------------------------------------------------------
/Bridge/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Bridge Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import GreyColor from './GreyColor';
11 | import FamilyCar from './FamilyCar';
12 | import MatteBlackColor from './MatteBlackColor';
13 | import Adventure4x4Car from './Adventure4X4Car';
14 | import RedColor from './RedColor';
15 | import UrbanCar from './UrbanCar';
16 |
17 | const familyCar = new FamilyCar(new GreyColor());
18 | const adventureCar = new Adventure4x4Car(new MatteBlackColor());
19 | const urbanCar = new UrbanCar(new RedColor());
20 |
21 | familyCar.applyColor();
22 | adventureCar.applyColor();
23 | urbanCar.applyColor();
24 |
25 |
26 |
27 |
Console
28 |
29 |
BRIDGE
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/Bridge/scripts/Adventure4X4Car.js:
--------------------------------------------------------------------------------
1 | import Car from './Car';
2 |
3 | class Adventure4x4Car extends Car {
4 | constructor(color) {
5 | super('4x4 Adventure car', 'For people that does not care about existing paths', 55000, 2, color);
6 | }
7 | }
8 |
9 | export default Adventure4x4Car;
--------------------------------------------------------------------------------
/Bridge/scripts/Car.js:
--------------------------------------------------------------------------------
1 | class Car {
2 | constructor(name, description, price, places = 2, color, brand = 'Cartisfaction') {
3 | this.brand = brand;
4 | this.name = name;
5 | this.description = description;
6 | this.price = price;
7 | this.places = places;
8 | this.color = color;
9 | }
10 | applyColor() {
11 | console.log(`${this.name} car painted with color ${this.color.applyColor()}`);
12 | }
13 | }
14 |
15 | export default Car;
--------------------------------------------------------------------------------
/Bridge/scripts/Color.js:
--------------------------------------------------------------------------------
1 | class Color {
2 | applyColor() {
3 | throw new Error('This method should be overwritten');
4 | }
5 | }
6 |
7 | export default Color;
--------------------------------------------------------------------------------
/Bridge/scripts/FamilyCar.js:
--------------------------------------------------------------------------------
1 | import Car from './Car';
2 |
3 | class FamilyCar extends Car {
4 | constructor(color) {
5 | super('Family car', 'Enjoy with your family', 30000, 5, color);
6 | }
7 | }
8 |
9 | export default FamilyCar;
--------------------------------------------------------------------------------
/Bridge/scripts/GreyColor.js:
--------------------------------------------------------------------------------
1 | import Color from './Color';
2 |
3 | class GreyColor extends Color {
4 | applyColor() {
5 | return 'grey';
6 | }
7 | }
8 |
9 | export default GreyColor;
--------------------------------------------------------------------------------
/Bridge/scripts/MatteBlackColor.js:
--------------------------------------------------------------------------------
1 | import Color from './Color';
2 |
3 | class MatteBlackColor extends Color {
4 | applyColor() {
5 | return 'matte black';
6 | }
7 | }
8 |
9 | export default MatteBlackColor;
--------------------------------------------------------------------------------
/Bridge/scripts/RedColor.js:
--------------------------------------------------------------------------------
1 | import Color from './Color';
2 |
3 | class RedColor extends Color {
4 | applyColor() {
5 | return 'red';
6 | }
7 | }
8 |
9 | export default RedColor;
--------------------------------------------------------------------------------
/Bridge/scripts/UrbanCar.js:
--------------------------------------------------------------------------------
1 | import Car from './Car';
2 |
3 | class UrbanCar extends Car {
4 | constructor(color) {
5 | super('Urban car', 'Small and designed for the city', 12000, 2, color);
6 | }
7 | }
8 |
9 | export default UrbanCar;
--------------------------------------------------------------------------------
/Bridge/scripts/main.js:
--------------------------------------------------------------------------------
1 | import GreyColor from './GreyColor';
2 | import FamilyCar from './FamilyCar';
3 | import MatteBlackColor from './MatteBlackColor';
4 | import Adventure4x4Car from './Adventure4X4Car';
5 | import RedColor from './RedColor';
6 | import UrbanCar from './UrbanCar';
7 |
8 | const familyCar = new FamilyCar(new GreyColor());
9 | const adventureCar = new Adventure4x4Car(new MatteBlackColor());
10 | const urbanCar = new UrbanCar(new RedColor());
11 |
12 | familyCar.applyColor();
13 | adventureCar.applyColor();
14 | urbanCar.applyColor();
--------------------------------------------------------------------------------
/Builder/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Builder Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import MealBuilder from './MealBuilder';
11 |
12 | var mealBuilder = new MealBuilder();
13 |
14 | var veganMeal = mealBuilder.prepareVeganMeal();
15 | veganMeal.showItems();
16 | console.log(`Vegan meal cost: $${veganMeal.getCost()}`);
17 |
18 | var nonVeganMeal = mealBuilder.prepareNonVeganMeal();
19 | nonVeganMeal.showItems();
20 | console.log(`Non vegan meal cost: $${nonVeganMeal.getCost()}`);
21 |
22 | var deluxeMeal = mealBuilder.prepareDeluxeMeal();
23 | deluxeMeal.showItems();
24 | console.log(`Deluxe meal cost: $${deluxeMeal.getCost()}`);
25 |
26 |
27 |
28 |
Console
29 |
30 |
Builder
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/Builder/scripts/Item.js:
--------------------------------------------------------------------------------
1 | class Item {
2 | get name() {
3 | throw new Error('This method should be implemented');
4 | }
5 | get packing() {
6 | throw new Error('This method should be implemented');
7 | }
8 | get price() {
9 | throw new Error('This method should be implemented');
10 | }
11 | }
12 |
13 | export default Item;
--------------------------------------------------------------------------------
/Builder/scripts/Meal.js:
--------------------------------------------------------------------------------
1 | const itemList = new WeakMap();
2 | class Meal {
3 | get list() {
4 | if(!itemList.get(this)) {
5 | itemList.set(this, []);
6 | }
7 | return itemList.get(this);
8 | }
9 | addItem(item) {
10 | this.list.push(item);
11 | }
12 | getCost() {
13 | return this.list.reduce((accum, item) => {
14 | accum += item.price;
15 | return accum;
16 | }, 0);
17 | }
18 | showItems() {
19 | this.list.forEach((item) => {
20 | console.log(`Item: ${item.name}, Packing: ${item.packing.pack()}, Price: $${item.price}`);
21 | });
22 | }
23 | }
24 |
25 | export default Meal;
--------------------------------------------------------------------------------
/Builder/scripts/MealBuilder.js:
--------------------------------------------------------------------------------
1 | import Meal from './Meal';
2 | import VeganBurger from './items/burgers/VeganBurger';
3 | import BeefBurger from './items/burgers/BeefBurger';
4 | import KobeBurger from './items/burgers/KobeBurger';
5 | import Water from './items/drinks/Water';
6 | import Coke from './items/drinks/Coke';
7 | import Champagne from './items/drinks/Champagne';
8 | import Salad from './items/side-dishes/Salad';
9 | import Fries from './items/side-dishes/Fries';
10 | import Crudettes from './items/side-dishes/Crudettes';
11 |
12 | class MealBuilder {
13 | prepareVeganMeal() {
14 | var meal = new Meal();
15 | meal.addItem(new VeganBurger());
16 | meal.addItem(new Water());
17 | meal.addItem(new Salad());
18 | return meal;
19 | }
20 | prepareNonVeganMeal() {
21 | var meal = new Meal();
22 | meal.addItem(new BeefBurger());
23 | meal.addItem(new Coke());
24 | meal.addItem(new Fries());
25 | return meal;
26 | }
27 | prepareDeluxeMeal() {
28 | var meal = new Meal();
29 | meal.addItem(new KobeBurger());
30 | meal.addItem(new Champagne());
31 | meal.addItem(new Crudettes());
32 | return meal;
33 | }
34 | }
35 |
36 | export default MealBuilder;
--------------------------------------------------------------------------------
/Builder/scripts/Packing.js:
--------------------------------------------------------------------------------
1 | class Packing {
2 | pack() {
3 | throw new Error('This method should be implemented');
4 | }
5 | }
6 |
7 | export default Packing;
--------------------------------------------------------------------------------
/Builder/scripts/items/Burger.js:
--------------------------------------------------------------------------------
1 | import Wrapper from '../packing/Wrapper';
2 | import Item from '../Item';
3 |
4 | class Burger extends Item {
5 | get packing() {
6 | return new Wrapper();
7 | }
8 | }
9 |
10 | export default Burger;
--------------------------------------------------------------------------------
/Builder/scripts/items/Drink.js:
--------------------------------------------------------------------------------
1 | import Bottle from '../packing/Bottle';
2 | import Item from '../Item';
3 |
4 | class Drink extends Item {
5 | get packing() {
6 | return new Bottle();
7 | }
8 | }
9 |
10 | export default Drink;
--------------------------------------------------------------------------------
/Builder/scripts/items/SideDishes.js:
--------------------------------------------------------------------------------
1 | import BoxUp from '../packing/BoxUp';
2 | import Item from '../Item';
3 |
4 | class SideDishes extends Item {
5 | get packing() {
6 | return new BoxUp();
7 | }
8 | }
9 |
10 | export default SideDishes;
--------------------------------------------------------------------------------
/Builder/scripts/items/burgers/BeefBurger.js:
--------------------------------------------------------------------------------
1 | import Burger from '../Burger';
2 |
3 | class BeefBurger extends Burger {
4 | get price() {
5 | return 13;
6 | }
7 | get name() {
8 | return 'Beef Burger';
9 | }
10 | }
11 |
12 | export default BeefBurger;
--------------------------------------------------------------------------------
/Builder/scripts/items/burgers/KobeBurger.js:
--------------------------------------------------------------------------------
1 | import Burger from '../Burger';
2 |
3 | class KobeBurger extends Burger {
4 | get price() {
5 | return 235;
6 | }
7 | get name() {
8 | return 'Kobe Burger';
9 | }
10 | }
11 |
12 | export default KobeBurger;
--------------------------------------------------------------------------------
/Builder/scripts/items/burgers/VeganBurger.js:
--------------------------------------------------------------------------------
1 | import Burger from '../Burger';
2 |
3 | class VeganBurger extends Burger {
4 | get price() {
5 | return 16;
6 | }
7 | get name() {
8 | return 'Vegan Burger';
9 | }
10 | }
11 |
12 | export default VeganBurger;
--------------------------------------------------------------------------------
/Builder/scripts/items/drinks/Champagne.js:
--------------------------------------------------------------------------------
1 | import Drink from '../Drink';
2 |
3 | class Champagne extends Drink {
4 | get price() {
5 | return 90;
6 | }
7 | get name() {
8 | return 'Champagne';
9 | }
10 | }
11 |
12 | export default Champagne;
--------------------------------------------------------------------------------
/Builder/scripts/items/drinks/Coke.js:
--------------------------------------------------------------------------------
1 | import Drink from '../Drink';
2 |
3 | class Coke extends Drink {
4 | get price() {
5 | return 3.5;
6 | }
7 | get name() {
8 | return 'Coke';
9 | }
10 | }
11 |
12 | export default Coke;
--------------------------------------------------------------------------------
/Builder/scripts/items/drinks/Water.js:
--------------------------------------------------------------------------------
1 | import Drink from '../Drink';
2 |
3 | class Water extends Drink {
4 | get price() {
5 | return 2.5;
6 | }
7 | get name() {
8 | return 'Water';
9 | }
10 | }
11 |
12 | export default Water;
--------------------------------------------------------------------------------
/Builder/scripts/items/side-dishes/Crudettes.js:
--------------------------------------------------------------------------------
1 | import SideDishes from '../SideDishes';
2 |
3 | class Crudettes extends SideDishes {
4 | get price() {
5 | return 4;
6 | }
7 | get name() {
8 | return 'Crudettes';
9 | }
10 | }
11 |
12 | export default Crudettes;
--------------------------------------------------------------------------------
/Builder/scripts/items/side-dishes/Fries.js:
--------------------------------------------------------------------------------
1 | import SideDishes from '../SideDishes';
2 |
3 | class Fries extends SideDishes {
4 | get price() {
5 | return 2;
6 | }
7 | get name() {
8 | return 'Fries';
9 | }
10 | }
11 |
12 | export default Fries;
--------------------------------------------------------------------------------
/Builder/scripts/items/side-dishes/Salad.js:
--------------------------------------------------------------------------------
1 | import SideDishes from '../SideDishes';
2 |
3 | class Salad extends SideDishes {
4 | get price() {
5 | return 6;
6 | }
7 | get name() {
8 | return 'Salad';
9 | }
10 | }
11 |
12 | export default Salad;
--------------------------------------------------------------------------------
/Builder/scripts/main.js:
--------------------------------------------------------------------------------
1 | import MealBuilder from './MealBuilder';
2 |
3 | var mealBuilder = new MealBuilder();
4 |
5 | var veganMeal = mealBuilder.prepareVeganMeal();
6 |
7 | veganMeal.showItems();
8 | console.log(`Vegan meal cost: $${veganMeal.getCost()}`);
9 |
10 | var nonVeganMeal = mealBuilder.prepareNonVeganMeal();
11 |
12 | nonVeganMeal.showItems();
13 | console.log(`Non vegan meal cost: $${nonVeganMeal.getCost()}`);
14 |
15 | var deluxeMeal = mealBuilder.prepareDeluxeMeal();
16 |
17 | deluxeMeal.showItems();
18 | console.log(`Deluxe meal cost: $${deluxeMeal.getCost()}`);
19 |
20 |
--------------------------------------------------------------------------------
/Builder/scripts/packing/Bottle.js:
--------------------------------------------------------------------------------
1 | import Packing from '../Packing';
2 |
3 | class Bottle extends Packing {
4 | pack() {
5 | return 'Bottle';
6 | }
7 | }
8 |
9 | export default Bottle;
--------------------------------------------------------------------------------
/Builder/scripts/packing/BoxUp.js:
--------------------------------------------------------------------------------
1 | import Packing from '../Packing';
2 |
3 | class BoxUp extends Packing {
4 | pack() {
5 | return 'Boxing';
6 | }
7 | }
8 |
9 | export default BoxUp;
--------------------------------------------------------------------------------
/Builder/scripts/packing/Wrapper.js:
--------------------------------------------------------------------------------
1 | import Packing from '../Packing';
2 |
3 | class Wrapper extends Packing {
4 | pack() {
5 | return 'Wrapping';
6 | }
7 | }
8 |
9 | export default Wrapper;
--------------------------------------------------------------------------------
/Chaining/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Chain Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Chainable from './Chainable';
11 |
12 | let chainable = new Chainable();
13 |
14 | console.log(chainable.add(3).add(4).multiply(3));
15 |
16 |
17 |
18 |
Console
19 |
20 |
Chain
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/Chaining/scripts/Chainable.js:
--------------------------------------------------------------------------------
1 | class Chainable {
2 | constructor() {
3 | this.number = 0;
4 | }
5 |
6 | add(number) {
7 | this.number += number;
8 | return this;
9 | }
10 |
11 | multiply(number) {
12 | this.number *= number;
13 | return this;
14 | }
15 |
16 | toString() {
17 | return this.number.toString();
18 | }
19 | }
20 |
21 | export default Chainable;
22 |
--------------------------------------------------------------------------------
/Chaining/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Chainable from './Chainable';
2 |
3 | let chainable = new Chainable();
4 |
5 | console.log(chainable.add(3).add(4).multiply(3));
6 |
--------------------------------------------------------------------------------
/Command/1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Command Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Light from '../../common/Light';
11 | import LightOnCommand from '../../common/LightOnCommand';
12 | import SimpleRemoteControl from '../../common/SimpleRemoteControl';
13 |
14 | let oSimpleRemote = new SimpleRemoteControl();
15 | let oLight = new Light();
16 | let oLightCommand = new LightOnCommand(oLight);
17 |
18 | oSimpleRemote.setCommand(oLightCommand);
19 | oSimpleRemote.buttonWasPressed();
20 |
21 |
22 |
23 |
Console
24 |
25 |
COMMAND
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/Command/1/scripts/Command.js:
--------------------------------------------------------------------------------
1 | class Command {
2 | execute() {
3 | throw new Error('This method must be overwritten!');
4 | }
5 | }
6 |
7 | export default Command;
8 |
--------------------------------------------------------------------------------
/Command/1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Light from '../../common/Light';
2 | import LightOnCommand from '../../common/LightOnCommand';
3 | import SimpleRemoteControl from '../../common/SimpleRemoteControl';
4 |
5 | let oSimpleRemote = new SimpleRemoteControl();
6 | let oLight = new Light();
7 | let oLightCommand = new LightOnCommand(oLight);
8 |
9 | oSimpleRemote.setCommand(oLightCommand);
10 | oSimpleRemote.buttonWasPressed();
11 |
--------------------------------------------------------------------------------
/Command/2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Command Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import SimpleRemoteControl from './SimpleRemoteControl';
11 | import Light from '../../common/Light';
12 | import LightOnCommand from './LightOnCommand';
13 |
14 | let simpleRemoteControl = new SimpleRemoteControl();
15 | let light = new Light();
16 | let lightCommand = new LightOnCommand(light);
17 |
18 | simpleRemoteControl.setCommand(lightCommand);
19 | simpleRemoteControl.buttonWasPressed();
20 | simpleRemoteControl.buttonUndoWasPressed();
21 |
22 |
23 |
24 |
Console
25 |
26 |
COMMAND
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/Command/2/scripts/Command.js:
--------------------------------------------------------------------------------
1 | import Command from '../../common/Command';
2 |
3 | class CommandWithUndo extends Command {
4 | undo() {
5 | throw new Error('This method must be overwritten!');
6 | }
7 | }
8 |
9 | export default CommandWithUndo;
10 |
--------------------------------------------------------------------------------
/Command/2/scripts/LightOnCommand.js:
--------------------------------------------------------------------------------
1 | import LightOnCommand from '../../common/LightOnCommand';
2 | ;
3 |
4 | class LightOnCommand2 extends LightOnCommand {
5 | undo() {
6 | this.light.off();
7 | }
8 | }
9 |
10 | export default LightOnCommand2;
11 |
--------------------------------------------------------------------------------
/Command/2/scripts/SimpleRemoteControl.js:
--------------------------------------------------------------------------------
1 | import SimpleRemoteControl from '../../common/SimpleRemoteControl';
2 |
3 | class SimpleRemoteControl2 extends SimpleRemoteControl {
4 | buttonUndoWasPressed() {
5 | this.command.undo();
6 | }
7 | }
8 |
9 | export default SimpleRemoteControl2;
10 |
--------------------------------------------------------------------------------
/Command/2/scripts/main.js:
--------------------------------------------------------------------------------
1 | import SimpleRemoteControl from './SimpleRemoteControl';
2 | import Light from '../../common/Light';
3 | import LightOnCommand from './LightOnCommand';
4 |
5 | let simpleRemoteControl = new SimpleRemoteControl();
6 | let light = new Light();
7 | let lightCommand = new LightOnCommand(light);
8 |
9 | simpleRemoteControl.setCommand(lightCommand);
10 | simpleRemoteControl.buttonWasPressed();
11 | simpleRemoteControl.buttonUndoWasPressed();
12 |
--------------------------------------------------------------------------------
/Command/common/Command.js:
--------------------------------------------------------------------------------
1 | class Command {
2 | execute() {
3 | throw new Error('This method must be overwritten!');
4 | }
5 | }
6 |
7 | export default Command;
8 |
--------------------------------------------------------------------------------
/Command/common/Light.js:
--------------------------------------------------------------------------------
1 | class Light {
2 | constructor() {
3 | this._on = false;
4 | }
5 |
6 | on() {
7 | this._on = true;
8 | console.log('Light is on');
9 | }
10 |
11 | off() {
12 | this._on = false;
13 | console.log('Light is off');
14 | }
15 | }
16 |
17 | export default Light;
18 |
--------------------------------------------------------------------------------
/Command/common/LightOnCommand.js:
--------------------------------------------------------------------------------
1 | import Command from './Command';
2 |
3 | class LightOnCommand extends Command {
4 | constructor(light) {
5 | super();
6 | this.light = light;
7 | }
8 |
9 | execute() {
10 | this.light.on();
11 | }
12 | }
13 |
14 | export default LightOnCommand;
15 |
--------------------------------------------------------------------------------
/Command/common/SimpleRemoteControl.js:
--------------------------------------------------------------------------------
1 | class SimpleRemoteControl {
2 | constructor() {
3 | this.command = null;
4 | }
5 |
6 | setCommand(command) {
7 | this.command = command;
8 | }
9 |
10 | buttonWasPressed() {
11 | this.command.execute();
12 | }
13 | }
14 |
15 | export default SimpleRemoteControl;
16 |
--------------------------------------------------------------------------------
/Composite/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Composite Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Menu from './Menu';
11 | import MenuItem from './MenuItem';
12 | import Mattress from './Mattress';
13 |
14 | let oPanCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast");
15 | let oLunchMenu = new Menu("Dinner Menu", "Lunch");
16 | let oCoffeeMenu = new Menu("Cafe Menu", "Lunch");
17 | let oAllMenus = new Menu("ALL MENUS", "All menus combined");
18 |
19 | oAllMenus.add(oPanCakeHouseMenu);
20 | oAllMenus.add(oLunchMenu);
21 |
22 | oLunchMenu.add(new MenuItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", true, 3.89));
23 | oLunchMenu.add(oCoffeeMenu);
24 |
25 | oCoffeeMenu.add(new MenuItem("Express", "Coffee from machine", false, 0.99));
26 |
27 | let oMattress = new Mattress(oAllMenus);
28 | console.log("---------------------------------------------");
29 | oMattress.printMenu();
30 | console.log("---------------------------------------------");
31 |
32 |
33 |
34 |
Console
35 |
36 |
COMPOSITE
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Composite/scripts/CafeMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from './Menu';
2 |
3 | class CafeMenu extends Menu {}
4 |
5 | export default CafeMenu;
6 |
--------------------------------------------------------------------------------
/Composite/scripts/LunchMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from './Menu';
2 |
3 | class LunchMenu extends Menu {}
4 |
5 | export default LunchMenu;
6 |
--------------------------------------------------------------------------------
/Composite/scripts/Mattress.js:
--------------------------------------------------------------------------------
1 | class Mattress {
2 | constructor(aMenus) {
3 | this.menus = aMenus;
4 | }
5 |
6 | printMenu() {
7 | this.menus.print();
8 | }
9 | }
10 |
11 | export default Mattress;
12 |
--------------------------------------------------------------------------------
/Composite/scripts/Menu.js:
--------------------------------------------------------------------------------
1 | import MenuComponent from './MenuComponent';
2 |
3 | class Menu extends MenuComponent {
4 | constructor(name, description) {
5 | super();
6 | this.menuComponents = [];
7 | this.name = name;
8 | this.description = description;
9 | this.createIterator = function() {
10 | throw new Error('This method must be overwritten!');
11 | }
12 | }
13 |
14 | add(menuComponent) {
15 | this.menuComponents.push(menuComponent);
16 | }
17 |
18 | remove(menuComponent) {
19 | this.menuComponents = this.menuComponents.filter(component => {
20 | return component !== component;
21 | });
22 | }
23 |
24 | getChild(index) {
25 | return this.menuComponents[index];
26 | }
27 |
28 | getName() {
29 | return this.name;
30 | }
31 |
32 | getDescription() {
33 | return this.description;
34 | }
35 |
36 | print() {
37 | console.log(this.getName() + ": " + this.getDescription());
38 | console.log("--------------------------------------------");
39 | this.menuComponents.forEach(component => {
40 | component.print();
41 | });
42 | }
43 | }
44 |
45 | export default Menu;
46 |
--------------------------------------------------------------------------------
/Composite/scripts/MenuComponent.js:
--------------------------------------------------------------------------------
1 | class MenuComponent {
2 | constructor(name = '', description = '', isVegetarian = false, price = 0) {
3 | this.name = name;
4 | this.description = description;
5 | this._isVegetarian = isVegetarian;
6 | this.price = price;
7 | }
8 |
9 | getName() {
10 | return this.name;
11 | }
12 |
13 | getDescription() {
14 | return this.description;
15 | }
16 |
17 | getPrice() {
18 | return this.price;
19 | }
20 |
21 | isVegetarian() {
22 | return this._isVegetarian;
23 | }
24 |
25 | print() {
26 | shouldBeOverwritten();
27 | }
28 |
29 | add() {
30 | shouldBeOverwritten();
31 | }
32 |
33 | remove() {
34 | shouldBeOverwritten();
35 | }
36 |
37 | getChild() {
38 | shouldBeOverwritten();
39 | }
40 | }
41 |
42 | export default MenuComponent;
43 |
--------------------------------------------------------------------------------
/Composite/scripts/MenuItem.js:
--------------------------------------------------------------------------------
1 | import MenuComponent from './MenuComponent';
2 |
3 | class MenuItem extends MenuComponent {
4 | print() {
5 | console.log(this.getName() + ": " + this.getDescription() + ", " + this.getPrice() + "euros");
6 | }
7 | }
8 |
9 | export default MenuItem;
10 |
--------------------------------------------------------------------------------
/Composite/scripts/PancakeHouseMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from './Menu';
2 |
3 | class PancakeHouseMenu extends Menu {}
4 |
5 | export default PancakeHouseMenu;
6 |
--------------------------------------------------------------------------------
/Composite/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Menu from './Menu';
2 | import MenuItem from './MenuItem';
3 | import Mattress from './Mattress';
4 |
5 | let oPanCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast");
6 | let oLunchMenu = new Menu("Lunch Menu", "Lunch");
7 | let oCoffeeMenu = new Menu("Cafe Menu", "Dinner");
8 | let oAllMenus = new Menu("ALL MENUS", "All menus combined");
9 |
10 | oAllMenus.add(oPanCakeHouseMenu);
11 | oAllMenus.add(oLunchMenu);
12 |
13 | oLunchMenu.add(new MenuItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", true, 3.89));
14 | oLunchMenu.add(oCoffeeMenu);
15 |
16 | oCoffeeMenu.add(new MenuItem("Express", "Coffee from machine", false, 0.99));
17 |
18 | let oMattress = new Mattress(oAllMenus);
19 | console.log("---------------------------------------------");
20 | oMattress.printMenu();
21 | console.log("---------------------------------------------");
22 |
--------------------------------------------------------------------------------
/CompositeIterator/1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | CompositeIterator Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Menu from '../../common/Menu';
11 | import MenuItem from '../../common/MenuItem';
12 | import Mattress from '../../common/Mattress';
13 |
14 | var oPanCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast");
15 | var oLunchMenu = new Menu("Lunch Menu", "Lunch");
16 | var oCoffeeMenu = new Menu("Cafe Menu", "Lunch");
17 | var oAllMenus = new Menu("ALL MENUS", "All menus combined");
18 |
19 | oAllMenus.add(oPanCakeHouseMenu);
20 | oAllMenus.add(oLunchMenu);
21 |
22 | oLunchMenu.add(new MenuItem("Pasta",
23 | "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
24 | true,
25 | 3.89));
26 | oLunchMenu.add(oCoffeeMenu);
27 |
28 | oCoffeeMenu.add(new MenuItem("Express", "Coffee from machine", false, 0.99));
29 |
30 | var oMattress = new Mattress(oAllMenus);
31 | console.log("---------------------------------------------");
32 | oMattress.printMenu();
33 | console.log("---------------------------------------------");
34 |
35 |
36 |
37 |
Console
38 |
39 |
COMPOSITE ITERATOR
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/CompositeIterator/1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Menu from '../../common/Menu';
2 | import MenuItem from '../../common/MenuItem';
3 | import Mattress from '../../common/Mattress';
4 |
5 | var oPanCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast");
6 | var oLunchMenu = new Menu("Dinner Menu", "Lunch");
7 | var oCoffeeMenu = new Menu("Cafe Menu", "Lunch");
8 | var oAllMenus = new Menu("ALL MENUS", "All menus combined");
9 |
10 | oAllMenus.add(oPanCakeHouseMenu);
11 | oAllMenus.add(oLunchMenu);
12 |
13 | oLunchMenu.add(new MenuItem("Pasta",
14 | "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
15 | true,
16 | 3.89));
17 | oLunchMenu.add(oCoffeeMenu);
18 |
19 | oCoffeeMenu.add(new MenuItem("Express", "Coffee from machine", false, 0.99));
20 |
21 | var oMattress = new Mattress(oAllMenus);
22 | console.log("---------------------------------------------");
23 | oMattress.printMenu();
24 | console.log("---------------------------------------------");
25 |
--------------------------------------------------------------------------------
/CompositeIterator/2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | CompositeIterator Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Menu from '../../common/Menu';
11 | import MenuItem from '../../common/MenuItem';
12 | import Mattress from './Mattress';
13 |
14 | let panCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast");
15 | let lunchMenu = new Menu("Lunch Menu", "Lunch");
16 | let coffeeMenu = new Menu("Cafe Menu", "Dinner");
17 | let allMenus = new Menu("ALL MENUS", "All menus combined");
18 |
19 | allMenus.add(panCakeHouseMenu);
20 | allMenus.add(lunchMenu);
21 |
22 | lunchMenu.add(new MenuItem("Pasta",
23 | "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
24 | true,
25 | 3.89));
26 | lunchMenu.add(coffeeMenu);
27 |
28 |
29 | coffeeMenu.add(new MenuItem("Express", "Coffee from machine", false, 0.99));
30 |
31 | let mattress = new Mattress(allMenus);
32 | console.log("---------------------------------------------");
33 | mattress.printVegetarianMenu();
34 | console.log("---------------------------------------------");
35 |
36 |
37 |
38 |
Console
39 |
40 |
COMPOSITE ITERATOR
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/CompositeIterator/2/scripts/Mattress.js:
--------------------------------------------------------------------------------
1 | import Mattress from '../../common/Mattress';
2 |
3 | class Mattress2 extends Mattress {
4 | printVegetarianMenu() {
5 | let iterator = this.menus.menuComponents[Symbol.iterator]();
6 | let menu = iterator.next();
7 | console.log("VEGETARIAN MENU");
8 | while (menu.value) {
9 | let menuComponentsIterator = menu.value.menuComponents[Symbol.iterator]();
10 | let menuComponent = menuComponentsIterator.next();
11 | while (menuComponent.value) {
12 | try {
13 | if (menuComponent.value.isVegetarian()) {
14 | menuComponent.value.print();
15 | }
16 | } catch (error) {}
17 | menuComponent = menuComponentsIterator.next();
18 | }
19 | menu = iterator.next();
20 | }
21 | }
22 | }
23 |
24 | export default Mattress2;
25 |
--------------------------------------------------------------------------------
/CompositeIterator/2/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Menu from '../../common/Menu';
2 | import MenuItem from '../../common/MenuItem';
3 | import Mattress from './Mattress';
4 |
5 | let panCakeHouseMenu = new Menu("Pancake House Menu", "Breakfast");
6 | let lunchMenu = new Menu("Dinner Menu", "Lunch");
7 | let coffeeMenu = new Menu("Cafe Menu", "Lunch");
8 | let allMenus = new Menu("ALL MENUS", "All menus combined");
9 |
10 | allMenus.add(panCakeHouseMenu);
11 | allMenus.add(lunchMenu);
12 |
13 | lunchMenu.add(new MenuItem("Pasta",
14 | "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
15 | true,
16 | 3.89));
17 | lunchMenu.add(coffeeMenu);
18 |
19 |
20 | coffeeMenu.add(new MenuItem("Express", "Coffee from machine", false, 0.99));
21 |
22 | let mattress = new Mattress(allMenus);
23 | console.log("---------------------------------------------");
24 | mattress.printVegetarianMenu();
25 | console.log("---------------------------------------------");
26 |
--------------------------------------------------------------------------------
/CompositeIterator/common/CafeMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from './Menu';
2 |
3 | class CafeMenu extends Menu {}
4 |
5 | export default CafeMenu;
6 |
--------------------------------------------------------------------------------
/CompositeIterator/common/Iterator.js:
--------------------------------------------------------------------------------
1 | class Iterator {
2 | hasNext() {
3 | throw new Error("This method must be overwritten!");
4 | }
5 |
6 | next() {
7 | throw new Error("This method must be overwritten!");
8 | }
9 |
10 | remove() {
11 | throw new Error("This method must be overwritten!");
12 | }
13 | }
14 |
15 | export default Iterator;
16 |
--------------------------------------------------------------------------------
/CompositeIterator/common/LunchMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from './Menu';
2 |
3 | class LunchMenu extends Menu {}
4 |
5 | export default LunchMenu;
6 |
--------------------------------------------------------------------------------
/CompositeIterator/common/Mattress.js:
--------------------------------------------------------------------------------
1 | class Mattress {
2 | constructor(menus) {
3 | this.menus = menus;
4 | }
5 |
6 | printMenu() {
7 | this.menus.print();
8 | }
9 | }
10 |
11 | export default Mattress;
12 |
--------------------------------------------------------------------------------
/CompositeIterator/common/Menu.js:
--------------------------------------------------------------------------------
1 | import MenuComponent from './MenuComponent';
2 |
3 | class Menu extends MenuComponent {
4 | constructor(name, description) {
5 | super();
6 | this.iterator = null;
7 | this.menuComponents = [];
8 | this.name = name;
9 | this.description = description;
10 | }
11 |
12 | add(menuComponent) {
13 | this.menuComponents.push(menuComponent);
14 | }
15 |
16 | remove(menuComponent) {
17 | this.menuComponents = this.menuComponents.filter(component => {
18 | return component !== menuComponent;
19 | });
20 | }
21 |
22 | getChild(index) {
23 | return this.menuComponents[index];
24 | }
25 |
26 | getName() {
27 | return this.name;
28 | }
29 |
30 | getDescription() {
31 | return this.description;
32 | }
33 |
34 | print() {
35 | console.log(this.getName() + ": " + this.getDescription());
36 | console.log("--------------------------------------------");
37 |
38 | for(let component of this.menuComponents) {
39 | component.print();
40 | }
41 | }
42 |
43 | createIterator() {
44 | if (this.iterator === null) {
45 | this.iterator = this.menuComponents[Symbol.iterator]();
46 | }
47 | return this.iterator;
48 | };
49 | }
50 |
51 | export default Menu;
52 |
--------------------------------------------------------------------------------
/CompositeIterator/common/MenuComponent.js:
--------------------------------------------------------------------------------
1 | class MenuComponent {
2 | constructor(name, description, isVegetarian, price) {
3 | this.name = name;
4 | this.description = description;
5 | this._isVegetarian = isVegetarian;
6 | this.price = price;
7 | }
8 |
9 | isVegetarian() {
10 | return !!this._isVegetarian;
11 | }
12 |
13 | getName() {
14 | throw new Error("This method must be overwritten!");
15 | }
16 |
17 | getDescription() {
18 | throw new Error("This method must be overwritten!");
19 | }
20 |
21 | getPrice() {
22 | throw new Error("This method must be overwritten!");
23 | }
24 |
25 | print() {
26 | throw new Error("This method must be overwritten!");
27 | }
28 |
29 | add() {
30 | throw new Error("This method must be overwritten!");
31 | }
32 |
33 | remove() {
34 | throw new Error("This method must be overwritten!");
35 | }
36 |
37 | getChild() {
38 | throw new Error("This method must be overwritten!");
39 | }
40 |
41 | createIterator() {
42 | throw new Error("This method must be overwritten!");
43 | }
44 | }
45 |
46 | export default MenuComponent;
47 |
--------------------------------------------------------------------------------
/CompositeIterator/common/MenuItem.js:
--------------------------------------------------------------------------------
1 | import MenuComponent from './MenuComponent';
2 |
3 | class MenuItem extends MenuComponent {
4 | getName() {
5 | return this.name;
6 | }
7 |
8 | getDescription() {
9 | return this.description;
10 | }
11 |
12 | getPrice() {
13 | return this.price;
14 | }
15 |
16 | print() {
17 | console.log(this.getName() + ": " + this.getDescription() + ", " + this.getPrice() + "euros");
18 | }
19 |
20 | createIterator() {
21 | var arr = [];
22 | return arr[Symbol.iterator]();
23 | }
24 | }
25 |
26 | export default MenuItem;
27 |
--------------------------------------------------------------------------------
/CompositeIterator/common/PancakeHouseMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from './Menu';
2 |
3 | class PancakeHouseMenu extends Menu {}
4 | ;
5 |
6 | export default PancakeHouseMenu;
7 |
--------------------------------------------------------------------------------
/Compound/1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Compound Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import MallardDuck from '../../common/MallardDuck';
11 | import DuckCall from '../../common/DuckCall';
12 | import RedheadDuck from '../../common/RedheadDuck';
13 | import RubberDuck from '../../common/RubberDuck';
14 | import Goose from '../../common/Goose';
15 | import GooseAdapter from '../../common/GooseAdapter';
16 |
17 | let oMallardDuck = new MallardDuck();
18 | let oDuckCall = new DuckCall();
19 | let oRedheadDuck = new RedheadDuck();
20 | let oRubberDuck = new RubberDuck();
21 | let oGoose = new Goose();
22 | let oGooseAdapter = new GooseAdapter(oGoose);
23 |
24 | console.log("Duck simulator:");
25 |
26 | oMallardDuck.quack();
27 | oDuckCall.quack();
28 | oRedheadDuck.quack();
29 | oRubberDuck.quack();
30 | oGooseAdapter.quack();
31 |
32 |
33 |
34 |
Console
35 |
36 |
COMPOUND
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Compound/1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import MallardDuck from '../../common/MallardDuck';
2 | import DuckCall from '../../common/DuckCall';
3 | import RedheadDuck from '../../common/RedheadDuck';
4 | import RubberDuck from '../../common/RubberDuck';
5 | import Goose from '../../common/Goose';
6 | import GooseAdapter from '../../common/GooseAdapter';
7 |
8 | let oMallardDuck = new MallardDuck();
9 | let oDuckCall = new DuckCall();
10 | let oRedheadDuck = new RedheadDuck();
11 | let oRubberDuck = new RubberDuck();
12 | let oGoose = new Goose();
13 | let oGooseAdapter = new GooseAdapter(oGoose);
14 |
15 | console.log("Duck simulator:");
16 |
17 | oMallardDuck.quack();
18 | oDuckCall.quack();
19 | oRedheadDuck.quack();
20 | oRubberDuck.quack();
21 | oGooseAdapter.quack();
22 |
--------------------------------------------------------------------------------
/Compound/2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Compound Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import CountingDuckFactory from '../../common/CountingDuckFactory';
11 | import Goose from '../../common/Goose';
12 | import GooseAdapter from '../../common/GooseAdapter';
13 | import QuackCounter from '../../common/QuackCounter';
14 |
15 | let oDuckFactory = new CountingDuckFactory();
16 | let oMallardDuck = oDuckFactory.createMallardDuck();
17 | let oDuckCall = oDuckFactory.createDuckCall();
18 | let oRedheadDuck = oDuckFactory.createRedheadDuck();
19 | let oRubberDuck = oDuckFactory.createRubberDuck();
20 | let oGoose = new Goose();
21 | let oGooseAdapter = new GooseAdapter(oGoose);
22 |
23 | console.log("Duck simulator:");
24 |
25 | oMallardDuck.quack();
26 | oDuckCall.quack();
27 | oRedheadDuck.quack();
28 | oRubberDuck.quack();
29 | oGooseAdapter.quack();
30 |
31 | console.log(QuackCounter.getQuacks());
32 |
33 |
34 |
35 |
Console
36 |
37 |
COMPOUND
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/Compound/2/scripts/CountingDuckFactory.js:
--------------------------------------------------------------------------------
1 | import AbstractDuckFactory from '../../common/AbstractDuckFactory';
2 | import MallardDuck from '../../common/MallardDuck';
3 | import RedheadDuck from '../../common/RedheadDuck';
4 | import DuckCall from '../../common/DuckCall';
5 | import RubberDuck from '../../common/RubberDuck';
6 | import QuackCounter from '../../common/QuackCounter';
7 |
8 | class CountingDuckFactory extends AbstractDuckFactory {
9 | createMallardDuck() {
10 | return new QuackCounter(new MallardDuck());
11 | }
12 |
13 | createRedheadDuck() {
14 | return new QuackCounter(new RedheadDuck());
15 | }
16 |
17 | createDuckCall() {
18 | return new QuackCounter(new DuckCall());
19 | }
20 |
21 | createRubberDuck() {
22 | return new QuackCounter(new RubberDuck());
23 | }
24 | }
25 |
26 | export default CountingDuckFactory;
27 |
--------------------------------------------------------------------------------
/Compound/2/scripts/main.js:
--------------------------------------------------------------------------------
1 | import CountingDuckFactory from '../../common/CountingDuckFactory';
2 | import Goose from '../../common/Goose';
3 | import GooseAdapter from '../../common/GooseAdapter';
4 | import QuackCounter from '../../common/QuackCounter';
5 |
6 | let oDuckFactory = new CountingDuckFactory();
7 | let oMallardDuck = oDuckFactory.createMallardDuck();
8 | let oDuckCall = oDuckFactory.createDuckCall();
9 | let oRedheadDuck = oDuckFactory.createRedheadDuck();
10 | let oRubberDuck = oDuckFactory.createRubberDuck();
11 | let oGoose = new Goose();
12 | let oGooseAdapter = new GooseAdapter(oGoose);
13 |
14 | console.log("Duck simulator:");
15 |
16 | oMallardDuck.quack();
17 | oDuckCall.quack();
18 | oRedheadDuck.quack();
19 | oRubberDuck.quack();
20 | oGooseAdapter.quack();
21 |
22 | console.log(QuackCounter.getQuacks());
23 |
--------------------------------------------------------------------------------
/Compound/3/scripts/main.js:
--------------------------------------------------------------------------------
1 | import CountingDuckFactory from '../../common/CountingDuckFactory';
2 | import Goose from '../../common/Goose';
3 | import GooseAdapter from '../../common/GooseAdapter';
4 | import Flock from '../../common/Flock';
5 | import QuackCounter from '../../common/QuackCounter';
6 |
7 | let oDuckFactory = new CountingDuckFactory();
8 | let oMallardDuck = oDuckFactory.createMallardDuck();
9 | let oDuckCall = oDuckFactory.createDuckCall();
10 | let oRedheadDuck = oDuckFactory.createRedheadDuck();
11 | let oRubberDuck = oDuckFactory.createRubberDuck();
12 | let oGoose = new Goose();
13 | let oGooseAdapter = new GooseAdapter(oGoose);
14 |
15 | let oFlockOfDucks = new Flock();
16 |
17 | oFlockOfDucks.add(oMallardDuck);
18 | oFlockOfDucks.add(oDuckCall);
19 | oFlockOfDucks.add(oRedheadDuck);
20 | oFlockOfDucks.add(oRubberDuck);
21 | oFlockOfDucks.add(oGooseAdapter);
22 |
23 | let oFlockOfMallards = new Flock();
24 |
25 | let oMallardDuck1 = oDuckFactory.createMallardDuck();
26 | let oMallardDuck2 = oDuckFactory.createMallardDuck();
27 | let oMallardDuck3 = oDuckFactory.createMallardDuck();
28 | let oMallardDuck4 = oDuckFactory.createMallardDuck();
29 |
30 | oFlockOfMallards.add(oMallardDuck1);
31 | oFlockOfMallards.add(oMallardDuck2);
32 | oFlockOfMallards.add(oMallardDuck3);
33 | oFlockOfMallards.add(oMallardDuck4);
34 |
35 | oFlockOfDucks.add(oFlockOfMallards);
36 |
37 | console.log("Duck simulator with Composite - Flocks:");
38 |
39 | oFlockOfDucks.quack();
40 |
41 | oFlockOfMallards.quack();
42 |
43 | console.log(QuackCounter.getQuacks());
44 |
--------------------------------------------------------------------------------
/Compound/4/scripts/Flock.js:
--------------------------------------------------------------------------------
1 | import Observable from './Observable';
2 | import Flock from '../../common/Flock';
3 |
4 | class FlockObservable extends Flock {
5 | constructor() {
6 | super();
7 | this.observable = new Observable(this);
8 | }
9 |
10 | registerObserver(observer) {
11 | this.observable.registerObserver(observer);
12 | }
13 |
14 | notifyObservers() {
15 | this.observable.notifyObservers();
16 | }
17 | }
18 |
19 | export default FlockObservable;
20 |
--------------------------------------------------------------------------------
/Compound/4/scripts/MallardDuck.js:
--------------------------------------------------------------------------------
1 | import Quackable from './Quackable';
2 | import Observable from './Observable';
3 |
4 | class MallardDuck extends Quackable {
5 | constructor() {
6 | super();
7 | this.observable = new Observable(this);
8 | }
9 |
10 | registerObserver(observer) {
11 | this.observable.registerObserver(observer);
12 | }
13 |
14 | notifyObservers() {
15 | this.observable.notifyObservers();
16 | }
17 |
18 | quack() {
19 | console.log('Quack!');
20 | this.notifyObservers();
21 | }
22 | }
23 |
24 | export default MallardDuck;
25 |
--------------------------------------------------------------------------------
/Compound/4/scripts/Observable.js:
--------------------------------------------------------------------------------
1 | import QuackObservable from './QuackObservable';
2 |
3 | class Observable extends QuackObservable {
4 | constructor(duck) {
5 | super();
6 | this.observers = [];
7 | this.duck = duck;
8 | }
9 |
10 | registerObserver(observer) {
11 | this.observers.push(observer);
12 | }
13 |
14 | notifyObservers() {
15 | var iterator = this.observers[Symbol.iterator]();
16 | while (iterator.hasNext()) {
17 | let observer = iterator.next();
18 | observer.update(this.duck);
19 | }
20 | }
21 | }
22 |
23 | export default Observable;
24 |
--------------------------------------------------------------------------------
/Compound/4/scripts/Observer.js:
--------------------------------------------------------------------------------
1 | class Observer {
2 | update() {
3 | throw new Error("This method must be overwritten!");
4 | }
5 | }
6 |
7 | export default Observer;
8 |
--------------------------------------------------------------------------------
/Compound/4/scripts/QuackObservable.js:
--------------------------------------------------------------------------------
1 | class QuackObservable {
2 | registerObserver() {
3 | throw new Error("This method must be overwritten!");
4 | }
5 |
6 | notifyObservers() {
7 | throw new Error("This method must be overwritten!");
8 | }
9 | }
10 |
11 | export default QuackObservable;
12 |
--------------------------------------------------------------------------------
/Compound/4/scripts/Quackable.js:
--------------------------------------------------------------------------------
1 | import QuackObservable from './QuackObservable';
2 |
3 | class Quackable extends QuackObservable {
4 | quack() {
5 | throw new Error("This method must be overwritten!");
6 | }
7 | }
8 |
9 | export default Quackable;
10 |
--------------------------------------------------------------------------------
/Compound/4/scripts/Quackologist.js:
--------------------------------------------------------------------------------
1 | import Observer from './Observer';
2 |
3 | class Quackologist extends Observer {
4 | update(duck) {
5 | console.log("Quackologist: " + oDuck + "just quacked");
6 | }
7 | }
8 |
9 | export default Quackologist;
10 |
--------------------------------------------------------------------------------
/Compound/4/scripts/RedheadDuck.js:
--------------------------------------------------------------------------------
1 | var RedheadDuck = function() {
2 | Quackable.apply(this);
3 | this.quack = function() {
4 | console.log("Quack!");
5 | };
6 | };
7 |
--------------------------------------------------------------------------------
/Compound/4/scripts/RubberDuck.js:
--------------------------------------------------------------------------------
1 | var RubberDuck = function() {
2 | Quackable.apply(this);
3 | this.quack = function() {
4 | console.log("Squeak!");
5 | };
6 | };
7 |
--------------------------------------------------------------------------------
/Compound/common/AbstractDuckFactory.js:
--------------------------------------------------------------------------------
1 | class AbstractDuckFactory {
2 | createMallardDuck() {
3 | throw new Error("This method must be overwritten!");
4 | }
5 |
6 | createRedheadDuck() {
7 | throw new Error("This method must be overwritten!");
8 | }
9 |
10 | createDuckCall() {
11 | throw new Error("This method must be overwritten!");
12 | }
13 |
14 | createRubberDuck() {
15 | throw new Error("This method must be overwritten!");
16 | }
17 | }
18 |
19 | export default AbstractDuckFactory;
20 |
--------------------------------------------------------------------------------
/Compound/common/CountingDuckFactory.js:
--------------------------------------------------------------------------------
1 | import AbstractDuckFactory from './AbstractDuckFactory';
2 | import QuackCounter from './QuackCounter';
3 | import MallardDuck from './MallardDuck';
4 | import RedheadDuck from './RedheadDuck';
5 | import DuckCall from './DuckCall';
6 | import RubberDuck from './RubberDuck';
7 |
8 | class CountingDuckFactory extends AbstractDuckFactory {
9 | createMallardDuck() {
10 | return new QuackCounter(new MallardDuck());
11 | }
12 |
13 | createRedheadDuck() {
14 | return new QuackCounter(new RedheadDuck());
15 | }
16 |
17 | createDuckCall() {
18 | return new QuackCounter(new DuckCall());
19 | }
20 |
21 | createRubberDuck() {
22 | return new QuackCounter(new RubberDuck());
23 | }
24 | }
25 |
26 | export default CountingDuckFactory;
27 |
--------------------------------------------------------------------------------
/Compound/common/DuckCall.js:
--------------------------------------------------------------------------------
1 | import Quackable from './Quackable';
2 |
3 | class DuckCall extends Quackable {
4 | quack() {
5 | console.log('Kwak!');
6 | }
7 | }
8 |
9 | export default DuckCall;
10 |
--------------------------------------------------------------------------------
/Compound/common/DuckFactory.js:
--------------------------------------------------------------------------------
1 | import AbstractDuckFactory from './AbstractDuckFactory';
2 | import MallardDuck from './MallardDuck';
3 | import RedheadDuck from './RedheadDuck';
4 | import DuckCall from './DuckCall';
5 | import RubberDuck from './RubberDuck';
6 |
7 | class DuckFactory extends AbstractDuckFactory {
8 | createMallardDuck() {
9 | return new MallardDuck();
10 | }
11 |
12 | createRedheadDuck() {
13 | return new RedheadDuck();
14 | }
15 |
16 | createDuckCall() {
17 | return new DuckCall();
18 | }
19 |
20 | createRubberDuck() {
21 | return new RubberDuck();
22 | }
23 | }
24 |
25 | export default DuckFactory;
26 |
--------------------------------------------------------------------------------
/Compound/common/Flock.js:
--------------------------------------------------------------------------------
1 | import Quackable from './Quackable';
2 |
3 | class Flock extends Quackable {
4 | constructor() {
5 | super();
6 | this.quackers = [];
7 | }
8 |
9 | quack() {
10 | let iterator = this.quackers[Symbol.iterator]();
11 | let quacker = iterator.next();
12 |
13 | while (quacker.value) {
14 | quacker.value.quack();
15 | quacker = iterator.next();
16 | }
17 | }
18 |
19 | add(quackable) {
20 | this.quackers.push(quackable);
21 | }
22 | }
23 |
24 | export default Flock;
25 |
--------------------------------------------------------------------------------
/Compound/common/Goose.js:
--------------------------------------------------------------------------------
1 | class Goose {
2 | honk() {
3 | console.log("Honk!");
4 | }
5 | }
6 |
7 | export default Goose;
8 |
--------------------------------------------------------------------------------
/Compound/common/GooseAdapter.js:
--------------------------------------------------------------------------------
1 | class GooseAdapter {
2 | constructor(oGoose) {
3 | this.oGoose = oGoose;
4 | }
5 |
6 | quack() {
7 | this.oGoose.honk();
8 | }
9 | }
10 |
11 | export default GooseAdapter;
12 |
--------------------------------------------------------------------------------
/Compound/common/MallardDuck.js:
--------------------------------------------------------------------------------
1 | import Quackable from './Quackable';
2 |
3 | class MallardDuck extends Quackable {
4 | quack() {
5 | console.log('Quack!');
6 | }
7 | }
8 |
9 | export default MallardDuck;
10 |
--------------------------------------------------------------------------------
/Compound/common/QuackCounter.js:
--------------------------------------------------------------------------------
1 | import Quackable from './Quackable';
2 |
3 | let counter = 0;
4 | class QuackCounter extends Quackable {
5 | constructor(duck) {
6 | super();
7 | counter = 0;
8 | this.duck = duck;
9 | }
10 |
11 | static get quacks() {
12 | return counter;
13 | }
14 |
15 | static getQuacks() {
16 | return counter;
17 | }
18 |
19 | quack() {
20 | this.duck.quack();
21 | counter++;
22 | }
23 | }
24 |
25 | export default QuackCounter;
26 |
--------------------------------------------------------------------------------
/Compound/common/Quackable.js:
--------------------------------------------------------------------------------
1 | class Quackable {
2 | quack() {
3 | throw new Error("This method must be overwritten!");
4 | }
5 | }
6 |
7 | export default Quackable;
8 |
--------------------------------------------------------------------------------
/Compound/common/RedheadDuck.js:
--------------------------------------------------------------------------------
1 | import Quackable from './Quackable';
2 |
3 | class RedheadDuck extends Quackable {
4 | quack() {
5 | console.log('Quack!');
6 | }
7 | }
8 |
9 | export default RedheadDuck;
10 |
--------------------------------------------------------------------------------
/Compound/common/RubberDuck.js:
--------------------------------------------------------------------------------
1 | import Quackable from './Quackable';
2 |
3 | class RubberDuck extends Quackable {
4 | quack() {
5 | console.log('Squeak!');
6 | }
7 | }
8 |
9 | export default RubberDuck;
10 |
--------------------------------------------------------------------------------
/Decorator/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Decorator Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Espresso from './Espresso';
11 | import Mocha from './Mocha';
12 | import Whip from './Whip';
13 |
14 | let oEspressoWithMochaAndWhip = new Espresso();
15 | oEspressoWithMochaAndWhip = new Mocha(oEspressoWithMochaAndWhip);
16 | oEspressoWithMochaAndWhip = new Whip(oEspressoWithMochaAndWhip);
17 |
18 | console.log(oEspressoWithMochaAndWhip.cost());
19 |
20 |
21 |
22 |
Console
23 |
24 |
DECORATOR
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Decorator/scripts/Beverage.js:
--------------------------------------------------------------------------------
1 | class Beverage {
2 | constructor(description = 'Unknown beverage') {
3 | this.description = description;
4 | }
5 |
6 | getDescription() {
7 | return this.description;
8 | }
9 |
10 | cost() {
11 | throw new Error("This method must be overwritten!");
12 | }
13 | }
14 |
15 | export default Beverage;
16 |
--------------------------------------------------------------------------------
/Decorator/scripts/CondimentDecorator.js:
--------------------------------------------------------------------------------
1 | import Beverage from './Beverage';
2 |
3 | class CondimentDecorator extends Beverage {}
4 |
5 | export default CondimentDecorator;
6 |
--------------------------------------------------------------------------------
/Decorator/scripts/Espresso.js:
--------------------------------------------------------------------------------
1 | import Beverage from './Beverage';
2 |
3 | class Espresso extends Beverage {
4 | cost() {
5 | return 1.99;
6 | }
7 | }
8 |
9 | export default Espresso;
10 |
--------------------------------------------------------------------------------
/Decorator/scripts/HouseBlend.js:
--------------------------------------------------------------------------------
1 | import Beverage from './Beverage';
2 |
3 | class HouseBlend extends Beverage {
4 | cost() {
5 | return 0.89;
6 | }
7 | }
8 |
9 | export default HouseBlend;
10 |
--------------------------------------------------------------------------------
/Decorator/scripts/Mocha.js:
--------------------------------------------------------------------------------
1 | import CondimentDecorator from './CondimentDecorator';
2 |
3 | class Mocha extends CondimentDecorator {
4 | constructor(beverage) {
5 | super();
6 | this.beverage = beverage;
7 | }
8 |
9 | getDescription() {
10 | return this.beverage.getDescription() + ", Mocha";
11 | }
12 |
13 | cost() {
14 | return 0.20 + this.beverage.cost();
15 | }
16 | }
17 |
18 | export default Mocha;
19 |
--------------------------------------------------------------------------------
/Decorator/scripts/Whip.js:
--------------------------------------------------------------------------------
1 | import CondimentDecorator from './CondimentDecorator';
2 |
3 | class Whip extends CondimentDecorator {
4 | constructor(beverage) {
5 | super();
6 | this.beverage = beverage;
7 | }
8 |
9 | getDescription() {
10 | return this.beverage.getDescription() + ', Whip';
11 | }
12 |
13 | cost() {
14 | return 0.60 + this.beverage.cost();
15 | }
16 | }
17 |
18 | export default Whip;
19 |
--------------------------------------------------------------------------------
/Decorator/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Espresso from './Espresso';
2 | import Mocha from './Mocha';
3 | import Whip from './Whip';
4 |
5 | let oEspressoWithMochaAndWhip = new Espresso();
6 | oEspressoWithMochaAndWhip = new Mocha(oEspressoWithMochaAndWhip);
7 | oEspressoWithMochaAndWhip = new Whip(oEspressoWithMochaAndWhip);
8 |
9 | console.log(oEspressoWithMochaAndWhip.cost());
10 |
--------------------------------------------------------------------------------
/Facade/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Facade Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import HomeTheaterFacade from './HomeTheaterFacade';
11 | import Amplifier from './elements/Amplifier';
12 | import CdPlayer from './elements/CdPlayer';
13 | import Projector from './elements/Projector';
14 | import TheaterLights from './elements/TheaterLights';
15 | import Screen from './elements/Screen';
16 | import PopcornPopper from './elements/PopcornPopper';
17 | import Nikita from './Nikita';
18 |
19 | var oHomeTheaterFacade = new HomeTheaterFacade({
20 | amplifier: new Amplifier(),
21 | dvdPlayer: new DvdPlayer(),
22 | cdPlayer: new CdPlayer(),
23 | projector: new Projector(),
24 | theaterLights: new TheaterLights(),
25 | screen: new Screen(),
26 | popcornPopper: new PopcornPopper()
27 | });
28 | oHomeTheaterFacade.watchMovie(new Nikita());
29 | oHomeTheaterFacade.endMovie();
30 |
31 |
32 |
33 |
Console
34 |
35 |
FACADE
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/Facade/scripts/Movie.js:
--------------------------------------------------------------------------------
1 | class Movie {
2 | constructor(name = '', minutes = 0, director = '', actors = [], description = '') {
3 | this.name = name;
4 | this.minutes = minutes;
5 | this.director = director;
6 | this.actors = actors;
7 | this.description = description;
8 | }
9 |
10 | setName(name) {
11 | this.name = name;
12 | }
13 |
14 | setMinutes(minutes) {
15 | this.minutes = minutes;
16 | }
17 |
18 | setDirector(director) {
19 | this.director = director;
20 | }
21 |
22 | setActors(actors) {
23 | this.actors = actors;
24 | }
25 |
26 | setDescription(description) {
27 | this.description = description;
28 | }
29 | }
30 |
31 | export default Movie;
32 |
--------------------------------------------------------------------------------
/Facade/scripts/Nikita.js:
--------------------------------------------------------------------------------
1 | import Movie from './Movie';
2 |
3 | class Nikita extends Movie {
4 | constructor() {
5 | super('Nikita, hard to kill!',
6 | 120,
7 | 'Steven Spielberg',
8 | ['Brad Pitt'],
9 | 'Bloody!'
10 | );
11 | }
12 | }
13 |
14 | export default Nikita;
15 |
--------------------------------------------------------------------------------
/Facade/scripts/elements/Amplifier.js:
--------------------------------------------------------------------------------
1 | import Switchable from './Switchable';
2 |
3 | class Amplifier extends Switchable(null) {
4 | constructor() {
5 | super();
6 | this.volume = 0;
7 | this.dvdPlayer = null;
8 | this.cdPlayer = null;
9 | this.tuner = null;
10 | this.surroundSound = false;
11 | this.stereoSound = false;
12 | }
13 |
14 | on() {
15 | console.log("Amplifier is on!");
16 | }
17 |
18 | off() {
19 | console.log("Amplifier is off!");
20 | }
21 |
22 | setVolume(volume) {
23 | this.volume = volume;
24 | console.log("Volume change to " + volume);
25 | }
26 |
27 | setDvdPlayer(dvdPlayer) {
28 | this.dvdPlayer = dvdPlayer;
29 | console.log("Plugged DVD Player to Amplifier!");
30 | }
31 |
32 | setCdPlayer(cdPlayer) {
33 | this.cdPlayer = cdPlayer;
34 | console.log("Plugged Cd Player to Amplifier!");
35 | }
36 |
37 | setTuner(tuner) {
38 | this.tuner = tuner;
39 | console.log("Plugged on Tuner to Amplifier!");
40 | }
41 |
42 | setSurroundSound() {
43 | this.surroundSound = true;
44 | console.log("Surround Mode is active!");
45 | }
46 |
47 | setStereoSound() {
48 | this.stereoSound = true;
49 | console.log("Stereo Mode is active!");
50 | }
51 | }
52 |
53 | export default Amplifier;
54 |
--------------------------------------------------------------------------------
/Facade/scripts/elements/CdPlayer.js:
--------------------------------------------------------------------------------
1 | import Switchable from './Switchable';
2 | import Playable from './Playable';
3 |
4 | class CdPlayer extends Switchable(Playable(null)) {
5 | on() {
6 | console.log("CdPlayer is on!");
7 | }
8 |
9 | off() {
10 | console.log("CdPlayer is off!");
11 | }
12 |
13 | eject() {
14 | console.log("Eject Cd!");
15 | }
16 |
17 | play(cd) {
18 | console.log("Playing " + cd.sName);
19 | }
20 |
21 | stop() {
22 | console.log("Stop CdPlayer!");
23 | }
24 | }
25 |
26 | export default CdPlayer;
27 |
--------------------------------------------------------------------------------
/Facade/scripts/elements/DvdPlayer.js:
--------------------------------------------------------------------------------
1 | import Switchable from './Switchable';
2 | import Playable from './Playable';
3 |
4 | class DvdPlayer extends Switchable(Playable(null)) {
5 | on() {
6 | console.log("DvdPlayer is on!");
7 | }
8 |
9 | off() {
10 | console.log("DvdPlayer is off!");
11 | }
12 |
13 | eject() {
14 | console.log("Eject Dvd!");
15 | }
16 |
17 | play(movie) {
18 | console.log("Playing " + movie.name);
19 | }
20 |
21 | stop() {
22 | console.log("Stop DvdPlayer!");
23 | }
24 | }
25 |
26 | export default DvdPlayer;
27 |
--------------------------------------------------------------------------------
/Facade/scripts/elements/Playable.js:
--------------------------------------------------------------------------------
1 | const Playable = Sup => class extends Sup {
2 | eject() {
3 | throw new Error('This method should be overwritten!');
4 | }
5 |
6 | play() {
7 | throw new Error('This method should be overwritten!');
8 | }
9 |
10 | stop() {
11 | throw new Error('This method should be overwritten!');
12 | }
13 | };
14 |
15 | export default Playable;
16 |
--------------------------------------------------------------------------------
/Facade/scripts/elements/PopcornPopper.js:
--------------------------------------------------------------------------------
1 | import Switchable from './Switchable';
2 |
3 | class PopcornPopper extends Switchable(null) {
4 | on() {
5 | console.log("PopcornPopper is on!");
6 | }
7 |
8 | off() {
9 | console.log("PopcornPopper is off!");
10 | }
11 |
12 | pop() {
13 | console.log("Yum!Yum!");
14 | }
15 | }
16 |
17 | export default PopcornPopper;
18 |
--------------------------------------------------------------------------------
/Facade/scripts/elements/Projector.js:
--------------------------------------------------------------------------------
1 | import Switchable from './Switchable';
2 |
3 | class Projector extends Switchable(null) {
4 | constructor() {
5 | super();
6 | this.wideScreenMode = false;
7 | }
8 |
9 | on() {
10 | console.log("Projector is on!");
11 | }
12 |
13 | off() {
14 | console.log("Projector is off!");
15 | }
16 |
17 | setWideScreenMode() {
18 | this.wideScreenMode = true;
19 | console.log("Projector now is on wide screen mode!");
20 | }
21 | }
22 |
23 | export default Projector;
24 |
--------------------------------------------------------------------------------
/Facade/scripts/elements/Screen.js:
--------------------------------------------------------------------------------
1 | class Screen {
2 | down() {
3 | console.log("The screen is down!");
4 | }
5 |
6 | up() {
7 | console.log("The screen is up!");
8 | }
9 | }
10 |
11 | export default Screen;
12 |
--------------------------------------------------------------------------------
/Facade/scripts/elements/Switchable.js:
--------------------------------------------------------------------------------
1 | const Switchable = Sup => class extends Sup {
2 | on() {
3 | throw new Error('This method should be overwritten!');
4 | }
5 |
6 | off() {
7 | throw new Error('This method should be overwritten!');
8 | }
9 | };
10 |
11 | export default Switchable;
12 |
--------------------------------------------------------------------------------
/Facade/scripts/elements/TheaterLights.js:
--------------------------------------------------------------------------------
1 | import Switchable from './Switchable';
2 |
3 | class TheaterLights extends Switchable(null) {
4 | on() {
5 | console.log("The lights are on!");
6 | }
7 |
8 | off() {
9 | console.log("The lights are off!");
10 | }
11 | }
12 |
13 |
14 | export default TheaterLights;
15 |
--------------------------------------------------------------------------------
/Facade/scripts/elements/Tuner.js:
--------------------------------------------------------------------------------
1 | import Switchable from './Switchable';
2 |
3 | class Tuner extends Switchable(null) {
4 | constructor() {
5 | this.amplifier = null;
6 | this.frequency = 0;
7 | }
8 |
9 | on() {
10 | console.log("Tuner is on!");
11 | }
12 |
13 | off() {
14 | console.log("Tuner is off!");
15 | }
16 |
17 | setAm() {
18 | console.log("Tuner AM!");
19 | }
20 |
21 | setFm() {
22 | console.log("Tuner FM!");
23 | }
24 |
25 | setFrequency(frequency) {
26 | this.frequency = frequency;
27 | console.log("Tuner frequency changed to " + frequency);
28 | }
29 | }
30 |
31 | export default Tuner;
32 |
--------------------------------------------------------------------------------
/Facade/scripts/main.js:
--------------------------------------------------------------------------------
1 | import HomeTheaterFacade from './HomeTheaterFacade';
2 | import Amplifier from './elements/Amplifier';
3 | import DvdPlayer from './elements/DvdPlayer';
4 | import CdPlayer from './elements/CdPlayer';
5 | import Projector from './elements/Projector';
6 | import TheaterLights from './elements/TheaterLights';
7 | import Screen from './elements/Screen';
8 | import PopcornPopper from './elements/PopcornPopper';
9 | import Nikita from './Nikita';
10 |
11 | var oHomeTheaterFacade = new HomeTheaterFacade({
12 | amplifier: new Amplifier(),
13 | dvdPlayer: new DvdPlayer(),
14 | cdPlayer: new CdPlayer(),
15 | projector: new Projector(),
16 | theaterLights: new TheaterLights(),
17 | screen: new Screen(),
18 | popcornPopper: new PopcornPopper()
19 | });
20 | oHomeTheaterFacade.watchMovie(new Nikita());
21 | oHomeTheaterFacade.endMovie();
22 |
--------------------------------------------------------------------------------
/Factory/1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Factory Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import NewYorkPizzaStore from './stores/NewYorkPizzaStore';
11 |
12 | var oPizzaStore = new NewYorkPizzaStore();
13 | oPizzaStore.orderPizza("cheese");
14 |
15 |
16 |
17 |
Console
18 |
19 |
FACTORY
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Factory/1/scripts/Pizza.js:
--------------------------------------------------------------------------------
1 | class Pizza {
2 | constructor({ name = '', dough = '', sauce = '', toppings = []}) {
3 | this.name = name;
4 | this.dough = dough;
5 | this.sauce = sauce;
6 | this.toppings = toppings;
7 | }
8 |
9 | prepare() {
10 | console.log("Preparing " + this.name);
11 | console.log("Tossing dough...");
12 | console.log("Adding sauce");
13 | console.log("Adding toppings:");
14 |
15 | for (let topping of this.toppings) {
16 | console.log(topping + " ");
17 | }
18 | }
19 |
20 | bake() {
21 | console.log("Bake for 25 minutes at 350");
22 | }
23 |
24 | cut() {
25 | console.log("Cutting the pizza into diagonal slices");
26 | }
27 |
28 | box() {
29 | console.log("Place pizza in official PizzaStore box");
30 | }
31 |
32 | getName() {
33 | return this.name;
34 | }
35 | }
36 |
37 | export default Pizza;
38 |
--------------------------------------------------------------------------------
/Factory/1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import NewYorkPizzaStore from './stores/NewYorkPizzaStore';
2 |
3 | var oPizzaStore = new NewYorkPizzaStore();
4 | oPizzaStore.orderPizza("cheese");
5 |
--------------------------------------------------------------------------------
/Factory/1/scripts/pizzas/ChicagoStyle/BasicPizza.js:
--------------------------------------------------------------------------------
1 | import Pizza from '../../Pizza';
2 |
3 | class BasicPizza extends Pizza {
4 | cut() {
5 | console.log("Cutting the pizza into square slices.");
6 | }
7 | }
8 |
9 | export default BasicPizza;
10 |
--------------------------------------------------------------------------------
/Factory/1/scripts/pizzas/ChicagoStyle/CheesePizza.js:
--------------------------------------------------------------------------------
1 | import BasicPizza from './BasicPizza';
2 |
3 | class CheesePizza extends BasicPizza {
4 | constructor() {
5 | super({
6 | name: 'Chicago Style Deep Dish Cheese Pizza',
7 | dough: 'Extra Thin Crust Dough',
8 | sauce: 'Plum Tomato Sauce',
9 | toppings: ["Shredded Mozzarella Cheese"]
10 | })
11 | }
12 | }
13 |
14 | export default CheesePizza;
15 |
--------------------------------------------------------------------------------
/Factory/1/scripts/pizzas/ChicagoStyle/ClamPizza.js:
--------------------------------------------------------------------------------
1 | import BasicPizza from './BasicPizza';
2 |
3 | class ClamPizza extends BasicPizza {
4 | constructor() {
5 | super({
6 | name: 'Chicago Style Deep Dish Clam Pizza',
7 | dough: 'Extra Thin Crust Dough',
8 | sauce: 'Plum Tomato Sauce',
9 | toppings: ["Shredded Mozzarella Cheese", 'Clams']
10 | })
11 | }
12 | }
13 |
14 | export default ClamPizza;
15 |
--------------------------------------------------------------------------------
/Factory/1/scripts/pizzas/ChicagoStyle/PepperoniPizza.js:
--------------------------------------------------------------------------------
1 | import BasicPizza from './BasicPizza';
2 |
3 | class PepperoniPizza extends BasicPizza {
4 | constructor() {
5 | super({
6 | name: 'Chicago Style Deep Dish Pepperoni Pizza',
7 | dough: 'Extra Thin Crust Dough',
8 | sauce: 'Plum Tomato Sauce',
9 | toppings: ["Shredded Mozzarella Cheese", 'Pepperoni']
10 | })
11 | }
12 | }
13 |
14 | export default PepperoniPizza;
15 |
--------------------------------------------------------------------------------
/Factory/1/scripts/pizzas/ChicagoStyle/VeggiePizza.js:
--------------------------------------------------------------------------------
1 | import BasicPizza from './BasicPizza';
2 |
3 | class VeggiePizza extends BasicPizza {
4 | constructor() {
5 | super({
6 | name: 'Chicago Style Deep Dish Veggie Pizza',
7 | dough: 'Extra Thin Crust Dough',
8 | sauce: 'Plum Tomato Sauce',
9 | toppings: ["Shredded Mozzarella Cheese", 'Paprika', 'Tomato', 'Beans']
10 | })
11 | }
12 | }
13 |
14 | export default VeggiePizza;
15 |
--------------------------------------------------------------------------------
/Factory/1/scripts/pizzas/NewYorkStyle/CheesePizza.js:
--------------------------------------------------------------------------------
1 | import Pizza from '../../Pizza';
2 |
3 | class CheesePizza extends Pizza {
4 | constructor() {
5 | super({
6 | name: 'NY Style Sauce and Cheese Pizza',
7 | dough: 'Thin Crust Dough',
8 | sauce: 'Marinara sauce',
9 | toppings: ["Grated Reggiano Cheese"]
10 | });
11 | }
12 | }
13 |
14 | export default CheesePizza;
15 |
--------------------------------------------------------------------------------
/Factory/1/scripts/pizzas/NewYorkStyle/ClamPizza.js:
--------------------------------------------------------------------------------
1 | import Pizza from '../../Pizza';
2 |
3 | class ClamPizza extends Pizza {
4 | constructor() {
5 | super({
6 | name: 'NY Style Sauce and Clam Pizza',
7 | dough: 'Thin Crust Dough',
8 | sauce: 'Marinara sauce',
9 | toppings: ["Grated Reggiano Cheese", 'Clams']
10 | });
11 | }
12 | }
13 |
14 | export default ClamPizza;
15 |
--------------------------------------------------------------------------------
/Factory/1/scripts/pizzas/NewYorkStyle/PepperoniPizza.js:
--------------------------------------------------------------------------------
1 | import Pizza from '../../Pizza';
2 |
3 | class PepperoniPizza extends Pizza {
4 | constructor() {
5 | super({
6 | name: 'NY Style Sauce and Pepperoni Pizza',
7 | dough: 'Thin Crust Dough',
8 | sauce: 'Marinara sauce',
9 | toppings: ["Grated Reggiano Cheese", 'Pepperoni']
10 | });
11 | }
12 | }
13 |
14 | export default PepperoniPizza;
15 |
--------------------------------------------------------------------------------
/Factory/1/scripts/pizzas/NewYorkStyle/VeggiePizza.js:
--------------------------------------------------------------------------------
1 | import Pizza from '../../Pizza';
2 |
3 | class VeggiePizza extends Pizza {
4 | constructor() {
5 | super({
6 | name: 'NY Style Sauce and Veggie Pizza',
7 | dough: 'Thin Crust Dough',
8 | sauce: 'Marinara sauce',
9 | toppings: ["Grated Reggiano Cheese", 'Paprika', 'Tomato', 'Beans']
10 | });
11 | }
12 | }
13 |
14 | export default VeggiePizza;
15 |
--------------------------------------------------------------------------------
/Factory/1/scripts/stores/ChicagoPizzaStore.js:
--------------------------------------------------------------------------------
1 | import PizzaStore from '../../../common/PizzaStore';
2 | import CheesePizza from '../pizzas/ChicagoStyle/CheesePizza';
3 | import VeggiePizza from '../pizzas/ChicagoStyle/VeggiePizza';
4 | import ClamPizza from '../pizzas/ChicagoStyle/ClamPizza';
5 | import PepperoniPizza from '../pizzas/ChicagoStyle/PepperoniPizza';
6 |
7 | const PIZZAS = {
8 | cheese: CheesePizza,
9 | veggie: VeggiePizza,
10 | clam: ClamPizza,
11 | pepperoni: PepperoniPizza
12 | };
13 |
14 | class ChicagoPizzaStore extends PizzaStore {
15 | createPizza(type) {
16 | let PizzaConstructor = PIZZAS[type];
17 | let pizza = null;
18 | if (PizzaConstructor) {
19 | pizza = new PizzaConstructor();
20 | }
21 | return pizza;
22 | }
23 | }
24 |
25 | export default ChicagoPizzaStore;
26 |
--------------------------------------------------------------------------------
/Factory/1/scripts/stores/NewYorkPizzaStore.js:
--------------------------------------------------------------------------------
1 | import PizzaStore from '../../../common/PizzaStore';
2 | import CheesePizza from '../pizzas/NewYorkStyle/CheesePizza';
3 | import VeggiePizza from '../pizzas/NewYorkStyle/VeggiePizza';
4 | import ClamPizza from '../pizzas/NewYorkStyle/ClamPizza';
5 | import PepperoniPizza from '../pizzas/NewYorkStyle/PepperoniPizza';
6 |
7 | const PIZZAS = {
8 | cheese: CheesePizza,
9 | veggie: VeggiePizza,
10 | clam: ClamPizza,
11 | pepperoni: PepperoniPizza
12 | };
13 |
14 | class NewYorkPizzaStore extends PizzaStore {
15 | createPizza(type) {
16 | let PizzaConstructor = PIZZAS[type];
17 | let pizza = null;
18 | if (PizzaConstructor) {
19 | pizza = new PizzaConstructor();
20 | }
21 | return pizza;
22 | }
23 | }
24 |
25 | export default NewYorkPizzaStore;
26 |
--------------------------------------------------------------------------------
/Factory/2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Factory Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import NewYorkPizzaStore from './stores/NewYorkPizzaStore';
11 |
12 | var oPizzaStore = new NewYorkPizzaStore();
13 | oPizzaStore.orderPizza("cheese");
14 |
15 |
16 |
17 |
Console
18 |
19 |
FACTORY
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Factory/2/scripts/Pizza.js:
--------------------------------------------------------------------------------
1 | class Pizza {
2 | constructor({ name = '', dough = null, sauce = null, veggies = [], cheese = null, pepperoni = null, clams = null }) {
3 | this.name = name;
4 | this.dough = dough;
5 | this.sauce = sauce;
6 | this.veggies = veggies;
7 | this.cheese = cheese;
8 | this.pepperoni = pepperoni;
9 | this.clams = clams;
10 | }
11 |
12 | prepare() {
13 | throw new Error("This method must be overwritten!");
14 | }
15 |
16 | bake() {
17 | console.log("Bake for 25 minutes at 350");
18 | }
19 |
20 | cut() {
21 | console.log("Cutting the pizza into diagonal slices");
22 | }
23 |
24 | box() {
25 | console.log("Place pizza in official PizzaStore box");
26 | }
27 |
28 | getName() {
29 | return this.name;
30 | }
31 |
32 | setName(name) {
33 | this.name = name;
34 | }
35 | }
36 |
37 | export default Pizza;
38 |
--------------------------------------------------------------------------------
/Factory/2/scripts/PizzaIngredientFactory.js:
--------------------------------------------------------------------------------
1 | class PizzaIngredientFactory {
2 | createDough() {
3 | throw new Error("This method must be overwritten!");
4 | }
5 |
6 | createSauce() {
7 | throw new Error("This method must be overwritten!");
8 | }
9 |
10 | createCheese() {
11 | throw new Error("This method must be overwritten!");
12 | }
13 |
14 | createVeggies() {
15 | throw new Error("This method must be overwritten!");
16 | }
17 |
18 | createPepperoni() {
19 | throw new Error("This method must be overwritten!");
20 | }
21 |
22 | createClam() {
23 | throw new Error("This method must be overwritten!");
24 | }
25 | }
26 |
27 | export default PizzaIngredientFactory;
28 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredientFactory/ChicagoPizzaIngredientFactory.js:
--------------------------------------------------------------------------------
1 | import PizzaIngredientFactory from '../PizzaIngredientFactory';
2 | import ThinCrustDough from '../ingredients/ThinCrustDough';
3 | import MarinaraSauce from '../ingredients/MarinaraSauce';
4 | import ReggianoCheese from '../ingredients/ReggianoCheese';
5 | import Garlic from '../ingredients/Garlic';
6 | import Onion from '../ingredients/Onion';
7 | import Mushroom from '../ingredients/Mushroom';
8 | import RedPepper from '../ingredients/RedPepper';
9 |
10 | class ChicagoPizzaIngredientFactory extends PizzaIngredientFactory {
11 | createDough() {
12 | return new ThinCrustDough();
13 | }
14 |
15 | createSauce() {
16 | return new MarinaraSauce();
17 | }
18 |
19 | createCheese() {
20 | return new ReggianoCheese();
21 | }
22 |
23 | createVeggies() {
24 | return [new Garlic(), new Onion(), new Mushroom(), new RedPepper()];
25 | }
26 |
27 | createPepperoni() {}
28 |
29 | createClam() {}
30 | }
31 |
32 | export default ChicagoPizzaIngredientFactory;
33 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredientFactory/NewYorkPizzaIngredientFactory.js:
--------------------------------------------------------------------------------
1 | import PizzaIngredientFactory from '../PizzaIngredientFactory';
2 | import ThinCrustDough from '../ingredients/ThinCrustDough';
3 | import MarinaraSauce from '../ingredients/MarinaraSauce';
4 | import ReggianoCheese from '../ingredients/ReggianoCheese';
5 | import Garlic from '../ingredients/Garlic';
6 | import Mushroom from '../ingredients/Mushroom';
7 | import RedPepper from '../ingredients/RedPepper';
8 |
9 | class NewYorkPizzaIngredientFactory extends PizzaIngredientFactory {
10 | createDough() {
11 | return new ThinCrustDough();
12 | }
13 |
14 | createSauce() {
15 | return new MarinaraSauce();
16 | }
17 |
18 | createCheese() {
19 | return new ReggianoCheese();
20 | }
21 |
22 | createVeggies() {
23 | return [new Garlic(), new Mushroom(), new RedPepper()];
24 | }
25 |
26 | createPepperoni() {}
27 |
28 | createClam() {}
29 | }
30 |
31 | export default NewYorkPizzaIngredientFactory;
32 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredients/FreshClams.js:
--------------------------------------------------------------------------------
1 | class FreshClams {}
2 |
3 | export default FreshClams;
4 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredients/Garlic.js:
--------------------------------------------------------------------------------
1 | class Garlic {}
2 |
3 | export default Garlic;
4 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredients/MarinaraSauce.js:
--------------------------------------------------------------------------------
1 | class MarinaraSauce {}
2 |
3 | export default MarinaraSauce;
4 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredients/Mushroom.js:
--------------------------------------------------------------------------------
1 | class Mushroom {}
2 |
3 | export default Mushroom;
4 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredients/Onion.js:
--------------------------------------------------------------------------------
1 | class Onion {}
2 |
3 | export default Onion;
4 |
5 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredients/RedPepper.js:
--------------------------------------------------------------------------------
1 | class RedPepper {}
2 |
3 | export default RedPepper;
4 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredients/ReggianoCheese.js:
--------------------------------------------------------------------------------
1 | class ReggianoCheese {}
2 |
3 | export default ReggianoCheese;
4 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredients/SlicedPepperoni.js:
--------------------------------------------------------------------------------
1 | class SlicedPepperoni {}
2 |
3 | export default SlicedPepperoni;
4 |
--------------------------------------------------------------------------------
/Factory/2/scripts/ingredients/ThinCrustDough.js:
--------------------------------------------------------------------------------
1 | class ThinCrustDough {}
2 |
3 | export default ThinCrustDough;
4 |
--------------------------------------------------------------------------------
/Factory/2/scripts/main.js:
--------------------------------------------------------------------------------
1 | import NewYorkPizzaStore from './stores/NewYorkPizzaStore';
2 |
3 | var oPizzaStore = new NewYorkPizzaStore();
4 | oPizzaStore.orderPizza("cheese");
5 |
--------------------------------------------------------------------------------
/Factory/2/scripts/pizzas/CheesePizza.js:
--------------------------------------------------------------------------------
1 | import Pizza from '../Pizza';
2 |
3 | class CheesePizza extends Pizza {
4 | constructor(style, ingredientFactory) {
5 | super({
6 | name: style + ' Cheese Pizza'
7 | });
8 | console.log(this.name);
9 | this.ingredientFactory = ingredientFactory;
10 | }
11 |
12 | prepare() {
13 | let ingredientFactory = this.ingredientFactory;
14 | console.log("Preparing " + this.name);
15 | this.dough = ingredientFactory.createDough();
16 | this.sauce = ingredientFactory.createSauce();
17 | this.cheese = ingredientFactory.createCheese();
18 | }
19 | }
20 |
21 | export default CheesePizza;
22 |
--------------------------------------------------------------------------------
/Factory/2/scripts/pizzas/ClamPizza.js:
--------------------------------------------------------------------------------
1 | import Pizza from '../Pizza';
2 |
3 | class ClamPizza extends Pizza {
4 | constructor(style, ingredientFactory) {
5 | super({
6 | name: style + ' Clams Pizza'
7 | });
8 | this.ingredientFactory = ingredientFactory;
9 | }
10 |
11 | prepare() {
12 | let ingredientFactory = this.ingredientFactory;
13 | console.log("Preparing " + this.name);
14 | this.dough = ingredientFactory.createDough();
15 | this.sauce = ingredientFactory.createSauce();
16 | this.cheese = ingredientFactory.createCheese();
17 | this.clams = ingredientFactory.createClam();
18 | }
19 | }
20 |
21 | export default ClamPizza;
22 |
--------------------------------------------------------------------------------
/Factory/2/scripts/pizzas/PepperoniPizza.js:
--------------------------------------------------------------------------------
1 | import Pizza from '../Pizza';
2 |
3 | class PepperoniPizza extends Pizza {
4 | constructor(style, ingredientFactory) {
5 | super({
6 | name: style + ' Pepperoni Pizza'
7 | });
8 | this.ingredientFactory = ingredientFactory;
9 | }
10 |
11 | prepare() {
12 | let ingredientFactory = this.ingredientFactory;
13 | console.log("Preparing " + this.name);
14 | this.dough = ingredientFactory.createDough();
15 | this.sauce = ingredientFactory.createSauce();
16 | this.cheese = ingredientFactory.createCheese();
17 | }
18 | }
19 |
20 | export default PepperoniPizza;
21 |
--------------------------------------------------------------------------------
/Factory/2/scripts/pizzas/VeggiePizza.js:
--------------------------------------------------------------------------------
1 | import Pizza from '../Pizza';
2 |
3 | class VeggiePizza extends Pizza {
4 | constructor(style, ingredientFactory) {
5 | super({
6 | name: style + ' Veggie Pizza'
7 | });
8 | this.ingredientFactory = ingredientFactory;
9 | }
10 |
11 | prepare() {
12 | let ingredientFactory = this.ingredientFactory;
13 | console.log("Preparing " + this.name);
14 | this.dough = ingredientFactory.createDough();
15 | this.sauce = ingredientFactory.createSauce();
16 | this.cheese = ingredientFactory.createCheese();
17 | }
18 | }
19 |
20 | export default VeggiePizza;
21 |
--------------------------------------------------------------------------------
/Factory/2/scripts/stores/ChicagoPizzaStore.js:
--------------------------------------------------------------------------------
1 | import PizzaStore from '../../../common/PizzaStore';
2 | import ChicagoPizzaIngredientFactory from '../ingredientFactory/ChicagoPizzaIngredientFactory';
3 | import CheesePizza from '../pizzas/CheesePizza';
4 | import VeggiePizza from '../pizzas/VeggiePizza';
5 | import ClamPizza from '../pizzas/ClamPizza';
6 | import PepperoniPizza from '../pizzas/PepperoniPizza';
7 |
8 | const PIZZAS = {
9 | cheese: CheesePizza,
10 | veggie: VeggiePizza,
11 | clam: ClamPizza,
12 | pepperoni: PepperoniPizza
13 | };
14 |
15 | class ChicagoPizzaStore extends PizzaStore {
16 | createPizza(type) {
17 | let ingredientFactory = new ChicagoPizzaIngredientFactory();
18 | let PizzaConstructor = PIZZAS[type];
19 | let pizza = null;
20 | if (PizzaConstructor) {
21 | pizza = new PizzaConstructor('Chicago Style', ingredientFactory);
22 | }
23 | return pizza;
24 | }
25 | }
26 |
27 | export default ChicagoPizzaStore;
28 |
--------------------------------------------------------------------------------
/Factory/2/scripts/stores/NewYorkPizzaStore.js:
--------------------------------------------------------------------------------
1 | import PizzaStore from '../../../common/PizzaStore';
2 | import NewYorkPizzaIngredientFactory from '../ingredientFactory/NewYorkPizzaIngredientFactory';
3 | import CheesePizza from '../pizzas/CheesePizza';
4 | import VeggiePizza from '../pizzas/VeggiePizza';
5 | import ClamPizza from '../pizzas/ClamPizza';
6 | import PepperoniPizza from '../pizzas/PepperoniPizza';
7 |
8 | const PIZZAS = {
9 | cheese: CheesePizza,
10 | veggie: VeggiePizza,
11 | clam: ClamPizza,
12 | pepperoni: PepperoniPizza
13 | };
14 |
15 | class NewYorkPizzaStore extends PizzaStore {
16 | createPizza(type) {
17 | let ingredientFactory = new NewYorkPizzaIngredientFactory();
18 | let PizzaConstructor = PIZZAS[type];
19 | let pizza = null;
20 | if (PizzaConstructor) {
21 | pizza = new PizzaConstructor('New York Style', ingredientFactory);
22 | }
23 | return pizza;
24 | }
25 | }
26 |
27 | export default NewYorkPizzaStore;
28 |
--------------------------------------------------------------------------------
/Factory/common/PizzaStore.js:
--------------------------------------------------------------------------------
1 | class PizzaStore {
2 | createPizza() {
3 | throw new Error("This method must be overwritten!");
4 | }
5 |
6 | orderPizza(type) {
7 | let pizza = this.createPizza(type);
8 |
9 | pizza.prepare();
10 | pizza.bake();
11 | pizza.cut();
12 | pizza.box();
13 | }
14 | }
15 |
16 | export default PizzaStore;
17 |
--------------------------------------------------------------------------------
/Flyweight/scripts/Forest.js:
--------------------------------------------------------------------------------
1 | import TreeFactory from './TreeFactory';
2 | import Tree from './Tree';
3 |
4 | const privateTrees = new WeakMap();
5 | class Forest {
6 | constructor() {
7 | privateTrees.set(this, []);
8 | }
9 | get trees() {
10 | return privateTrees.get(this);
11 | }
12 | plantTree(x, y, name, color, treeConfig) {
13 | const type = TreeFactory.getTreeType(name, color, treeConfig);
14 | const tree = new Tree(x, y, type);
15 | this.trees.push(tree);
16 | }
17 | render(canvas) {
18 | this.trees.forEach((tree) => {
19 | tree.render(canvas);
20 | });
21 | }
22 | }
23 |
24 | export default Forest;
--------------------------------------------------------------------------------
/Flyweight/scripts/Tree.js:
--------------------------------------------------------------------------------
1 | const privateX = new WeakMap();
2 | const privateY = new WeakMap();
3 | const privateTreeType = new WeakMap();
4 |
5 | class Tree {
6 | constructor(x = 0, y = 0, treeType) {
7 | privateX.set(this, x);
8 | privateY.set(this, y);
9 | privateTreeType.set(this, treeType);
10 | }
11 | get x() {
12 | return privateX.get(this);
13 | }
14 | get y() {
15 | return privateY.get(this);
16 | }
17 | get treeType() {
18 | return privateTreeType.get(this);
19 | }
20 | render(canvas) {
21 | const context = canvas.getContext("2d");
22 | this.treeType.render(context, this.x, this.y);
23 | }
24 | }
25 |
26 | export default Tree;
--------------------------------------------------------------------------------
/Flyweight/scripts/TreeFactory.js:
--------------------------------------------------------------------------------
1 | import TreeType from './TreeType';
2 |
3 | const treeTypesMap = new Map();
4 | class TreeFactory {
5 | static getTreeType(name, color, treeConfig) {
6 | let result = treeTypesMap.get(name);
7 | if(result == null) {
8 | result = new TreeType(name, color, treeConfig);
9 | treeTypesMap.set(name, result);
10 | }
11 | return result;
12 | }
13 | }
14 |
15 | export default TreeFactory;
--------------------------------------------------------------------------------
/Flyweight/scripts/TreeType.js:
--------------------------------------------------------------------------------
1 | const privateName = new WeakMap();
2 | const privateColor = new WeakMap();
3 | const privateTreeConfig = new WeakMap();
4 |
5 | class TreeType {
6 | constructor(name, color, treeConfig) {
7 | privateName.set(this, name);
8 | privateColor.set(this, color);
9 | privateTreeConfig.set(this, treeConfig);
10 | }
11 | get name() {
12 | return privateName.set(this);
13 | }
14 | get color() {
15 | return privateColor.set(this);
16 | }
17 | get treeConfig() {
18 | return privateTreeConfig.set(this);
19 | }
20 | render(context, x, y) {
21 | context.fillStyle = "black";
22 | context.fillRect(x - 1, y, 3, 5);
23 | }
24 | }
25 |
26 | export default TreeType;
--------------------------------------------------------------------------------
/Flyweight/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Forest from './Forest';
2 |
3 | const CANVAS_SIZE = 600;
4 | const TREES_TO_DRAW = 99900;
5 | const TREE_TYPES = 2;
6 | const forest = new Forest();
7 |
8 | const getAmountOfTreesToRender = (amount, types) => {
9 | return Math.floor(amount / types);
10 | };
11 |
12 | const random = (min, max) => {
13 | return min + (Math.random() * ((max - min) + 1));
14 | }
15 | const renderForest = (forest, canvas) => {
16 | for(let i = 0; i < getAmountOfTreesToRender(TREES_TO_DRAW, TREE_TYPES); i++) {
17 | forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE), 'Red Maple', 'red', 'Red Maple texture');
18 | forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE), 'Gray Birch', 'gray', 'Gray Birch texture stub');
19 | }
20 |
21 | forest.render(canvas);
22 |
23 | console.log(TREES_TO_DRAW + ' trees rendered');
24 | console.log('Memory usage:');
25 | console.log('Tree size (8 bytes) * ' + TREES_TO_DRAW + '+ TreeTypes size (~30 bytes) * ' + TREE_TYPES);
26 | console.log('Total: ' + ((TREES_TO_DRAW * 8 + TREE_TYPES * 30) / 1024 / 1024) + 'MB (instead of ' + ((TREES_TO_DRAW * 38) / 1024 / 1024) + 'MB)');
27 | }
28 |
29 | renderForest(new Forest(), document.createElement('canvas'));
--------------------------------------------------------------------------------
/Iterator/1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Iterator Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Mattress from './Mattress';
11 |
12 | let oMattress = new Mattress();
13 | console.log("---------------------------------------------");
14 | oMattress.printMenu();
15 | console.log("---------------------------------------------");
16 | oMattress.printBreakfastMenu();
17 | console.log("---------------------------------------------");
18 | oMattress.printLunchMenu();
19 |
20 |
21 |
22 |
Console
23 |
24 |
ITERATOR
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Iterator/1/scripts/LunchMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from './Menu';
2 | import MenuItem from './MenuItem';
3 | const MAX_ITEMS = 6;
4 |
5 | class LunchMenu extends Menu {
6 | constructor() {
7 | super();
8 | this.addItem("Vegetarian BLT", "(Fakin') Bacon with lettuce and tomato on whole wheat", true, 2.99);
9 | this.addItem("BLT", "Bacon with lettuce and tomato on whole wheat", false, 2.99);
10 | this.addItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29);
11 | this.addItem("Hotdog", "A hotdog with saurkraut, relish, onions, topped with cheese", false, 3.05);
12 | }
13 |
14 | addItem(name, description, isVegetarian, price) {
15 | if (this.length === MAX_ITEMS) {
16 | throw new Error("Sorry menu is full! Can't add item to menu");
17 | }
18 | super.addItem(new MenuItem({
19 | name: name,
20 | description: description,
21 | isVegetarian: isVegetarian,
22 | price: price
23 | }));
24 | }
25 | }
26 |
27 | export default LunchMenu;
28 |
--------------------------------------------------------------------------------
/Iterator/1/scripts/Mattress.js:
--------------------------------------------------------------------------------
1 | import PancakeHouseMenu from './PancakeHouseMenu';
2 | import LunchMenu from './LunchMenu';
3 |
4 | function processArray(array, callback) {
5 | for (let item of array) {
6 | if (!callback(item)) {
7 | break;
8 | }
9 | }
10 | }
11 |
12 | function printEachMenu(array) {
13 | processArray(array, function(item) {
14 | console.log(item.getName() + ": " + item.getDescription() + ", " + item.getPrice() + "eur.");
15 | });
16 | }
17 |
18 | function printEachVegetarianMenu(array, name) {
19 | processArray(array, function(item) {
20 | if (item.getName() === name) {
21 | return item.isVegetarian();
22 | }
23 | });
24 | }
25 |
26 | class Mattress {
27 | constructor() {
28 | this.breakfastItems = new PancakeHouseMenu().getMenuItems();
29 | this.lunchItems = new LunchMenu().getMenuItems();
30 | }
31 |
32 | printBreakfastMenu() {
33 | printEachMenu(this.breakfastItems);
34 | }
35 |
36 | printLunchMenu() {
37 | printEachMenu(this.lunchItems);
38 | }
39 |
40 | printMenu() {
41 | this.printBreakfastMenu();
42 | this.printLunchMenu();
43 | }
44 |
45 | isItemVegetarian(name) {
46 | printEachVegetarianMenu(this.breakfastItems, name);
47 | printEachVegetarianMenu(this.lunchItems, name);
48 |
49 | throw new Error("Sorry, but we don't have this in our menu!");
50 | }
51 | }
52 |
53 | export default Mattress;
54 |
--------------------------------------------------------------------------------
/Iterator/1/scripts/Menu.js:
--------------------------------------------------------------------------------
1 | class Menu {
2 | constructor() {
3 | this.menuItems = [];
4 | this.length = 0;
5 | }
6 |
7 | addItem(menuItem) {
8 | this.menuItems.push(menuItem);
9 | this.length++;
10 | }
11 |
12 | getMenuItems() {
13 | return this.menuItems;
14 | }
15 | }
16 |
17 |
18 | export default Menu;
19 |
--------------------------------------------------------------------------------
/Iterator/1/scripts/MenuItem.js:
--------------------------------------------------------------------------------
1 | class MenuItem {
2 | constructor({ name = '', description = '', isVegetarian = false, price = 0 }) {
3 | this.name = name;
4 | this.description = description;
5 | this._isVegetarian = isVegetarian;
6 | this.price = price;
7 | }
8 |
9 | getName() {
10 | return this.name;
11 | }
12 |
13 | getDescription() {
14 | return this.description;
15 | }
16 |
17 | getPrice() {
18 | return this.price;
19 | }
20 |
21 | isVegetarian() {
22 | return this._isVegetarian;
23 | }
24 | }
25 |
26 | export default MenuItem;
27 |
--------------------------------------------------------------------------------
/Iterator/1/scripts/PancakeHouseMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from './Menu';
2 | import MenuItem from './MenuItem';
3 |
4 | class PancakeHouseMenu extends Menu {
5 | constructor() {
6 | super();
7 | this.addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99);
8 | this.addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99);
9 | this.addItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49);
10 | this.addItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59);
11 | }
12 |
13 | addItem(name, description, isVegetarian, price) {
14 | super.addItem(new MenuItem({
15 | name: name,
16 | description: description,
17 | isVegetarian: isVegetarian,
18 | price: price
19 | }));
20 | }
21 |
22 | getMenuItems() {
23 | return this.menuItems;
24 | }
25 | }
26 |
27 | export default PancakeHouseMenu;
28 |
--------------------------------------------------------------------------------
/Iterator/1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Mattress from './Mattress';
2 |
3 | let oMattress = new Mattress();
4 | console.log("---------------------------------------------");
5 | oMattress.printMenu();
6 | console.log("---------------------------------------------");
7 | oMattress.printBreakfastMenu();
8 | console.log("---------------------------------------------");
9 | oMattress.printLunchMenu();
10 |
--------------------------------------------------------------------------------
/Iterator/2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Iterator Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Mattress from './Mattress';
11 | import PancakeHouseMenu from './PancakeHouseMenu';
12 | import LunchMenu from './LunchMenu';
13 |
14 | let oMattress = new Mattress({
15 | pancakeHouseMenu: new PancakeHouseMenu(),
16 | lunchMenu: new LunchMenu()
17 | });
18 | console.log("---------------------------------------------");
19 | oMattress.printMenu();
20 | console.log("---------------------------------------------");
21 | oMattress.printBreakfastMenu();
22 | console.log("---------------------------------------------");
23 | oMattress.printLunchMenu();
24 |
25 |
26 |
27 |
Console
28 |
29 |
ITERATOR
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/Iterator/2/scripts/LunchMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from '../../common/Menu';
2 | import MenuItem from '../../common/MenuItem';
3 | import Iterator from '../../common/Iterator';
4 |
5 | const MAX_ITEMS = 6;
6 |
7 | class LunchMenu extends Menu {
8 | constructor() {
9 | super();
10 | this.addItem("Vegetarian BLT", "(Fakin') Bacon with lettuce and tomato on whole wheat", true, 2.99);
11 | this.addItem("BLT", "Bacon with lettuce and tomato on whole wheat", false, 2.99);
12 | this.addItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29);
13 | this.addItem("Hotdog", "A hotdog with saurkraut, relish, onions, topped with cheese", false, 3.05);
14 | }
15 |
16 | addItem(name, description, isVegetarian, price) {
17 | if (this.length === MAX_ITEMS) {
18 | throw new Error("Sorry menu is full! Can't add item to menu");
19 | }
20 | super.addItem(new MenuItem(name, description, isVegetarian, price));
21 | }
22 |
23 | createIterator() {
24 | return new Iterator(this.menuItems);
25 | }
26 | }
27 |
28 | export default LunchMenu;
29 |
--------------------------------------------------------------------------------
/Iterator/2/scripts/PancakeHouseMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from '../../common/Menu';
2 | import MenuItem from '../../common/MenuItem';
3 | import Iterator from '../../common/Iterator';
4 |
5 | class PancakeHouseMenu extends Menu {
6 | constructor() {
7 | super();
8 | this.addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99);
9 | this.addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99);
10 | this.addItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49);
11 | this.addItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59);
12 | }
13 |
14 | addItem(name, description, isVegetarian, price) {
15 | super.addItem(new MenuItem(name, description, isVegetarian, price));
16 | }
17 |
18 | createIterator() {
19 | return new Iterator(this.menuItems);
20 | }
21 | }
22 |
23 | export default PancakeHouseMenu;
24 |
--------------------------------------------------------------------------------
/Iterator/2/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Mattress from './Mattress';
2 | import PancakeHouseMenu from './PancakeHouseMenu';
3 | import LunchMenu from './LunchMenu';
4 |
5 | let oMattress = new Mattress({
6 | pancakeHouseMenu: new PancakeHouseMenu(),
7 | lunchMenu: new LunchMenu()
8 | });
9 | console.log("---------------------------------------------");
10 | oMattress.printMenu();
11 | console.log("---------------------------------------------");
12 | oMattress.printBreakfastMenu();
13 | console.log("---------------------------------------------");
14 | oMattress.printLunchMenu();
15 |
--------------------------------------------------------------------------------
/Iterator/3/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Iterator Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Mattress from './Mattress';
11 | import PancakeHouseMenu from './PancakeHouseMenu';
12 | import LunchMenu from './LunchMenu';
13 |
14 | var oMattress = new Mattress({
15 | pancakeHouseMenu: new PancakeHouseMenu(),
16 | lunchMenu: new LunchMenu()
17 | });
18 | console.log("---------------------------------------------");
19 | oMattress.printMenu();
20 | console.log("---------------------------------------------");
21 | oMattress.printBreakfastMenu();
22 | console.log("---------------------------------------------");
23 | oMattress.printLunchMenu();
24 |
25 |
26 |
27 |
Console
28 |
29 |
ITERATOR
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/Iterator/3/scripts/LunchMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from '../../common/Menu';
2 | import Iterator from '../../common/Iterator';
3 | import MenuItem from '../../common/MenuItem';
4 |
5 | const MAX_ITEMS = 6;
6 |
7 | class LunchMenu extends Menu {
8 | constructor() {
9 | super();
10 | this.addItem("Vegetarian BLT", "(Fakin') Bacon with lettuce and tomato on whole wheat", true, 2.99);
11 | this.addItem("BLT", "Bacon with lettuce and tomato on whole wheat", false, 2.99);
12 | this.addItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29);
13 | this.addItem("Hotdog", "A hotdog with saurkraut, relish, onions, topped with cheese", false, 3.05);
14 | }
15 | createIterator() {
16 | return new Iterator(this.menuItems);
17 | }
18 | addItem(name, description, isVegetarian, price) {
19 | if (this.length === MAX_ITEMS) {
20 | throw new Error("Sorry menu is full! Can't add item to menu");
21 | }
22 | super.addItem(new MenuItem(name, description, isVegetarian, price));
23 | }
24 | }
25 |
26 | export default LunchMenu;
27 |
--------------------------------------------------------------------------------
/Iterator/3/scripts/PancakeHouseMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from '../../common/Menu';
2 | import Iterator from '../../common/Iterator';
3 | import MenuItem from '../../common/MenuItem';
4 |
5 | class PancakeHouseMenu extends Menu {
6 | constructor() {
7 | super();
8 | this.addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99);
9 | this.addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99);
10 | this.addItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49);
11 | this.addItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59);
12 | }
13 |
14 | addItem(name, description, isVegetarian, price) {
15 | super.addItem(new MenuItem(name, description, isVegetarian, price));
16 | }
17 |
18 | createIterator() {
19 | return new Iterator(this.menuItems);
20 | }
21 | }
22 |
23 | export default PancakeHouseMenu;
24 |
--------------------------------------------------------------------------------
/Iterator/3/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Mattress from './Mattress';
2 | import PancakeHouseMenu from './PancakeHouseMenu';
3 | import LunchMenu from './LunchMenu';
4 |
5 | var oMattress = new Mattress({
6 | pancakeHouseMenu: new PancakeHouseMenu(),
7 | lunchMenu: new LunchMenu()
8 | });
9 | console.log("---------------------------------------------");
10 | oMattress.printMenu();
11 | console.log("---------------------------------------------");
12 | oMattress.printBreakfastMenu();
13 | console.log("---------------------------------------------");
14 | oMattress.printLunchMenu();
15 |
--------------------------------------------------------------------------------
/Iterator/4/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Iterator Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Mattress from './Mattress';
11 | import PancakeHouseMenu from './PancakeHouseMenu';
12 | import LunchMenu from './LunchMenu';
13 | import CafeMenu from './CafeMenu';
14 |
15 | var oMattress = new Mattress([new PancakeHouseMenu(), new LunchMenu(), new CafeMenu()]);
16 | console.log("---------------------------------------------");
17 | oMattress.printMenu();
18 | console.log("---------------------------------------------");
19 |
20 |
21 |
22 |
Console
23 |
24 |
ITERATOR
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Iterator/4/scripts/CafeMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from '../../common/Menu';
2 | import MenuItem from '../../common/MenuItem';
3 | import Iterator from '../../common/Iterator';
4 |
5 | class CafeMenu extends Menu {
6 | constructor() {
7 | super();
8 | this.addItem("Express", "Coffee from machine", false, 0.99);
9 | this.addItem("Long with water", "Coffee with a lot of water", false, 1.20);
10 | this.addItem("On the rocks", "Coffee with ice", false, 2.00);
11 | }
12 | createIterator() {
13 | return new Iterator(this.menuItems);
14 | }
15 | addItem(name, description, isVegetarian, price) {
16 | super.addItem(new MenuItem(name, description, isVegetarian, price));
17 | }
18 | }
19 |
20 | export default CafeMenu;
21 |
--------------------------------------------------------------------------------
/Iterator/4/scripts/LunchMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from '../../common/Menu';
2 | import MenuItem from '../../common/MenuItem';
3 | import Iterator from '../../common/Iterator';
4 |
5 | const MAX_ITEMS = 6;
6 | class LunchMenu extends Menu {
7 | constructor() {
8 | super();
9 | this.addItem("Vegetarian BLT", "(Fakin') Bacon with lettuce and tomato on whole wheat", true, 2.99);
10 | this.addItem("BLT", "Bacon with lettuce and tomato on whole wheat", false, 2.99);
11 | this.addItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29);
12 | this.addItem("Hotdog", "A hotdog with saurkraut, relish, onions, topped with cheese", false, 3.05);
13 | }
14 | createIterator() {
15 | return new Iterator(this.menuItems);
16 | }
17 | addItem(name, description, isVegetarian, price) {
18 | if(this.length === MAX_ITEMS){
19 | super.addItem(new MenuItem(name, description, isVegetarian, price));
20 | }
21 | }
22 | }
23 |
24 | export default LunchMenu;
25 |
--------------------------------------------------------------------------------
/Iterator/4/scripts/Mattress.js:
--------------------------------------------------------------------------------
1 | function printMenu(iterator) {
2 | let menuItem = iterator.next();
3 | while(menuItem.value) {
4 | console.log(menuItem.value.getName() + ": " + menuItem.value.getDescription() + ", " + menuItem.value.getPrice() + "eur.");
5 | menuItem = iterator.next();
6 | }
7 | }
8 |
9 | class Mattress {
10 | constructor(menus) {
11 | this.menus = menus;
12 | }
13 | printMenu() {
14 | this.menus.forEach(function (menu) {
15 | let iterator = menu.createIterator();
16 | printMenu(iterator);
17 | });
18 | }
19 | }
20 |
21 | export default Mattress;
22 |
--------------------------------------------------------------------------------
/Iterator/4/scripts/PancakeHouseMenu.js:
--------------------------------------------------------------------------------
1 | import Menu from '../../common/Menu';
2 | import MenuItem from '../../common/MenuItem';
3 | import Iterator from '../../common/Iterator';
4 |
5 | class PancakeHouseMenu extends Menu {
6 | constructor() {
7 | super();
8 | this.addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99);
9 | this.addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99);
10 | this.addItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49);
11 | this.addItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59);
12 | }
13 | createIterator() {
14 | return new Iterator(this.menuItems);
15 | }
16 | addItem(name, description, isVegetarian, price) {
17 | super.addItem(new MenuItem(name, description, isVegetarian, price));
18 | }
19 | }
20 |
21 | export default PancakeHouseMenu;
22 |
--------------------------------------------------------------------------------
/Iterator/4/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Mattress from './Mattress';
2 | import PancakeHouseMenu from './PancakeHouseMenu';
3 | import LunchMenu from './LunchMenu';
4 | import CafeMenu from './CafeMenu';
5 |
6 | var oMattress = new Mattress([new PancakeHouseMenu(), new LunchMenu(), new CafeMenu()]);
7 | console.log("---------------------------------------------");
8 | oMattress.printMenu();
9 | console.log("---------------------------------------------");
10 |
--------------------------------------------------------------------------------
/Iterator/common/Iterator.js:
--------------------------------------------------------------------------------
1 | class Iterator {
2 | constructor(menuItems) {
3 | this.iterator = menuItems[Symbol.iterator]();
4 | this.keys = Object.keys(menuItems);
5 | this.length = this.keys.length;
6 | }
7 |
8 | next() {
9 | return this.iterator.next();
10 | }
11 |
12 | remove(key) {
13 | delete this.menuItems[key];
14 | this.keys = Object.keys(this.menuItems);
15 | this.length = this.keys.length;
16 | }
17 | }
18 |
19 | export default Iterator;
20 |
--------------------------------------------------------------------------------
/Iterator/common/Menu.js:
--------------------------------------------------------------------------------
1 | class Menu {
2 | constructor() {
3 | this.menuItems = [];
4 | this.length = 0;
5 | }
6 |
7 | addItem(menuItem) {
8 | this.menuItems.push(menuItem);
9 | this.length++;
10 | }
11 |
12 | getMenuItems() {
13 | return this.menuItems;
14 | }
15 | }
16 |
17 | export default Menu;
18 |
--------------------------------------------------------------------------------
/Iterator/common/MenuItem.js:
--------------------------------------------------------------------------------
1 | class MenuItem {
2 | constructor(name, description, isVegetarian, price) {
3 | this.name = name;
4 | this.description = description;
5 | this._isVegetarian = isVegetarian;
6 | this.price = price;
7 | }
8 |
9 | getName() {
10 | return this.name;
11 | }
12 |
13 | getDescription() {
14 | return this.description;
15 | }
16 |
17 | getPrice() {
18 | return this.price;
19 | }
20 |
21 | isVegetarian() {
22 | return this._isVegetarian;
23 | }
24 | }
25 |
26 | export default MenuItem;
27 |
--------------------------------------------------------------------------------
/Lazy/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Lazy Pattern
5 |
14 |
15 |
16 |
17 |
Source
18 |
19 | import Lazy from './Lazy';
20 |
21 | let counter = 0;
22 | const elements = ['Zero', 'First', 'Second', 'Third', 'Fourth'];
23 |
24 | let timeout = null;
25 | let lazy = new Lazy(document.getElementById('test'), elements[counter], new Date());
26 |
27 | timeout = setInterval(function() {
28 | if (counter === 4) {
29 | clearInterval(timeout);
30 | }
31 | lazy.update(elements[counter++], new Date());
32 | }, 500);
33 |
34 |
35 |
36 |
Console
37 |
38 |
39 |
Lazy
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/Lazy/scripts/Lazy.js:
--------------------------------------------------------------------------------
1 | class Lazy {
2 | constructor(container, text, date) {
3 | this.container = container;
4 | this.update(text, date);
5 | }
6 |
7 | static addZero(time) {
8 | return time < 10 ? '0' + time : time;
9 | }
10 |
11 | getFormattedTime(date) {
12 | return Lazy.addZero(date.getHours()) + ":" + Lazy.addZero(date.getMinutes()) + ":" + Lazy.addZero(date.getSeconds());
13 | }
14 |
15 | update(text, date) {
16 | this.container.innerHTML = `
17 |
18 |
19 | Not changed:
20 |
21 | ${this.getFormattedTime(new Date())}
22 |
23 |
24 |
25 | ${text}
26 |
27 |
28 | ${this.getFormattedTime(date)}
29 |
30 |
31 | `;
32 | this.update = (text, date) => {
33 | var textNode = this.container.querySelector('.text');
34 | var timeNode = this.container.querySelector('.time');
35 | textNode.innerHTML = text;
36 | timeNode.innerHTML = this.getFormattedTime(date);
37 | };
38 | }
39 | }
40 |
41 | export default Lazy;
42 |
--------------------------------------------------------------------------------
/Lazy/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Lazy from './Lazy';
2 |
3 | let counter = 0;
4 | const elements = ['Zero', 'First', 'Second', 'Third', 'Fourth'];
5 |
6 | let timeout = null;
7 | let lazy = new Lazy(document.getElementById('test'), elements[counter], new Date());
8 |
9 | timeout = setInterval(function() {
10 | if (counter === 4) {
11 | clearInterval(timeout);
12 | }
13 | lazy.update(elements[counter++], new Date());
14 | }, 500);
15 |
--------------------------------------------------------------------------------
/MVC/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | MVC Pattern
4 |
5 |
19 |
20 |
21 |
22 |
Source
23 |
24 | import ListView from './ListView';
25 | import TodoModel from './TodoModel';
26 | import ListController from './ListController';
27 |
28 | let oListView = new ListView(document.getElementById("results"));
29 | let oTodoModel = new TodoModel();
30 | let oListController = new ListController(oListView, oTodoModel);
31 |
32 | oListController.init();
33 |
34 |
35 |
36 |
Console
37 |
38 |
M.V.C.
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/MVC/scripts/ListController.js:
--------------------------------------------------------------------------------
1 | import $ from './jquery';
2 |
3 | class ListController {
4 | constructor(view, model) {
5 | this.view = view;
6 | this.model = model;
7 | }
8 |
9 | init() {
10 | this.model
11 | .all()
12 | .then(
13 | items => {
14 | let html = '';
15 |
16 | items.forEach((item, index) => {
17 | let className = item.done ? 'done' : 'todo';
18 | html += `${item.text} `;
19 | });
20 |
21 | this.view.innerHTML = html;
22 | $(this.view).on('click', '.todo', event => {
23 | var element = event.target;
24 | this.model
25 | .completeItem(element.id)
26 | .then(
27 | () => {
28 | element.className = 'done';
29 | }
30 | );
31 | }).on('click', '.done', event => {
32 | var element = event.target;
33 | this.model
34 | .undoItem(element.id)
35 | .then(() => {
36 | element.className = 'todo';
37 | })
38 | });
39 | },
40 | error => {
41 | this.handleError('Server failed to get todo items', error);
42 | }
43 | );
44 | }
45 |
46 | handleError(message) {
47 | var className = 'error';
48 | this.view.innerHTML += `${message} `;
49 | }
50 | }
51 |
52 | export default ListController;
53 |
--------------------------------------------------------------------------------
/MVC/scripts/ListView.js:
--------------------------------------------------------------------------------
1 | class ListView {
2 | constructor(element) {
3 | return element;
4 | }
5 | }
6 |
7 | export default ListView;
8 |
--------------------------------------------------------------------------------
/MVC/scripts/jquery.js:
--------------------------------------------------------------------------------
1 | export default window.jQuery;;
2 |
--------------------------------------------------------------------------------
/MVC/scripts/main.js:
--------------------------------------------------------------------------------
1 | import ListView from './ListView';
2 | import TodoModel from './TodoModel';
3 | import ListController from './ListController';
4 |
5 | let oListView = new ListView(document.getElementById("results"));
6 | let oTodoModel = new TodoModel();
7 | let oListController = new ListController(oListView, oTodoModel);
8 |
9 | oListController.init();
10 |
--------------------------------------------------------------------------------
/Module Revealed/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Module Revealed Pattern
5 |
14 |
15 |
16 |
17 |
Source
18 |
19 | import ModuleRevealed from './ModuleRevealed';
20 |
21 | ModuleRevealed.init(document.getElementById('test'));
22 |
23 |
24 |
25 |
Console
26 |
27 |
28 |
Module Revealed
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/Module Revealed/scripts/ModuleRevealed.js:
--------------------------------------------------------------------------------
1 | export default (function(win) {
2 | var oContainer = null;
3 |
4 | function setContainer(oCont) {
5 | oContainer = oCont;
6 | }
7 |
8 | function addZero(nTime) {
9 | return nTime < 10 ? '0' + nTime : nTime;
10 | }
11 |
12 | function getFormattedTime(dTime) {
13 | return addZero(dTime.getHours()) + ":" + addZero(dTime.getMinutes()) + ":" + addZero(dTime.getSeconds());
14 | }
15 |
16 | function insertTestModule() {
17 | oContainer.innerHTML = 'Test Module: ' + getFormattedTime(new Date());
18 | }
19 |
20 | function removeContent() {
21 | oContainer.innerHTML = '';
22 | }
23 |
24 | return {
25 | init: function(oContainer) {
26 | setContainer(oContainer);
27 | insertTestModule();
28 | },
29 | destroy: function() {
30 | removeContent();
31 | }
32 | };
33 | }());
34 |
--------------------------------------------------------------------------------
/Module Revealed/scripts/main.js:
--------------------------------------------------------------------------------
1 | import ModuleRevealed from './ModuleRevealed';
2 |
3 | ModuleRevealed.init(document.getElementById('test'));
4 |
--------------------------------------------------------------------------------
/Module/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Module Pattern
5 |
14 |
15 |
16 |
17 |
Source
18 |
19 | import Module from './Module';
20 |
21 | var oModule = Module(document.getElementById('test'));
22 | oModule.init();
23 |
24 |
25 |
26 |
Console
27 |
28 |
29 |
Module
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/Module/scripts/Module.js:
--------------------------------------------------------------------------------
1 | function Module(container) {
2 | return new class {
3 | get container() {
4 | return container;
5 | }
6 |
7 | init() {
8 | this.container.innerHTML = 'Test module';
9 | }
10 |
11 | destroy() {
12 | this.container.innerHTML = '';
13 | delete this.container;
14 | }
15 | }
16 | }
17 |
18 | export default Module;
19 |
--------------------------------------------------------------------------------
/Module/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Module from './Module';
2 |
3 | var oModule = Module(document.getElementById('test'));
4 | oModule.init();
5 |
--------------------------------------------------------------------------------
/Multi-Inheritance-ES6/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Multi-Inheritance Pattern
5 |
14 |
15 |
16 |
17 |
Source
18 |
19 | import Duck from './Duck';
20 |
21 | var duck = new Duck();
22 |
23 | duck.fly();
24 | duck.quack();
25 | duck.swim();
26 |
27 |
28 |
29 |
Console
30 |
31 |
32 |
Multi-Inheritance ES6
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/Multi-Inheritance-ES6/scripts/Duck.js:
--------------------------------------------------------------------------------
1 | import Flyable from './Flyable';
2 | import Quackable from './Quackable';
3 |
4 | class Duck extends Quackable(Flyable(null)) {
5 | swim() {
6 | console.log('Chop!');
7 | }
8 | }
9 |
10 | export default Duck;
11 |
--------------------------------------------------------------------------------
/Multi-Inheritance-ES6/scripts/Flyable.js:
--------------------------------------------------------------------------------
1 | const Flyable = Sup => class extends Sup {
2 | fly() {
3 | console.log('Flap, Flap!');
4 | }
5 | };
6 |
7 | export default Flyable;
8 |
--------------------------------------------------------------------------------
/Multi-Inheritance-ES6/scripts/Quackable.js:
--------------------------------------------------------------------------------
1 | const Quackable = Sup => class extends Sup {
2 | quack() {
3 | console.log('Quack!');
4 | }
5 | };
6 |
7 | export default Quackable;
8 |
--------------------------------------------------------------------------------
/Multi-Inheritance-ES6/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 |
3 | var duck = new Duck();
4 |
5 | duck.fly();
6 | duck.quack();
7 | duck.swim();
8 |
--------------------------------------------------------------------------------
/Namespace/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Namespace Pattern
5 |
14 |
15 |
16 |
17 |
Source
18 |
19 | import Namespace from './Namespace';
20 |
21 | Namespace.DOM.byId('test');
22 |
23 | let ajax = new Namespace.Ajax();
24 | ajax.setUp().call();
25 |
26 |
27 |
28 |
Console
29 |
30 |
Namespace
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/Namespace/scripts/App.js:
--------------------------------------------------------------------------------
1 | export default {};
2 |
--------------------------------------------------------------------------------
/Namespace/scripts/Namespace.js:
--------------------------------------------------------------------------------
1 | import App from './App';
2 |
3 | export default (function(namespace) {
4 | var Ajax = function() {
5 | console.log('Ajax: Instanced!');
6 | };
7 | Ajax.prototype.setUp = function() {
8 | console.log('Ajax: Setup!');
9 | return this;
10 | };
11 | Ajax.prototype.call = function() {
12 | console.log('Ajax: Call!');
13 | };
14 | var DOM = function() {
15 | console.log('DOM: Instanced!');
16 | };
17 | DOM.prototype.byId = function(sId) {
18 | console.log('DOM: ById ' + sId + '!')
19 | };
20 | namespace.DOM = new DOM();
21 | namespace.Ajax = Ajax;
22 | return namespace;
23 | }(App));
24 |
--------------------------------------------------------------------------------
/Namespace/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Namespace from './Namespace';
2 |
3 | Namespace.DOM.byId('test');
4 |
5 | let ajax = new Namespace.Ajax();
6 | ajax.setUp().call();
7 |
--------------------------------------------------------------------------------
/Nullify/examples/Background_Links.txt:
--------------------------------------------------------------------------------
1 | http://javascript.crockford.com/memory/leak.html
2 | http://geekswithblogs.net/FrostRed/archive/2008/11/29/127440.aspx
3 | http://www.ibm.com/developerworks/web/library/wa-memleak/
4 | http://msdn.microsoft.com/en-us/library/Bb250448
5 | http://buildnewgames.com/garbage-collector-friendly-code/
--------------------------------------------------------------------------------
/Nullify/examples/problem/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Nullify Pattern
5 |
14 |
15 |
16 |
17 |
Source
18 |
19 | var nElement = 0;
20 | var nTimes = 100000;
21 | function createElement() {
22 | var div = document.createElement("div");
23 | div.onclick = function () {};
24 | }
25 | for (; nElement < nTimes; nElement++) {
26 | createElement();
27 | }
28 |
29 |
30 |
31 |
Console
32 |
33 |
34 |
Nullify
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/Nullify/examples/problem/scripts/main.js:
--------------------------------------------------------------------------------
1 | var nElement = 0;
2 | var nTimes = 100000;
3 | function createElement() {
4 | var div = document.createElement("div");
5 | div.onclick = function () {};
6 | }
7 | for (; nElement < nTimes; nElement++) {
8 | createElement();
9 | }
10 |
--------------------------------------------------------------------------------
/Nullify/examples/solution/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Nullify Pattern
5 |
14 |
15 |
16 |
17 |
Source
18 |
19 | var nElement = 0;
20 | var nTimes = 100000;
21 | function createElement() {
22 | var div = document.createElement("div");
23 | div.onclick = function () {};
24 | div = null;
25 | }
26 | for (; nElement < nTimes; nElement++) {
27 | createElement();
28 | }
29 |
30 |
31 |
32 |
Console
33 |
34 |
35 |
Nullify
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/Nullify/examples/solution/scripts/main.js:
--------------------------------------------------------------------------------
1 | var nElement = 0;
2 | var nTimes = 100000;
3 | function createElement() {
4 | var div = document.createElement("div");
5 | div.onclick = function () {};
6 | div = null;
7 | }
8 | for (; nElement < nTimes; nElement++) {
9 | createElement();
10 | }
11 |
--------------------------------------------------------------------------------
/Nullify/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Namespace Pattern
5 |
14 |
15 |
16 |
17 |
Source
18 |
19 | import Nullify from './Nullify';
20 |
21 | var oNullify = new Nullify();
22 | oNullify.fillContent();
23 |
24 |
25 |
26 |
Console
27 |
32 |
33 |
Nullify
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/Nullify/scripts/Nullify.js:
--------------------------------------------------------------------------------
1 | class Nullify {
2 | fillContent() {
3 | let test1 = document.getElementById("test1");
4 | let test2 = document.getElementById("test2");
5 | let test3 = document.getElementById("test3");
6 |
7 | test1.onclick = function () {};
8 | test2.onclick = function () {};
9 | test3.onclick = function () {};
10 |
11 | test1.innerHTML = 'TEST 1';
12 | test2.innerHTML = 'TEST 2';
13 | test3.innerHTML = 'TEST 3';
14 |
15 | test1 = test2 = test3 = null;
16 | }
17 | }
18 |
19 | export default Nullify;
20 |
--------------------------------------------------------------------------------
/Nullify/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Nullify from './Nullify';
2 |
3 | var oNullify = new Nullify();
4 | oNullify.fillContent();
5 |
--------------------------------------------------------------------------------
/Observer/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Observer Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | var oWeatherData = new WeatherData();
11 | var oCurrentConditionsDisplay = new CurrentConditionsDisplay(oWeatherData);
12 | oWeatherData.setMeasurements(80, 65, 30.4);
13 |
14 |
15 |
16 |
Console
17 |
18 |
OBSERVER
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/Observer/scripts/CurrentConditionsDisplay.js:
--------------------------------------------------------------------------------
1 | import Observer from './Observable';
2 | import Display from './Displayable';
3 |
4 | class CurrentConditionsDisplay extends Observer(Display(null)) {
5 | constructor(subject) {
6 | super();
7 | this.temperature = 0;
8 | this.humidity = 0;
9 | this.pressure = 0;
10 | this.subject = subject;
11 | this.subject.registerObserver(this);
12 | }
13 |
14 | update(temperature, humidity, pressure) {
15 | this.temperature = temperature;
16 | this.humidity = humidity;
17 | this.pressure = pressure;
18 | this.display();
19 | }
20 |
21 | display() {
22 | console.log("Current conditions: " + this.temperature + "F degrees and " + this.humidity + "% humidity.");
23 | }
24 | }
25 |
26 | export default CurrentConditionsDisplay;
27 |
--------------------------------------------------------------------------------
/Observer/scripts/Displayable.js:
--------------------------------------------------------------------------------
1 | const Displayable = Sup => class extends Sup {
2 | display() {
3 | throw new Error('This method should be overwritten!');
4 | }
5 | };
6 |
7 | export default Displayable;
8 |
--------------------------------------------------------------------------------
/Observer/scripts/Observable.js:
--------------------------------------------------------------------------------
1 | const Observable = Sup => class extends Sup {
2 | update() {
3 | throw new Error("This method must be overwritten!");
4 | };
5 | };
6 |
7 | export default Observable;
8 |
--------------------------------------------------------------------------------
/Observer/scripts/Subject.js:
--------------------------------------------------------------------------------
1 | class Subject {
2 | registerObserver() {
3 | throw new Error("This method must be overwritten!");
4 | }
5 |
6 | removeObserver() {
7 | throw new Error("This method must be overwritten!");
8 | }
9 |
10 | notifyObservers() {
11 | throw new Error("This method must be overwritten!");
12 | }
13 | }
14 |
15 | export default Subject;
16 |
--------------------------------------------------------------------------------
/Observer/scripts/WeatherData.js:
--------------------------------------------------------------------------------
1 | import Subject from './Subject';
2 |
3 | class WeatherData extends Subject {
4 | constructor() {
5 | super();
6 | this.observers = {};
7 | this.temperature = 0;
8 | this.humidity = 0;
9 | this.pressure = 0;
10 | }
11 |
12 | registerObserver(observer) {
13 | this.observers[observer.id] = observer;
14 | }
15 |
16 | removeObserver(observer) {
17 | delete this.observers[observer.id];
18 | }
19 |
20 | notifyObservers() {
21 | for (let observerId in this.observers) {
22 | if (this.observers.hasOwnProperty(observerId)) {
23 | this.observers[observerId].update(this.temperature, this.humidity, this.pressure);
24 | }
25 | }
26 | }
27 |
28 | measurementsChanged() {
29 | this.notifyObservers();
30 | }
31 |
32 | setMeasurements(temperature, humidity, pressure) {
33 | this.temperature = temperature;
34 | this.humidity = humidity;
35 | this.pressure = pressure;
36 |
37 | this.measurementsChanged();
38 | }
39 | }
40 |
41 | export default WeatherData;
42 |
--------------------------------------------------------------------------------
/Observer/scripts/main.js:
--------------------------------------------------------------------------------
1 | import WeatherData from './WeatherData';
2 | import CurrentConditionsDisplay from './CurrentConditionsDisplay';
3 |
4 | let oWeatherData = new WeatherData();
5 | new CurrentConditionsDisplay(oWeatherData);
6 | oWeatherData.setMeasurements(80, 65, 30.4);
7 |
--------------------------------------------------------------------------------
/Prototype/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Prototype Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import HumanBeing from './HumanBeing';
11 |
12 | var me = new HumanBeing({ skinColor: 'pale', hairColor: 'brown', height:'173cm', weight: '100kg', gender: 'male'});
13 |
14 | var clone = me.clone();
15 |
16 | console.log(`Are original and clone the same instance? ${me === clone}`);
17 |
18 | for(let key in me) {
19 | console.log(`Are both ${key} property values in original and clone the same value? ${me[key] === clone[key]}`);
20 | }
21 |
22 |
23 |
24 |
Console
25 |
26 |
Prototype
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/Prototype/scripts/HumanBeing.js:
--------------------------------------------------------------------------------
1 | class HumanBeing {
2 | constructor(config) {
3 | this.skinColor = config.skinColor;
4 | this.hairColor = config.hairColor;
5 | this.height = config.height;
6 | this.weight = config.weight;
7 | this.gender = config.gender;
8 | // And more data.
9 | }
10 | clone() {
11 | return new HumanBeing(Object.assign({}, this));
12 | }
13 | }
14 |
15 | export default HumanBeing;
--------------------------------------------------------------------------------
/Prototype/scripts/main.js:
--------------------------------------------------------------------------------
1 | import HumanBeing from './HumanBeing';
2 |
3 | var me = new HumanBeing({ skinColor: 'pale', hairColor: 'brown', height:'173cm', weight: '100kg', gender: 'male'});
4 |
5 | var clone = me.clone();
6 |
7 | console.log(`Are original and clone the same instance? ${me === clone}`);
8 |
9 | for(let key in me) {
10 | console.log(`Are both ${key} property values in original and clone the same value? ${me[key] === clone[key]}`);
11 | }
--------------------------------------------------------------------------------
/Proxy/1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Proxy Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import PublicLibraryProxy from './PublicLibraryProxy';
11 |
12 | var oProxyLibrary = new PublicLibraryProxy([]);
13 | oProxyLibrary.findBooks('test');
14 |
15 |
16 |
17 |
Console
18 |
19 |
PROXY
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Proxy/1/scripts/PublicLibraryProxy.js:
--------------------------------------------------------------------------------
1 | import PublicLibrary from '../../common/PublicLibrary';
2 |
3 | class PublicLibraryProxy {
4 | constructor(catalog = []) {
5 | this.library = new PublicLibrary(catalog);
6 | }
7 |
8 | findBooks(query) {
9 | console.log("Enter findBooks PublicLibraryProxy");
10 | return this.library.findBooks(query);
11 | }
12 |
13 | checkoutBook(book) {
14 | return this.library.checkoutBook(book);
15 | }
16 |
17 | returnBook(book) {
18 | return this.library.returnBook(book);
19 | }
20 | }
21 |
22 | export default PublicLibraryProxy;
23 |
--------------------------------------------------------------------------------
/Proxy/1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import PublicLibraryProxy from './PublicLibraryProxy';
2 |
3 | var oProxyLibrary = new PublicLibraryProxy();
4 | oProxyLibrary.findBooks('test');
5 |
--------------------------------------------------------------------------------
/Proxy/2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Proxy Virtual Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import PublicLibraryVirtualProxy from './PublicLibraryVirtualProxy';
11 |
12 | let oVirtualProxyLibrary = new PublicLibraryVirtualProxy();
13 | oVirtualProxyLibrary.findBooks('test');
14 |
15 |
16 |
17 |
Console
18 |
19 |
PROXY VIRTUAL
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Proxy/2/scripts/PublicLibraryVirtualProxy.js:
--------------------------------------------------------------------------------
1 | import PublicLibrary from '../../common/PublicLibrary';
2 |
3 | function initializeLibrary(instance) {
4 | if (instance.library === null) {
5 | instance.library = new PublicLibrary(instance.catalog);
6 | }
7 | }
8 |
9 | class PublicLibraryVirtualProxy {
10 | constructor(catalog = []) {
11 | this.library = null;
12 | this.catalog = catalog;
13 | }
14 |
15 | findBooks(query) {
16 | console.log("Enter findBooks PublicLibraryVirtualProxy");
17 | initializeLibrary(this);
18 | return this.library.findBooks(query);
19 | }
20 |
21 | checkoutBook(book) {
22 | initializeLibrary(this);
23 | return this.library.checkoutBook(book);
24 | }
25 |
26 | returnBook(book) {
27 | initializeLibrary(this);
28 | return this.library.returnBook(book);
29 | }
30 | }
31 |
32 | export default PublicLibraryVirtualProxy;
33 |
--------------------------------------------------------------------------------
/Proxy/2/scripts/main.js:
--------------------------------------------------------------------------------
1 | import PublicLibraryVirtualProxy from './PublicLibraryVirtualProxy';
2 |
3 | let oVirtualProxyLibrary = new PublicLibraryVirtualProxy();
4 | oVirtualProxyLibrary.findBooks('test');
5 |
--------------------------------------------------------------------------------
/Proxy/common/PublicLibrary.js:
--------------------------------------------------------------------------------
1 | class PublicLibrary {
2 | constructor(books) {
3 | this.catalog = {};
4 | this.setCatalogFromBooks(books);
5 | }
6 |
7 | setCatalogFromBooks(books) {
8 | books.forEach(book => {
9 | this.catalog[book.getIsbn()] = {
10 | book: book,
11 | available: true
12 | };
13 | });
14 | }
15 |
16 | findBooks(query) {
17 | console.log("Enter findBooks PublicLibrary");
18 | let results = [];
19 | for(let book of this.catalog) {
20 | if (query.match(book.getTitle()) || query.match(book.getAuthor())) {
21 | results.push(book);
22 | }
23 | }
24 | return results;
25 | }
26 |
27 | checkoutBook(book) {
28 | let isbn = book.getIsbn();
29 | book = this.catalog[isbn];
30 | if(book) {
31 | if(book.available) {
32 | book.available = false;
33 | return book;
34 | } else {
35 | throw new Error('PublicLibrary: book ' + book.getTitle() + ' is not currently available.');
36 | }
37 | } else {
38 | throw new Error('PublicLibrary: book ' + book.getTitle() + ' not found.');
39 | }
40 | }
41 |
42 | returnBook(book) {
43 | let isbn = book.getIsbn();
44 | book = this.catalog[isbn];
45 | if(book) {
46 | book.available = true;
47 | } else {
48 | throw new Error('PublicLibrary: book ' + book.getTitle() + ' not found.');
49 | }
50 | }
51 | }
52 |
53 | export default PublicLibrary;
54 |
--------------------------------------------------------------------------------
/Singleton/1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Singleton Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Singleton from './Singleton';
11 |
12 | var oSingle1 = Singleton;
13 | var oSingle2 = Singleton;
14 | console.log(Singleton.toString());
15 | console.log("oSingle1 is the same instance that oSingle2? " + (oSingle1 === oSingle2));
16 |
17 |
18 |
19 |
Console
20 |
21 |
SINGLETON
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Singleton/1/scripts/Singleton.js:
--------------------------------------------------------------------------------
1 | export default {
2 | toString: function() {
3 | return "[object Singleton]";
4 | }
5 | };
6 |
--------------------------------------------------------------------------------
/Singleton/1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Singleton from './Singleton';
2 |
3 | var oSingle1 = Singleton;
4 | var oSingle2 = Singleton;
5 | console.log(Singleton.toString());
6 | console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
7 |
--------------------------------------------------------------------------------
/Singleton/2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Singleton Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Singleton from './Singleton';
11 |
12 | var oSingle1 = new Singleton();
13 | var oSingle2 = new Singleton();
14 | console.log(new Singleton().toString());
15 | console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
16 |
17 |
18 |
19 |
Console
20 |
21 |
SINGLETON
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Singleton/2/scripts/Singleton.js:
--------------------------------------------------------------------------------
1 | let instance = null;
2 |
3 | class Singleton {
4 | static get instance() {
5 | return instance;
6 | }
7 |
8 | static set instance(_instance) {
9 | instance = _instance;
10 | }
11 |
12 | constructor() {
13 | if (Singleton.instance === null) {
14 | Singleton.instance = this;
15 | }
16 | return Singleton.instance;
17 | }
18 |
19 | toString() {
20 | return "[object Singleton]";
21 | }
22 |
23 | getInstance() {
24 | return new Singleton();
25 | }
26 | }
27 |
28 | export default Singleton;
29 |
--------------------------------------------------------------------------------
/Singleton/2/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Singleton from './Singleton';
2 |
3 | var oSingle1 = new Singleton();
4 | var oSingle2 = new Singleton();
5 | console.log(new Singleton().toString());
6 | console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
7 |
--------------------------------------------------------------------------------
/Singleton/3/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Singleton Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Singleton from './Singleton';
11 |
12 | var oSingle1 = Singleton;
13 | var oSingle2 = Singleton;
14 | console.log(Singleton.toString());
15 | console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
16 |
17 |
18 |
19 |
Console
20 |
21 |
SINGLETON
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Singleton/3/scripts/Singleton.js:
--------------------------------------------------------------------------------
1 | let Singleton = (function() {
2 | return {
3 | toString: function() {
4 | return "[object Singleton]";
5 | }
6 | };
7 | }());
8 |
9 | export default Singleton;
10 |
--------------------------------------------------------------------------------
/Singleton/3/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Singleton from './Singleton';
2 |
3 | var oSingle1 = Singleton;
4 | var oSingle2 = Singleton;
5 | console.log(Singleton.toString());
6 | console.log("oSingle1 is the same instance that oSingle2? " + (oSingle1 === oSingle2));
7 |
--------------------------------------------------------------------------------
/Singleton/4/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Singleton Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Singleton from './Singleton';
11 |
12 | var oSingle1 = Singleton.getInstance();
13 | var oSingle2 = Singleton.getInstance();
14 | console.log(Singleton.getInstance().toString());
15 | console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
16 |
17 |
18 |
19 |
Console
20 |
21 |
SINGLETON
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Singleton/4/scripts/Singleton.js:
--------------------------------------------------------------------------------
1 | export default function Singleton(instance) {
2 | if (!Singleton.getInstance) {
3 | Singleton.getInstance = function() {
4 | return instance;
5 | };
6 | instance = new Singleton;
7 | }
8 | this.toString = function() {
9 | return "[object Singleton]";
10 | };
11 | }(new Singleton);
12 |
--------------------------------------------------------------------------------
/Singleton/4/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Singleton from './Singleton';
2 |
3 | var oSingle1 = Singleton.getInstance();
4 | var oSingle2 = Singleton.getInstance();
5 | console.log(Singleton.getInstance().toString());
6 | console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
7 |
--------------------------------------------------------------------------------
/Singleton/5/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Singleton Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import DatabaseConnection from './DatabaseConnection';
11 |
12 | var oSingle1 = DatabaseConnection.instance;
13 | var oSingle2 = DatabaseConnection.instance;
14 | console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
15 |
16 |
17 |
18 |
Console
19 |
20 |
SINGLETON
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/Singleton/5/scripts/DatabaseConnection.js:
--------------------------------------------------------------------------------
1 | let DBCInstance = null;
2 | global.DatabaseConnection = class DatabaseConnection {
3 | get url() {
4 | return 'mongodb://localhost:27017/myproject';
5 | }
6 | get username() {
7 | return 'admin';
8 | }
9 | get connection() {
10 | let connection;
11 | // Do something to get the connection to the DB.
12 | return connection;
13 | }
14 | get password() {
15 | return 'localhost';
16 | }
17 | static get instance() {
18 | if(DBCInstance === null ||
19 | DBCInstance.getConnection().isClosed()) {
20 | DBCInstance = new DatabaseConnection();
21 | }
22 | return DBCInstance;
23 | }
24 | }
--------------------------------------------------------------------------------
/Singleton/5/scripts/main.js:
--------------------------------------------------------------------------------
1 | import DatabaseConnection from './DatabaseConnection';
2 |
3 | var oSingle1 = DatabaseConnection.instance;
4 | var oSingle2 = DatabaseConnection.instance;
5 | console.log("Are oSingle1 and oSingle2 the same instance? " + (oSingle1 === oSingle2));
6 |
--------------------------------------------------------------------------------
/State/1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | State Pattern
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
Source
13 |
14 | import Download from './Download';
15 |
16 | var oDownload = new Download();
17 |
18 | $("#download_button").click(function() {
19 | oDownload.download();
20 | });
21 | $("#pause_button").click(function() {
22 | oDownload.pause();
23 | });
24 | $("#resume_button").click(function() {
25 | oDownload.download();
26 | });
27 |
28 |
29 |
30 |
Console
31 |
32 |
STATE
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/State/1/scripts/Download.js:
--------------------------------------------------------------------------------
1 | import ReadyState from './states/ReadyState';
2 | import DownloadingState from './states/DownloadingState';
3 | import DownloadPausedState from './states/DownloadPausedState';
4 | import DownloadedState from './states/DownloadedState';
5 | import DownloadFailedState from './states/DownloadFailedState';
6 |
7 | class Download {
8 | constructor() {
9 | this.state = new ReadyState(this);
10 | }
11 |
12 | setState(state) {
13 | this.state = state;
14 | }
15 |
16 | download() {
17 | this.state.download();
18 | }
19 |
20 | pause() {
21 | this.state.pause();
22 | }
23 |
24 | fail() {
25 | this.state.fail();
26 | }
27 |
28 | finish() {
29 | this.state.finish();
30 | }
31 |
32 | getReadyState() {
33 | return new ReadyState(this);
34 | }
35 |
36 | getDownloadingState() {
37 | return new DownloadingState(this);
38 | }
39 |
40 | getDownloadPausedState() {
41 | return new DownloadPausedState(this);
42 | }
43 |
44 | getDownloadedState() {
45 | return new DownloadedState(this);
46 | }
47 |
48 | getDownloadedFailedState() {
49 | return new DownloadFailedState(this);
50 | }
51 | }
52 |
53 | export default Download;
54 |
--------------------------------------------------------------------------------
/State/1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Download from './Download';
2 |
3 | var oDownload = new Download();
4 |
5 | $("#download_button").click(function() {
6 | oDownload.download();
7 | });
8 | $("#pause_button").click(function() {
9 | oDownload.pause();
10 | });
11 | $("#resume_button").click(function() {
12 | oDownload.download();
13 | });
14 |
--------------------------------------------------------------------------------
/State/1/scripts/states/DownloadFailedState.js:
--------------------------------------------------------------------------------
1 | import State from './State';
2 |
3 | class DownloadFailedState extends State {
4 | constructor(download) {
5 | super();
6 | this._download = download;
7 | }
8 |
9 | download() {
10 | this._download.setState(this._download.getDownloadingState());
11 | console.log("Try to Download again!");
12 | }
13 |
14 | pause() {
15 | throw new Error("You can't pause a failed download!");
16 | }
17 |
18 | fail() {
19 | throw new Error("A failed download can't fail itself!");
20 | }
21 |
22 | finish() {
23 | throw new Error("A failed download is not finished!");
24 | }
25 | }
26 |
27 | export default DownloadFailedState;
28 |
--------------------------------------------------------------------------------
/State/1/scripts/states/DownloadPausedState.js:
--------------------------------------------------------------------------------
1 | import State from './State';
2 |
3 | class DownloadPausedState extends State {
4 | constructor(download) {
5 | super();
6 | this._download = download;
7 | }
8 |
9 | download() {
10 | this._download.setState(this._download.getDownloadingState());
11 | console.log("Continue Download!");
12 | }
13 |
14 | pause() {
15 | throw new Error("You can't pause a paused download!");
16 | }
17 |
18 | fail() {
19 | this._download.setState(this._download.getDownloadedFailedState());
20 | console.log("Download has failed!");
21 | }
22 |
23 | finish() {
24 | this._download.setState(this._download.getDownloadedState());
25 | console.log("Download has finished!");
26 | }
27 | }
28 |
29 | export default DownloadPausedState;
30 |
--------------------------------------------------------------------------------
/State/1/scripts/states/DownloadedState.js:
--------------------------------------------------------------------------------
1 | import State from './State';
2 |
3 | class DownloadedState extends State {
4 | constructor(download) {
5 | super();
6 | this._download = download;
7 | }
8 |
9 | download() {
10 | this._download.setState(this._download.getDownloadingState());
11 | console.log("Download again!");
12 | }
13 |
14 | pause() {
15 | throw new Error("You can't pause a downloaded file!");
16 | }
17 |
18 | fail() {
19 | throw new Error("A downloaded file can't fail!");
20 | }
21 |
22 | finish() {
23 | throw new Error("A downloaded file can't finish itself!");
24 | }
25 | }
26 |
27 | export default DownloadedState;
28 |
--------------------------------------------------------------------------------
/State/1/scripts/states/DownloadingState.js:
--------------------------------------------------------------------------------
1 | import State from './State';
2 |
3 | class DownloadingState extends State {
4 | constructor(download) {
5 | super();
6 | this._download = download;
7 | }
8 |
9 | download() {
10 | throw new Error("You can't download a file that is being downloaded already!");
11 | }
12 |
13 | pause() {
14 | this._download.setState(this._download.getDownloadPausedState());
15 | console.log("Pause download!");
16 | }
17 |
18 | fail() {
19 | this._download.setState(this._download.getDownloadedFailedState());
20 | console.log("Download has failed!");
21 | }
22 |
23 | finish() {
24 | this._download.setState(this._download.getDownloadedState());
25 | console.log("Download has finished!");
26 | }
27 | }
28 |
29 | export default DownloadingState;
30 |
--------------------------------------------------------------------------------
/State/1/scripts/states/ReadyState.js:
--------------------------------------------------------------------------------
1 | import State from './State';
2 |
3 | class ReadyState extends State {
4 | constructor(download) {
5 | super();
6 | this._download = download;
7 | }
8 |
9 | download() {
10 | this._download.setState(this._download.getDownloadingState());
11 | console.log("Start Download!");
12 | }
13 |
14 | pause() {
15 | throw new Error("You can't pause a not started download!");
16 | }
17 |
18 | fail() {
19 | throw new Error("A download can't file if is not started!");
20 | }
21 |
22 | finish() {
23 | throw new Error("A download can't finish if is not started!");
24 | }
25 | }
26 |
27 | export default ReadyState;
28 |
--------------------------------------------------------------------------------
/State/1/scripts/states/State.js:
--------------------------------------------------------------------------------
1 | class State {
2 | download() {
3 | throw new Error("This method must be overwritten!");
4 | }
5 |
6 | pause() {
7 | throw new Error("This method must be overwritten!");
8 | }
9 |
10 | fail() {
11 | throw new Error("This method must be overwritten!");
12 | }
13 |
14 | finish() {
15 | throw new Error("This method must be overwritten!");
16 | }
17 |
18 | }
19 |
20 | export default State;
21 |
--------------------------------------------------------------------------------
/State/2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | State Pattern
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
Source
13 |
14 | import GumballMachine from './GumballMachine';
15 |
16 | var oGumballMachine = new GumballMachine();
17 |
18 | $("#insert_quarter_button").click(function() {
19 | oGumballMachine.insertQuarter();
20 | });
21 | $("#release_quarter_button").click(function() {
22 | oGumballMachine.ejectQuarter();
23 | });
24 | $("#turn_crank_button").click(function() {
25 | oGumballMachine.turnCrank();
26 | oGumballMachine.dispense();
27 | });
28 |
29 |
30 |
31 |
Console
32 |
33 |
STATE
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/State/2/scripts/GumballMachine.js:
--------------------------------------------------------------------------------
1 | import NoQuarterState from './states/NoQuarterState';
2 | import HasQuarterState from './states/HasQuarterState';
3 | import SoldOutState from './states/SoldOutState';
4 | import SoldState from './states/SoldState';
5 | import WinnerState from './states/WinnerState';
6 |
7 | class GumballMachine {
8 | constructor() {
9 | this.state = new NoQuarterState(this);
10 | this.gumballs = 10;
11 | }
12 |
13 | insertQuarter() {
14 | this.state.insertQuarter();
15 | }
16 |
17 | ejectQuarter() {
18 | this.state.ejectQuarter();
19 | }
20 |
21 | turnCrank() {
22 | this.state.turnCrank();
23 | }
24 |
25 | dispense() {
26 | this.state.dispense();
27 | }
28 |
29 | getCount() {
30 | return this.gumballs;
31 | }
32 |
33 | releaseBall() {
34 | this.state.releaseBall();
35 | }
36 |
37 | setState(state) {
38 | this.state = state;
39 | }
40 |
41 | getNoQuarterState() {
42 | return new NoQuarterState(this);
43 | }
44 |
45 | getHasQuarterState() {
46 | return new HasQuarterState(this);
47 | }
48 |
49 | getSoldOutState() {
50 | return new SoldOutState(this);
51 | }
52 |
53 | getSoldState() {
54 | return new SoldState(this);
55 | }
56 |
57 | getWinnerState() {
58 | return new WinnerState(this);
59 | }
60 | }
61 |
62 | export default GumballMachine;
63 |
--------------------------------------------------------------------------------
/State/2/scripts/main.js:
--------------------------------------------------------------------------------
1 | import GumballMachine from './GumballMachine';
2 |
3 | var oGumballMachine = new GumballMachine();
4 |
5 | $("#insert_quarter_button").click(function() {
6 | oGumballMachine.insertQuarter();
7 | });
8 | $("#release_quarter_button").click(function() {
9 | oGumballMachine.ejectQuarter();
10 | });
11 | $("#turn_crank_button").click(function() {
12 | oGumballMachine.turnCrank();
13 | oGumballMachine.dispense();
14 | });
15 |
--------------------------------------------------------------------------------
/State/2/scripts/states/HasQuarterState.js:
--------------------------------------------------------------------------------
1 | import State from './State';
2 |
3 | class HasQuarterState extends State {
4 | constructor(gumballMachine) {
5 | super(gumballMachine);
6 | this.randomWinner = Math.random();
7 | }
8 |
9 | insertQuarter() {
10 | console.log('You can not insert another quarter!');
11 | }
12 |
13 | ejectQuarter() {
14 | console.log('Quarter returned!');
15 | this.gumballMachine.setState(this.gumballMachine.getNoQuarterState());
16 | }
17 |
18 | turnCrank() {
19 | console.log("You turned...");
20 | if (this.randomWinner < 0.20) {
21 | console.log("Winner state");
22 | this.gumballMachine.setState(this.gumballMachine.getWinnerState());
23 | } else {
24 | console.log("Sold state");
25 | this.gumballMachine.setState(this.gumballMachine.getSoldState());
26 | }
27 | }
28 |
29 | dispense() {
30 | console.log("No gumball dispensed!");
31 | }
32 | }
33 |
34 | export default HasQuarterState;
35 |
--------------------------------------------------------------------------------
/State/2/scripts/states/NoQuarterState.js:
--------------------------------------------------------------------------------
1 | import State from './State';
2 |
3 | class NoQuarterState extends State {
4 | insertQuarter() {
5 | console.log("You inserted a quarter!");
6 | this.gumballMachine.setState(this.gumballMachine.getHasQuarterState());
7 | }
8 |
9 | ejectQuarter() {
10 | console.log("You haven't inserted a quarter!");
11 | }
12 |
13 | turnCrank() {
14 | console.log("You turned, but there is no quarter");
15 | }
16 |
17 | dispense() {
18 | console.log("You must to pay first!");
19 | }
20 | }
21 |
22 | export default NoQuarterState;
23 |
--------------------------------------------------------------------------------
/State/2/scripts/states/SoldOutState.js:
--------------------------------------------------------------------------------
1 | import State from './State';
2 |
3 | class SoldOutState extends State {
4 | insertQuarter() {
5 | console.log("You inserted a quarter!");
6 | this.gumballMachine.setState(this.gumballMachine.getHasQuarterState());
7 | }
8 |
9 | ejectQuarter() {
10 | console.log("You haven't inserted a quarter!");
11 | }
12 |
13 | turnCrank() {
14 | console.log("You turned, but there is no quarter");
15 | }
16 |
17 | dispense() {
18 | console.log("You must to pay first!");
19 | }
20 | }
21 |
22 | export default SoldOutState;
23 |
--------------------------------------------------------------------------------
/State/2/scripts/states/SoldState.js:
--------------------------------------------------------------------------------
1 | import State from './State';
2 |
3 | class SoldState extends State {
4 | insertQuarter() {
5 | console.log("Please wait, we're already giving you a gumball!");
6 | }
7 |
8 | ejectQuarter() {
9 | console.log("Sorry, you already turned the crank!");
10 | }
11 |
12 | turnCrank() {
13 | console.log("Turning twice doesn't get you another gumball!");
14 | }
15 |
16 | releaseBall() {
17 | console.log('Gum is being released.');
18 | this.gumballMachine.gumballs--;
19 | }
20 |
21 | dispense() {
22 | if (this.gumballMachine.getCount() > 0) {
23 | this.gumballMachine.releaseBall();
24 | this.gumballMachine.setState(this.gumballMachine.getNoQuarterState());
25 | } else {
26 | console.log("Oops!, out of gumballs!");
27 | this.gumballMachine.setState(this.gumballMachine.getSoldOutState());
28 | }
29 | }
30 | }
31 |
32 | export default SoldState;
33 |
--------------------------------------------------------------------------------
/State/2/scripts/states/State.js:
--------------------------------------------------------------------------------
1 | class State {
2 | constructor(gumballMachine) {
3 | this.gumballMachine = gumballMachine;
4 | }
5 |
6 | insertQuarter() {
7 | throw new Error("This method must be overwritten!");
8 | }
9 |
10 | releaseBall() {
11 | throw new Error("This method must be overwritten!");
12 | }
13 |
14 | ejectQuarter() {
15 | throw new Error("This method must be overwritten!");
16 | }
17 |
18 | turnCrank() {
19 | throw new Error("This method must be overwritten!");
20 | }
21 |
22 | dispense() {
23 | throw new Error("This method must be overwritten!");
24 | }
25 | }
26 |
27 | export default State;
28 |
--------------------------------------------------------------------------------
/State/2/scripts/states/WinnerState.js:
--------------------------------------------------------------------------------
1 | import State from './State';
2 |
3 | class WinnerState extends State {
4 | insertQuarter() {
5 | console.log("Please wait, we're already giving you a gumball!");
6 | }
7 |
8 | ejectQuarter() {
9 | console.log("Sorry, you already turned the crank!");
10 | }
11 |
12 | turnCrank() {
13 | console.log("Turning twice doesn't get you another gumball!");
14 | }
15 |
16 | releaseBall() {
17 | console.log('Gum is being released.');
18 | this.gumballMachine.gumballs--;
19 | }
20 |
21 | dispense() {
22 | console.log("You're a Winner! You get two gumball for your quarter!");
23 | if (this.gumballMachine.getCount() > 0) {
24 | this.gumballMachine.releaseBall();
25 | this.gumballMachine.releaseBall();
26 | this.gumballMachine.setState(this.gumballMachine.getNoQuarterState());
27 | } else {
28 | console.log("Oops!, out of gumballs!");
29 | this.gumballMachine.setState(this.gumballMachine.getSoldOutState());
30 | }
31 | }
32 | }
33 |
34 | export default WinnerState;
35 |
--------------------------------------------------------------------------------
/Strategy/1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Try-Finally Pattern
5 |
21 |
22 |
23 |
24 |
Source
25 |
26 | import MallardDuck from '../../common/MallardDuck';
27 | import RedheadDuck from '../../common/RedheadDuck';
28 |
29 | var mallard = new MallardDuck();
30 | var redhead = new RedheadDuck();
31 | mallard.quack();
32 | mallard.swim();
33 | mallard.display();
34 |
35 | redhead.quack();
36 | redhead.swim();
37 | redhead.display();
38 |
39 |
40 |
41 |
Console
42 |
43 |
Try-Finally
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/Strategy/1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import MallardDuck from '../../common/MallardDuck';
2 | import RedheadDuck from '../../common/RedheadDuck';
3 |
4 | var mallard = new MallardDuck();
5 | var redhead = new RedheadDuck();
6 | mallard.quack();
7 | mallard.swim();
8 | mallard.display();
9 |
10 | redhead.quack();
11 | redhead.swim();
12 | redhead.display();
13 |
--------------------------------------------------------------------------------
/Strategy/2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Try-Finally Pattern
5 |
21 |
22 |
23 |
24 |
Source
25 |
26 | import MallardDuck from '../../common/MallardDuck';
27 | import RedheadDuck from '../../common/RedheadDuck';
28 | import RubberDuck from '../../common/RubberDuck';
29 |
30 | var mallard = new MallardDuck();
31 | var redhead = new RedheadDuck();
32 | var rubber = new RubberDuck();
33 |
34 | mallard.quack();
35 | mallard.swim();
36 | mallard.fly();
37 | mallard.display();
38 |
39 | redhead.quack();
40 | redhead.swim();
41 | redhead.fly();
42 | redhead.display();
43 |
44 | rubber.quack();
45 | rubber.swim();
46 | rubber.fly();
47 | rubber.display();
48 |
49 |
50 |
51 |
Console
52 |
53 |
Try-Finally
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/Strategy/2/scripts/main.js:
--------------------------------------------------------------------------------
1 | import MallardDuck from '../../common/MallardDuck';
2 | import RedheadDuck from '../../common/RedheadDuck';
3 | import RubberDuck from '../../common/RubberDuck';
4 |
5 | var mallard = new MallardDuck();
6 | var redhead = new RedheadDuck();
7 | var rubber = new RubberDuck();
8 |
9 | mallard.quack();
10 | mallard.swim();
11 | mallard.fly();
12 | mallard.display();
13 |
14 | redhead.quack();
15 | redhead.swim();
16 | redhead.fly();
17 | redhead.display();
18 |
19 | rubber.quack();
20 | rubber.swim();
21 | rubber.fly();
22 | rubber.display();
23 |
--------------------------------------------------------------------------------
/Strategy/2_1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Try-Finally Pattern
5 |
21 |
22 |
23 |
24 |
Source
25 |
26 | import MallardDuck from '../../common/MallardDuck';
27 | import RedheadDuck from '../../common/RedheadDuck';
28 | import RubberDuck from '../../common/RubberDuck';
29 | import DecoyDuck from './DecoyDuck';
30 |
31 | var mallard = new MallardDuck();
32 | var redhead = new RedheadDuck();
33 | var rubber = new RubberDuck();
34 | var decoy = new DecoyDuck();
35 |
36 | mallard.quack();
37 | mallard.swim();
38 | mallard.fly();
39 | mallard.display();
40 |
41 | redhead.quack();
42 | redhead.swim();
43 | redhead.fly();
44 | redhead.display();
45 |
46 | rubber.quack();
47 | rubber.swim();
48 | rubber.fly();
49 | rubber.display();
50 |
51 | decoy.quack();
52 | decoy.swim();
53 | decoy.fly();
54 | decoy.display();
55 |
56 |
57 |
58 |
Console
59 |
60 |
Try-Finally
61 |
62 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/Strategy/2_1/scripts/DecoyDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from '../../common/Duck';
2 |
3 | class DecoyDuck extends Duck {
4 | quack() {
5 |
6 | }
7 |
8 | fly() {
9 |
10 | }
11 |
12 | display() {
13 | console.log("DecoyDuck show");
14 | }
15 | }
16 |
17 | export default DecoyDuck;
18 |
--------------------------------------------------------------------------------
/Strategy/2_1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import MallardDuck from '../../common/MallardDuck';
2 | import RedheadDuck from '../../common/RedheadDuck';
3 | import RubberDuck from '../../common/RubberDuck';
4 | import DecoyDuck from './DecoyDuck';
5 |
6 | var mallard = new MallardDuck();
7 | var redhead = new RedheadDuck();
8 | var rubber = new RubberDuck();
9 | var decoy = new DecoyDuck();
10 |
11 | mallard.quack();
12 | mallard.swim();
13 | mallard.fly();
14 | mallard.display();
15 |
16 | redhead.quack();
17 | redhead.swim();
18 | redhead.fly();
19 | redhead.display();
20 |
21 | rubber.quack();
22 | rubber.swim();
23 | rubber.fly();
24 | rubber.display();
25 |
26 | decoy.quack();
27 | decoy.swim();
28 | decoy.fly();
29 | decoy.display();
30 |
--------------------------------------------------------------------------------
/Strategy/3/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Try-Finally Pattern
5 |
21 |
22 |
23 |
24 |
Source
25 |
26 | import MallardDuck from '../../common/MallardDuck';
27 | import RedheadDuck from '../../common/RedheadDuck';
28 | import RubberDuck from '../../common/RubberDuck';
29 | import DecoyDuck from './DecoyDuck';
30 |
31 | var mallard = new MallardDuck();
32 | var redhead = new RedheadDuck();
33 | var rubber = new RubberDuck();
34 | var decoy = new DecoyDuck();
35 |
36 | mallard.quack();
37 | mallard.swim();
38 | mallard.fly();
39 | mallard.display();
40 |
41 | redhead.quack();
42 | redhead.swim();
43 | redhead.fly();
44 | redhead.display();
45 |
46 | rubber.quack();
47 | rubber.swim();
48 | rubber.display();
49 |
50 | decoy.display();
51 |
52 |
53 |
54 |
Console
55 |
56 |
Try-Finally
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/Strategy/3/scripts/DecoyDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 |
3 | class DecoyDuck extends Duck {
4 | display() {
5 | console.log("DecoyDuck show");
6 | }
7 | }
8 |
9 | export default DecoyDuck;
10 |
--------------------------------------------------------------------------------
/Strategy/3/scripts/Duck.js:
--------------------------------------------------------------------------------
1 | class Duck {
2 | swim() {
3 | console.log('Chop!');
4 | }
5 | display() {
6 | throw new Error("This method must be overwritten!");
7 | }
8 | }
9 |
10 | export default Duck;
11 |
--------------------------------------------------------------------------------
/Strategy/3/scripts/Flyable.js:
--------------------------------------------------------------------------------
1 | const Flyable = Sup => class extends Sup {
2 | fly() {
3 | console.log('Wings!');
4 | }
5 | };
6 |
7 | export default Flyable;
8 |
--------------------------------------------------------------------------------
/Strategy/3/scripts/MallardDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 | import Quackable from './Quackable';
3 |
4 | class MallardDuck extends Duck(Quackable(null)) {
5 | display() {
6 | console.log('MallardDuck show');
7 | }
8 | }
9 |
10 | export default MallardDuck;
11 |
--------------------------------------------------------------------------------
/Strategy/3/scripts/Quackable.js:
--------------------------------------------------------------------------------
1 | const Quackable = Sup => class extends Sup {
2 | quack() {
3 | console.log('Quack!');
4 | }
5 | };
6 |
7 | export default Quackable;
8 |
--------------------------------------------------------------------------------
/Strategy/3/scripts/RedheadDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 | import Quackable from './Quackable';
3 | import Flyable from './Flyable';
4 |
5 | class RedheadDuck extends Duck(Quackable(Flyable(null))) {
6 | display() {
7 | console.log('RedheadDuck show');
8 | }
9 | }
10 |
11 | export default RedheadDuck;
12 |
--------------------------------------------------------------------------------
/Strategy/3/scripts/RubberDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 | import Quackable from './Quackable';
3 |
4 | class RubberDuck extends Duck(Quackable(null)) {
5 | display() {
6 | console.log('RubberDuck show')
7 | }
8 | }
9 |
10 | export default RubberDuck;
11 |
--------------------------------------------------------------------------------
/Strategy/3/scripts/main.js:
--------------------------------------------------------------------------------
1 | import MallardDuck from '../../common/MallardDuck';
2 | import RedheadDuck from '../../common/RedheadDuck';
3 | import RubberDuck from '../../common/RubberDuck';
4 | import DecoyDuck from './DecoyDuck';
5 |
6 | var mallard = new MallardDuck();
7 | var redhead = new RedheadDuck();
8 | var rubber = new RubberDuck();
9 | var decoy = new DecoyDuck();
10 |
11 | mallard.quack();
12 | mallard.swim();
13 | mallard.fly();
14 | mallard.display();
15 |
16 | redhead.quack();
17 | redhead.swim();
18 | redhead.fly();
19 | redhead.display();
20 |
21 | rubber.quack();
22 | rubber.swim();
23 | rubber.display();
24 |
25 | decoy.display();
26 |
--------------------------------------------------------------------------------
/Strategy/4/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Try-Finally Pattern
5 |
21 |
22 |
23 |
24 |
Source
25 |
26 | import MallardDuck from './MallardDuck';
27 | import RedheadDuck from './RedheadDuck';
28 | import RubberDuck from './RubberDuck';
29 | import DecoyDuck from './DecoyDuck';
30 |
31 | var mallard = new MallardDuck();
32 | var redhead = new RedheadDuck();
33 | var rubber = new RubberDuck();
34 | var decoy = new DecoyDuck();
35 |
36 | mallard.quack();
37 | mallard.swim();
38 | mallard.fly();
39 | mallard.display();
40 |
41 | redhead.quack();
42 | redhead.swim();
43 | redhead.fly();
44 | redhead.display();
45 |
46 | rubber.quack();
47 | rubber.swim();
48 | rubber.fly();
49 | rubber.display();
50 |
51 | decoy.quack();
52 | decoy.swim();
53 | decoy.fly();
54 | decoy.display();
55 |
56 |
57 |
58 |
Console
59 |
60 |
Try-Finally
61 |
62 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/DecoyDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 | import FlyNoWay from './FlyNoWay';
3 | import MuteQuack from './MuteQuack';
4 |
5 | class DecoyDuck extends Duck {
6 | constructor() {
7 | super();
8 | this.flyBehavior = new FlyNoWay();
9 | this.quackBehavior = new MuteQuack();
10 | }
11 |
12 | display() {
13 | console.log('DecoyDuck show');
14 | }
15 | }
16 |
17 | export default DecoyDuck;
18 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/Duck.js:
--------------------------------------------------------------------------------
1 | class Duck {
2 | constructor() {
3 | this.flyBehavior = null;
4 | this.quackBehavior = null;
5 | }
6 |
7 | setFlyBehavior(flyBehavior) {
8 | this.flyBehavior = flyBehavior;
9 | }
10 |
11 | setQuackBehavior(quackBehavior) {
12 | this.quackBehavior = quackBehavior;
13 | }
14 |
15 | fly() {
16 | this.flyBehavior.fly();
17 | }
18 |
19 | quack() {
20 | this.quackBehavior.quack();
21 | }
22 |
23 | swim() {
24 | console.log('Chop!');
25 | }
26 |
27 | display() {
28 | throw new Error("This method must be overwritten!");
29 | }
30 | }
31 |
32 | export default Duck;
33 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/FlyBehavior.js:
--------------------------------------------------------------------------------
1 | class FlyBehavior {
2 | fly() {
3 | throw new Error("This method must be overwritten");
4 | }
5 | }
6 |
7 | export default FlyBehavior;
8 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/FlyNoWay.js:
--------------------------------------------------------------------------------
1 | class FlyNoWay {
2 | fly() {
3 | //Don't do nothing.
4 | }
5 | }
6 |
7 | export default FlyNoWay;
8 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/FlyWithWings.js:
--------------------------------------------------------------------------------
1 | import FlyBehavior from './FlyBehavior';
2 |
3 | class FlyWithWings extends FlyBehavior {
4 | fly() {
5 | console.log('Flap!Flap!');
6 | }
7 | }
8 |
9 | export default FlyWithWings;
10 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/MallardDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 | import FlyWithWings from './FlyWithWings';
3 | import QuackBehavior from './QuackBehavior';
4 |
5 | class MallardDuck extends Duck {
6 | constructor() {
7 | super();
8 | this.flyBehavior = new FlyWithWings();
9 | this.quackBehavior = new QuackBehavior();
10 | }
11 |
12 | display() {
13 | console.log('MallardDuck show');
14 | }
15 | }
16 |
17 | export default MallardDuck;
18 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/MuteQuack.js:
--------------------------------------------------------------------------------
1 | import QuackBehavior from './QuackBehavior';
2 |
3 | class MuteQuack extends QuackBehavior {
4 | quack() {
5 | //Don't do nothing!
6 | }
7 | }
8 |
9 | export default MuteQuack;
10 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/Quack.js:
--------------------------------------------------------------------------------
1 | import QuackBehavior from './QuackBehavior';
2 |
3 | class Quack extends QuackBehavior {
4 | quack() {
5 | console.log('Quack!');
6 | }
7 | }
8 |
9 | export default Quack;
10 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/QuackBehavior.js:
--------------------------------------------------------------------------------
1 | class QuackBehavior {
2 | quack() {
3 | console.log('Quack!');
4 | }
5 | }
6 |
7 | export default QuackBehavior;
8 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/RedheadDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 | import FlyWithWings from './FlyWithWings';
3 | import Quack from './Quack';
4 |
5 | class RedheadDuck extends Duck {
6 | constructor() {
7 | super();
8 | this.flyBehavior = new FlyWithWings();
9 | this.quackBehavior = new Quack();
10 | }
11 |
12 | display() {
13 | console.log("RedheadDuck show");
14 | }
15 | }
16 |
17 | export default RedheadDuck;
18 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/RubberDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 | import FlyNoWay from './FlyNoWay';
3 | import Squeak from './Squeak';
4 |
5 | class RubberDuck extends Duck {
6 | constructor() {
7 | super();
8 | this.flyBehavior = new FlyNoWay();
9 | this.quackBehavior = new Squeak();
10 | }
11 |
12 | display() {
13 | console.log("RubberDuck show");
14 | }
15 | }
16 |
17 | export default RubberDuck;
18 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/Squeak.js:
--------------------------------------------------------------------------------
1 | import QuackBehavior from './QuackBehavior';
2 |
3 | class Squeak extends QuackBehavior {
4 | quack() {
5 | console.log('Squeeze!');
6 | }
7 | }
8 |
9 | export default Squeak;
10 |
--------------------------------------------------------------------------------
/Strategy/4/scripts/main.js:
--------------------------------------------------------------------------------
1 | import MallardDuck from './MallardDuck';
2 | import RedheadDuck from './RedheadDuck';
3 | import RubberDuck from './RubberDuck';
4 | import DecoyDuck from './DecoyDuck';
5 |
6 | var mallard = new MallardDuck();
7 | var redhead = new RedheadDuck();
8 | var rubber = new RubberDuck();
9 | var decoy = new DecoyDuck();
10 |
11 | mallard.quack();
12 | mallard.swim();
13 | mallard.fly();
14 | mallard.display();
15 |
16 | redhead.quack();
17 | redhead.swim();
18 | redhead.fly();
19 | redhead.display();
20 |
21 | rubber.quack();
22 | rubber.swim();
23 | rubber.fly();
24 | rubber.display();
25 |
26 | decoy.quack();
27 | decoy.swim();
28 | decoy.fly();
29 | decoy.display();
30 |
--------------------------------------------------------------------------------
/Strategy/common/Duck.js:
--------------------------------------------------------------------------------
1 | class Duck {
2 | quack() {
3 | console.log('Quack!');
4 | }
5 |
6 | swim() {
7 | console.log('Chop!');
8 | }
9 |
10 | fly() {
11 | console.log('Wings!');
12 | }
13 |
14 | display() {
15 | throw new Error("This method must be overwritten!");
16 | }
17 |
18 | }
19 |
20 | export default Duck;
21 |
--------------------------------------------------------------------------------
/Strategy/common/MallardDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 |
3 | class MallardDuck extends Duck {
4 | display() {
5 | console.log('MallardDuck show');
6 | }
7 | }
8 |
9 | export default MallardDuck;
10 |
--------------------------------------------------------------------------------
/Strategy/common/RedheadDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 |
3 | class RedheadDuck extends Duck {
4 | display() {
5 | console.log("RedheadDuck show");
6 | }
7 | }
8 |
9 | export default RedheadDuck;
10 |
--------------------------------------------------------------------------------
/Strategy/common/RubberDuck.js:
--------------------------------------------------------------------------------
1 | import Duck from './Duck';
2 |
3 | class RubberDuck extends Duck {
4 | quack() {
5 | console.log('Squeak!');
6 | }
7 |
8 | fly() {
9 | // Don't do anything;
10 | }
11 |
12 | display() {
13 | console.log('RubbberDuck show');
14 | }
15 | }
16 |
17 | export default RubberDuck;
18 |
--------------------------------------------------------------------------------
/Template/1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Template Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Coffee from './Coffee';
11 | import Tea from './Tea';
12 |
13 | let oCoffee = new Coffee();
14 | oCoffee.prepareRecipe();
15 | console.log("*********************************************************");
16 | let oTea = new Tea();
17 | oTea.prepareRecipe();
18 |
19 |
20 |
21 |
Console
22 |
23 |
TEMPLATE
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/Template/1/scripts/CaffeineBeverage.js:
--------------------------------------------------------------------------------
1 | class CaffeineBeverage {
2 | prepareRecipe() {
3 | this.boilWater();
4 | this.brew();
5 | this.pourOnCup();
6 | if (this.customerWantsCondiments()) {
7 | this.addCondiments();
8 | }
9 | }
10 |
11 | boilWater() {
12 | console.log("Put water on fire until the water starts boiling!");
13 | }
14 |
15 | pourOnCup() {
16 | console.log("Put beverage on Cup!");
17 | }
18 |
19 | brew() {
20 | throw new Error("This method mus be overwritten!");
21 | }
22 |
23 | addCondiments() {
24 | throw new Error("This method mus be overwritten!");
25 | }
26 |
27 | customerWantsCondiments() {
28 | return true;
29 | }
30 | }
31 |
32 | export default CaffeineBeverage;
33 |
--------------------------------------------------------------------------------
/Template/1/scripts/Coffee.js:
--------------------------------------------------------------------------------
1 | import CaffeineBeverage from './CaffeineBeverage';
2 |
3 | class Coffee extends CaffeineBeverage {
4 | brew() {
5 | console.log("Dripping Coffee through filter!");
6 | }
7 |
8 | addCondiments() {
9 | console.log("Add Sugar and Milk!");
10 | }
11 |
12 | customerWantsCondiments() {
13 | return confirm("Do you want sugar and milk?");
14 | }
15 | }
16 |
17 | export default Coffee;
18 |
--------------------------------------------------------------------------------
/Template/1/scripts/Tea.js:
--------------------------------------------------------------------------------
1 | import CaffeineBeverage from './CaffeineBeverage';
2 |
3 | class Tea extends CaffeineBeverage {
4 | brew() {
5 | console.log("Steeping the tea!");
6 | }
7 |
8 | addCondiments() {
9 | console.log("Adding lemon!");
10 | }
11 |
12 | customerWantsCondiments() {
13 | return confirm("Do you want some lemon?");
14 | }
15 | }
16 |
17 | export default Tea;
18 |
--------------------------------------------------------------------------------
/Template/1/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Coffee from './Coffee';
2 | import Tea from './Tea';
3 |
4 | let oCoffee = new Coffee();
5 | oCoffee.prepareRecipe();
6 | console.log("*********************************************************");
7 | let oTea = new Tea();
8 | oTea.prepareRecipe();
9 |
--------------------------------------------------------------------------------
/Template/2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Template Pattern
5 |
6 |
7 |
8 |
Source
9 |
10 | import Coffee from './Coffee';
11 | import Tea from './Tea';
12 |
13 | var oCoffee = new Coffee();
14 | oCoffee.prepareRecipe();
15 | console.log("*********************************************************");
16 | var oTea = new Tea();
17 | oTea.prepareRecipe();
18 |
19 |
20 |
21 |
Console
22 |
23 |
TEMPLATE
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/Template/2/scripts/CaffeineBeverage.js:
--------------------------------------------------------------------------------
1 | class CaffeineBeverage {
2 | prepareRecipe() {
3 | this.boilWater();
4 | this.brew();
5 | this.pourOnCup();
6 | this.addCondiments();
7 | }
8 |
9 | boilWater() {
10 | console.log("Put water on fire until the water starts boiling!");
11 | }
12 |
13 | pourOnCup() {
14 | console.log("Put beverage on Cup!");
15 | }
16 |
17 | brew() {
18 | throw new Error("This method must be overwritten!");
19 | }
20 |
21 | addCondiments() {
22 | throw new Error("This method must be overwritten!");
23 | }
24 | }
25 |
26 | export default CaffeineBeverage;
27 |
--------------------------------------------------------------------------------
/Template/2/scripts/Coffee.js:
--------------------------------------------------------------------------------
1 | import CaffeineBeverage from './CaffeineBeverage';
2 |
3 | class Coffee extends CaffeineBeverage {
4 | brew() {
5 | console.log("Dripping Coffee through filter!");
6 | }
7 |
8 | addCondiments() {
9 | console.log("Add Sugar and Milk!");
10 | }
11 | }
12 |
13 | export default Coffee;
14 |
--------------------------------------------------------------------------------
/Template/2/scripts/Tea.js:
--------------------------------------------------------------------------------
1 | import CaffeineBeverage from './CaffeineBeverage';
2 |
3 | class Tea extends CaffeineBeverage {
4 | brew() {
5 | console.log("Steeping the tea!");
6 | }
7 |
8 | addCondiments() {
9 | console.log("Adding lemon!");
10 | }
11 | }
12 |
13 | export default Tea;
14 |
--------------------------------------------------------------------------------
/Template/2/scripts/main.js:
--------------------------------------------------------------------------------
1 | import Coffee from './Coffee';
2 | import Tea from './Tea';
3 |
4 | var oCoffee = new Coffee();
5 | oCoffee.prepareRecipe();
6 | console.log("*********************************************************");
7 | var oTea = new Tea();
8 | oTea.prepareRecipe();
9 |
--------------------------------------------------------------------------------
/Try-Finally/examples/problem/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Try-Finally Pattern
5 |
14 |
15 |
16 |
17 |
Source
18 |
19 | var aElements = [];
20 | var nElement = 0;
21 | var nTimes = 100000;
22 | function createElement() {
23 | var div = document.createElement("div");
24 | div.onclick = function () {};
25 | return div;
26 | }
27 | for (; nElement < nTimes; nElement++) {
28 | aElements.push(createElement());
29 | }
30 |
31 |
32 |
33 |
Console
34 |
35 |
36 |
Try-Finally
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Try-Finally/examples/problem/scripts/main.js:
--------------------------------------------------------------------------------
1 | var aElements = [];
2 | var nElement = 0;
3 | var nTimes = 100000;
4 | function createElement() {
5 | var div = document.createElement("div");
6 | div.onclick = function () {};
7 | return div;
8 | }
9 | for (; nElement < nTimes; nElement++) {
10 | aElements.push(createElement());
11 | }
12 |
--------------------------------------------------------------------------------
/Try-Finally/examples/solution/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Try-Finally Pattern
5 |
14 |
15 |
16 |
17 |
Source
18 |
19 | var aElements = [];
20 | var nElement = 0;
21 | var nTimes = 100000;
22 | function createElement() {
23 | var div = document.createElement("div");
24 | div.onclick = function () {};
25 | try {
26 | return div;
27 | } finally {
28 | div = null;
29 | }
30 | }
31 | for (; nElement < nTimes; nElement++) {
32 | aElements.push(createElement());
33 | }
34 |
35 |
36 |
37 |
Console
38 |
39 |
40 |
Try-Finally
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/Try-Finally/examples/solution/scripts/main.js:
--------------------------------------------------------------------------------
1 | var aElements = [];
2 | var nElement = 0;
3 | var nTimes = 100000;
4 | function createElement() {
5 | var div = document.createElement("div");
6 | div.onclick = function () {};
7 | try {
8 | return div;
9 | } finally {
10 | div = null;
11 | }
12 | }
13 | for (; nElement < nTimes; nElement++) {
14 | aElements.push(createElement());
15 | }
16 |
--------------------------------------------------------------------------------
/Try-Finally/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Try-Finally Pattern
5 |
21 |
22 |
23 |
24 |
Source
25 |
26 |
Start example source
27 |
Test 1
28 |
Test 2
29 |
Test 3
30 |
End example source
31 |
32 |
33 | var oTryFinally = new TryFinally();
34 | oTryFinally.getContent();
35 |
36 |
37 |
38 |
Console
39 |
40 |
Try-Finally
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/Try-Finally/scripts/TryFinally.js:
--------------------------------------------------------------------------------
1 | class TryFinally {
2 | getContent() {
3 | var test1 = document.getElementById("test1");
4 | var test2 = document.getElementById("test2");
5 | var test3 = document.getElementById("test3");
6 | test1.onclick = function() {};
7 | test2.onclick = function() {};
8 | test3.onclick = function() {};
9 | try {
10 | return {
11 | test1: test1.innerHTML,
12 | test2: test2.innerHTML,
13 | test3: test3.innerHTML
14 | }
15 | } finally {
16 | test1 = test2 = test3 = null;
17 | }
18 | }
19 | }
20 |
21 | export default TryFinally;
22 |
--------------------------------------------------------------------------------
/Try-Finally/scripts/main.js:
--------------------------------------------------------------------------------
1 | import TryFinally from './TryFinally';
2 |
3 | var oTryFinally = new TryFinally();
4 | var oTexts = oTryFinally.getContent();
5 | console.log(oTexts.test1);
6 | console.log(oTexts.test2);
7 | console.log(oTexts.test3);
8 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "design-patterns-in-javascript",
3 | "version": "2.0.0",
4 | "description": "A set of design patters to use in Javascript with ES2015 a.k.a ES6",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/tcorral/Design-Patterns-in-Javascript.git"
12 | },
13 | "keywords": [
14 | "design",
15 | "patterns",
16 | "js",
17 | "es2015",
18 | "es6"
19 | ],
20 | "author": "Tomas Corral ",
21 | "license": "ISC",
22 | "bugs": {
23 | "url": "https://github.com/tcorral/Design-Patterns-in-Javascript/issues"
24 | },
25 | "homepage": "https://github.com/tcorral/Design-Patterns-in-Javascript#readme",
26 | "devDependencies": {
27 | "babel-core": "^6.7.7",
28 | "babel-plugin-transform-es2015-modules-amd": "^6.6.5",
29 | "babel-preset-es2015": "^6.6.0",
30 | "babelify": "^7.2.0",
31 | "browserify": "^13.0.0",
32 | "gulp": "^3.9.1",
33 | "gulp-babel": "^6.1.2",
34 | "gulp-clean": "^0.3.2",
35 | "gulp-concat": "^2.6.0",
36 | "gulp-sourcemaps": "^2.0.0-alpha",
37 | "gulp-uglify": "^1.5.3",
38 | "vinyl-buffer": "^1.0.0",
39 | "vinyl-source-stream": "^1.1.0",
40 | "watchify": "^3.7.0"
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/statics/css/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | background: black url(../img/background.jpg) repeat-x;
3 | margin: 0 auto;
4 | width: 1001px;
5 | }
6 |
7 | #source {
8 | background: transparent url(../img/source.jpg) no-repeat;
9 | width: 949px;
10 | height: 294px;
11 | margin-top: 30px;
12 | margin-left: 30px;
13 | font-family: Courier, monospace;
14 | font-size: 12px;
15 | color: #ccc;
16 | }
17 |
18 | h1 {
19 | font-size: 50px;
20 | width: 1001px;
21 | padding-top: 20px;
22 | text-align: center;
23 | }
24 |
25 | h2 {
26 | margin: 0;
27 | padding: 10px 0 20px 10px;
28 | }
29 |
30 | #source pre {
31 | display: block;
32 | margin-left: 10px;
33 | height: 230px;
34 | margin-top: 0;
35 | overflow: auto;
36 | width: 937px;
37 | }
38 |
39 | #console {
40 | background: transparent url(../img/consola.png) no-repeat;
41 | width: 1001px;
42 | height: 392px;
43 | margin-top: 50px;
44 | font-family: Courier, monospace;
45 | font-size: 12px;
46 | color: #ccc;
47 | }
48 |
49 | #console ul {
50 | list-style-type: none;
51 | margin: 0;
52 | padding: 10px 0 0 50px;
53 | font-family: Courier, monospace;
54 | font-size: 12px;
55 | color: #ccc;
56 | height: 208px;
57 | width: 924px;
58 | overflow: auto;
59 | }
60 |
61 | #console h2 {
62 | margin: 0;
63 | padding: 10px 0 20px 40px;
64 | }
65 |
--------------------------------------------------------------------------------
/statics/img/background.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tcorral/Design-Patterns-in-Javascript/e37f712d8a3c0988d05cda3ca91893ea78349fc1/statics/img/background.jpg
--------------------------------------------------------------------------------
/statics/img/consola.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tcorral/Design-Patterns-in-Javascript/e37f712d8a3c0988d05cda3ca91893ea78349fc1/statics/img/consola.png
--------------------------------------------------------------------------------
/statics/img/source.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tcorral/Design-Patterns-in-Javascript/e37f712d8a3c0988d05cda3ca91893ea78349fc1/statics/img/source.jpg
--------------------------------------------------------------------------------
/statics/js/utils.js:
--------------------------------------------------------------------------------
1 | var oConsole = document.getElementById("console");
2 | var oList = oConsole.getElementsByTagName("ul")[0];
3 | function fpLog(sMessage) {
4 | var oElement = document.createElement("li");
5 | oElement.appendChild(document.createTextNode("- " + sMessage));
6 | oList.appendChild(oElement);
7 | };
8 |
9 | if (!window.console) {
10 | window.console = {};
11 | window.console.log = function(sMessage) {
12 | fpLog(sMessage);
13 | };
14 | } else {
15 | if (window.console.log) {
16 | var fpConsoleLog = window.console.log;
17 | window.console.log = function(sMessage) {
18 | fpLog(sMessage);
19 | fpConsoleLog.call(window.console, sMessage);
20 | };
21 | }
22 | }
23 |
--------------------------------------------------------------------------------