forked from lfdev-gdg/designpatternsjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f6f8c93
Showing
42 changed files
with
774 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const MazeGame = require ('./maze/MazeGame'); | ||
|
||
const BombedMazeFactory = require ('./maze/bombed/BombedMazeFactory'); | ||
const EnchantedMazeFactory = require ('./maze/enchanted/EnchantedMazeFactory'); | ||
|
||
const bombedGame = new BombedMazeFactory(); | ||
const enchantedGame = new EnchantedMazeFactory(); | ||
|
||
const mazeGame = new MazeGame(); | ||
const game = mazeGame.createMaze(enchantedGame); | ||
|
||
console.log(game); | ||
console.log(game.getRooms()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class Door { | ||
|
||
constructor(room1, room2) { | ||
this.room1 = room1; | ||
this.room2 = room2; | ||
} | ||
|
||
isOpen() { | ||
return this.isOpen; | ||
} | ||
|
||
open() { | ||
if (!this.isOpen) { | ||
this.isOpen = !isOpen; | ||
} | ||
} | ||
|
||
initialize(room1, room2) { | ||
this.room1 = room1; | ||
this.room2 = room2; | ||
} | ||
} | ||
|
||
module.exports = Door; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Maze { | ||
|
||
constructor() { | ||
this.rooms = []; | ||
} | ||
|
||
addRoom(room) { | ||
this.rooms.push(room); | ||
} | ||
|
||
getRooms() { | ||
return this.rooms; | ||
} | ||
|
||
clone () { | ||
return new Maze(); | ||
} | ||
} | ||
|
||
module.exports = Maze; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const Door = require('./Door'); | ||
const Room = require('./Room'); | ||
const Maze = require('./Maze'); | ||
const Wall = require('./Wall'); | ||
|
||
class MazeFactory { | ||
|
||
constructor() {} | ||
|
||
makeMaze() { | ||
return new Maze(); | ||
} | ||
|
||
makeDoor(room1, room2) { | ||
return new Door(room1, room2); | ||
} | ||
|
||
makeRoom(roomNumber) { | ||
return new Room(roomNumber); | ||
} | ||
|
||
makeWall() { | ||
return new Wall(); | ||
} | ||
} | ||
|
||
module.exports = MazeFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class MazeGame { | ||
|
||
createMaze (factory) { | ||
this.maze = factory.makeMaze(); | ||
|
||
let room1 = factory.makeRoom(1); | ||
let room2 = factory.makeRoom(2); | ||
let door = factory.makeDoor(room1, room2); | ||
|
||
this.maze.addRoom(room1); | ||
|
||
room1.setSide('north', factory.makeWall()); | ||
room1.setSide('south', door); | ||
|
||
return this.maze; | ||
} | ||
|
||
} | ||
|
||
module.exports = MazeGame; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Room { | ||
|
||
constructor(number) { | ||
this.roomNumber = number; | ||
|
||
this.positions = []; | ||
this.elements = []; | ||
} | ||
|
||
setNumber(number) { | ||
this.roomNumber = number; | ||
} | ||
|
||
setSide(position, element) { | ||
this.positions.push(position); | ||
this.elements.push(element); | ||
} | ||
} | ||
|
||
module.exports = Room; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Wall { | ||
|
||
} | ||
|
||
module.exports = Wall; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Door = require ('../Door'); | ||
|
||
class BombedDoor extends Door { | ||
constructor(room1, room2) { | ||
super(room1, room2); | ||
} | ||
} | ||
|
||
module.exports = BombedDoor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Maze = require ('../Maze'); | ||
|
||
class BombedMaze extends Maze { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
|
||
module.exports = BombedMaze; |
30 changes: 30 additions & 0 deletions
30
creational/abstract-factory/maze/bombed/BombedMazeFactory.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const MazeFactory = require ('../MazeFactory'); | ||
|
||
const Door = require('./BombedDoor'); | ||
const Room = require('./BombedRoom'); | ||
const Maze = require('./BombedMaze'); | ||
const Wall = require('./BombedWall'); | ||
|
||
class BombedMazeFactory extends MazeFactory { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
makeMaze() { | ||
return new Maze(); | ||
} | ||
|
||
makeDoor(room1, room2) { | ||
return new Door(room1, room2); | ||
} | ||
|
||
makeRoom(roomNumber) { | ||
return new Room(roomNumber); | ||
} | ||
|
||
makeWall() { | ||
return new Wall(); | ||
} | ||
} | ||
|
||
module.exports = BombedMazeFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Room = require ('../Room'); | ||
|
||
class BombedRoom extends Room { | ||
constructor(roomNumber) { | ||
super(roomNumber); | ||
} | ||
} | ||
|
||
module.exports = BombedRoom; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Wall = require ('../Wall'); | ||
|
||
class BombedWall extends Wall { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
|
||
module.exports = BombedWall; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Door = require ('../Door'); | ||
|
||
class EnchantedDoor extends Door { | ||
constructor(room1, room2) { | ||
super(room1, room2); | ||
} | ||
} | ||
|
||
module.exports = EnchantedDoor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Maze = require ('../Maze'); | ||
|
||
class EnchantedMaze extends Maze { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
|
||
module.exports = EnchantedMaze; |
30 changes: 30 additions & 0 deletions
30
creational/abstract-factory/maze/enchanted/EnchantedMazeFactory.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const MazeFactory = require ('../MazeFactory'); | ||
|
||
const Door = require('./EnchantedDoor'); | ||
const Room = require('./EnchantedRoom'); | ||
const Maze = require('./EnchantedMaze'); | ||
const Wall = require('./EnchantedWall'); | ||
|
||
class EnchantedMazeFactory extends MazeFactory { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
makeMaze() { | ||
return new Maze(); | ||
} | ||
|
||
makeDoor(room1, room2) { | ||
return new Door(room1, room2); | ||
} | ||
|
||
makeRoom(roomNumber) { | ||
return new Room(roomNumber); | ||
} | ||
|
||
makeWall() { | ||
return new Wall(); | ||
} | ||
} | ||
|
||
module.exports = EnchantedMazeFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Room = require ('../Room'); | ||
|
||
class EnchantedRoom extends Room { | ||
constructor(roomNumber) { | ||
super(roomNumber); | ||
} | ||
} | ||
|
||
module.exports = EnchantedRoom; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Wall = require ('../Wall'); | ||
|
||
class EnchantedWall extends Wall { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
|
||
module.exports = EnchantedWall; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const BurgerBuilder = require ('./subway/BurgerBuilder'); | ||
|
||
const burger = (new BurgerBuilder(15)) | ||
.chooseBread('3 queijos') | ||
.chooseCheeze('cheedar') | ||
.addSauces(['parmesão', 'chiplote']) | ||
.addSalads(['tomate', 'alface', 'azeitonas']) | ||
.build(); | ||
|
||
console.log(burger); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Burger { | ||
constructor(builder) { | ||
this.size = builder.size; | ||
this.bread = builder.bread; | ||
this.cheeze = builder.cheeze; | ||
this.sauces = builder.sauces || false; | ||
this.salads = builder.salads || false; | ||
} | ||
} | ||
|
||
module.exports = Burger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const Burger = require ('./Burger'); | ||
|
||
class BurgerBuilder { | ||
|
||
constructor(size) { | ||
this.size = size | ||
} | ||
|
||
chooseBread(bread) { | ||
this.bread = bread; | ||
return this; | ||
} | ||
|
||
chooseCheeze(cheeze) { | ||
this.cheeze = cheeze; | ||
return this; | ||
} | ||
|
||
addSauces(sauces = []) { | ||
this.sauces = sauces; | ||
return this; | ||
} | ||
|
||
addSalads(salads = []) { | ||
this.salads = salads; | ||
return this; | ||
} | ||
|
||
build() { | ||
return new Burger(this); | ||
} | ||
} | ||
|
||
module.exports = BurgerBuilder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CarFactoryMethod { | ||
constructor () { | ||
this.cheap = 'cheap'; | ||
this.fast = 'fast'; | ||
} | ||
|
||
create (type) { | ||
return this.makeCar(type); | ||
} | ||
} | ||
|
||
module.exports = CarFactoryMethod; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Mercedes { | ||
set color (color) { | ||
this.myColor = color; | ||
} | ||
|
||
get color () { | ||
return this.myColor; | ||
} | ||
|
||
addAMGTunning() {} | ||
} | ||
|
||
class Ferrari { | ||
set color (color) { | ||
this.myColor = color; | ||
} | ||
|
||
get color () { | ||
return this.myColor; | ||
} | ||
} | ||
|
||
class Beetle { | ||
set color (color) { | ||
this.myColor = color; | ||
} | ||
|
||
get color () { | ||
return this.myColor; | ||
} | ||
} | ||
|
||
module.exports = {Mercedes, Ferrari, Beetle}; |
Oops, something went wrong.