Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mitch master #1

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
out
.vscode
.DS_Store
*~
node_modules/
dist/
22 changes: 22 additions & 0 deletions src/abstractfactory/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Maze,IMaze} from './maze'
import {IMazeElementFactory} from './factory'
import Direction from './direction';


export function makeMaze(theFactory: IMazeElementFactory): IMaze {
let theMaze = new Maze();
let r1 = theFactory.makeRoom()
let r2 = theFactory.makeRoom()
let theDoor = theFactory.makeDoor(r1, r2);
theMaze.addRoom(r1);
theMaze.addRoom(r2);
r1.setSide(Direction.North, theFactory.makeWall());
r1.setSide(Direction.East, theDoor);
r1.setSide(Direction.South, theFactory.makeWall());
r1.setSide(Direction.West, theFactory.makeWall());
r2.setSide(Direction.North, theFactory.makeWall());
r2.setSide(Direction.East, theFactory.makeWall());
r2.setSide(Direction.South, theFactory.makeWall());
r2.setSide(Direction.West, theDoor);
return theMaze;
}
56 changes: 56 additions & 0 deletions src/abstractfactory/exploding-rooms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// exploding rooms, all in one place
import { PlainRoom } from './room'
import { PlainMazeElementFactory } from './factory'
import { IMazeElementFactory } from './factory'
import { Wall } from './wall'
import { IRoom } from './room'
import { Door } from './door'

// exploding room, also implements interface Room
export class ExplodingRoom extends PlainRoom {
constructor(public hasBomb: boolean) {
super();
}
public explode(): void { }
public toString(): string {
return 'ExplodingRoom #' + this.roomID + ' (hasBomb = ' + this.hasBomb + ')'
}
}

// factory for exploding rooms. This factory puts a bomb in every other room,
// but some other factory might do it differently-- the decision about which
// rooms get bombs is a property of the factory, not of the rooms.

// version 1: build it from scratch
export class ExplodingRoomFactory1 implements IMazeElementFactory {
// put a bomb in every other room
private roomCounter: number = 0
public makeRoom(): ExplodingRoom {
this.roomCounter++;
return new ExplodingRoom((this.roomCounter % 2) == 0)
}
public makeWall(): Wall { return new Wall() }
public makeDoor(room1: IRoom, room2: IRoom): Door { return new Door(room1, room2) }
}

// But this shares a lot of behavior with the first factory (PlainMazeElementFactory),
// so we can use inheritance to avoid reimplementing the other methods.

// It doesn't make much difference here, but if there were a lot of other methods
// it would be a big help.

// The use of inheritance here is not an essential part of the Abstract Factory pattern.
// However, it will often be the case that one factory is only a minor variant of an earlier one,
// so inheritance will often turn out to be useful in conjunction with the Abstract Factory
// pattern

export class ExplodingRoomFactory2 extends PlainMazeElementFactory {
private roomCounter: number = 0
public makeRoom(): ExplodingRoom {
this.roomCounter++;
// put a bomb in every other room
return new ExplodingRoom((this.roomCounter % 2) == 0)
}
}


5 changes: 4 additions & 1 deletion src/abstractfactory/exploding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Room from "./room";
import PlainMazeFactory from "./plainmazefactory";
import IMazeFactory from './imazefactory'

class ExplodingRoom extends Room {
constructor(public hasBomb: boolean) {
Expand All @@ -11,7 +12,9 @@ class ExplodingRoom extends Room {
}
}

class ExplodingRoomFactory extends PlainMazeFactory {
class ExplodingRoomFactory extends PlainMazeFactory
implements IMazeFactory // implements the abstract factory
{
private roomCounter: number = 0
public makeRoom(): ExplodingRoom {
this.roomCounter++;
Expand Down
21 changes: 21 additions & 0 deletions src/abstractfactory/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// factory.ts
// defines PlainMazeFactory

import {IRoom, PlainRoom} from "./room"
import {Wall} from "./wall"
import {Door} from './door'

export interface IMazeElementFactory {
makeRoom(): IRoom
makeWall(): Wall
makeDoor(room1: IRoom, room2: IRoom): Door
}

export class PlainMazeElementFactory implements IMazeElementFactory {
public makeRoom(): IRoom { return new PlainRoom() }
public makeWall(): Wall { return new Wall() }
public makeDoor(room1: IRoom, room2: IRoom): Door { return new Door(room1, room2) }
}



8 changes: 8 additions & 0 deletions src/abstractfactory/mazeelement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// a maze element is something you can enter
// currently, a room, a door, or a wall
// don't try to enter a wall!

export interface IMazeElement
{
enter() : void
}
Loading