Created
August 7, 2020 06:29
-
-
Save andhieka/ceff50db44406abd913459f67a00884b to your computer and use it in GitHub Desktop.
PrototypePattern
This file contains hidden or 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
//#region Plain Maze | |
enum Direction { | |
North, | |
West, | |
South, | |
East | |
} | |
class Maze { | |
List<Room> rooms = []; | |
void addRoom(Room room) { | |
rooms.add(room); | |
} | |
} | |
// Basically an interface | |
abstract class RoomSide { | |
RoomSide clone(); | |
} | |
class Wall implements RoomSide { | |
Wall clone() { | |
return Wall(); | |
} | |
} | |
class Door implements RoomSide { | |
Room room1; | |
Room room2; | |
Door(this.room1, this.room2); | |
Door clone() { | |
// Shallow-copy | |
return Door(room1, room2); | |
} | |
} | |
class Room { | |
Map<Direction, RoomSide> sides = {}; | |
void setWall(Direction direction, RoomSide side) { | |
sides[direction] = side; | |
} | |
} | |
class MazeFactory { | |
Maze createMaze() => Maze(); | |
Wall createWall() => Wall(); | |
Room createRoom() => Room(); | |
Door createDoor(Room room1, Room room2) => Door(room1, room2); | |
} | |
//#endregion | |
//#region Strengthened Maze | |
class StrengthenedDoor extends Door { | |
StrengthenedDoor(room1, room2) : super(room1, room2); | |
Door clone() => StrengthenedDoor(room1, room2); | |
} | |
class StrengthenedWall extends Wall { | |
int strength; | |
StrengthenedWall({this.strength}) : super(); | |
Wall clone() { | |
return StrengthenedWall(strength: strength); | |
} | |
} | |
class StrengthenedMazeFactory extends MazeFactory { | |
Wall createWall() => StrengthenedWall(); | |
Door createDoor(Room room1, Room room2) => StrengthenedDoor(room1, room2); | |
} | |
//#endregion | |
//#region Spellbound Maze | |
class SpellboundDoor extends Door { | |
SpellboundDoor(room1, room2) : super(room1, room2); | |
} | |
class SpellboundMazeFactory extends MazeFactory { | |
Door createDoor(Room room1, Room room2) => SpellboundDoor(room1, room2); | |
} | |
//#endregion | |
//#region MixMatch | |
class MixMatchFactory extends MazeFactory { | |
Door prototypeDoor; | |
Wall prototypeWall; | |
MixMatchFactory({ | |
this.prototypeDoor, | |
this.prototypeWall, | |
}) : super(); | |
@override | |
Wall createWall() => prototypeWall.clone(); | |
@override | |
Door createDoor(room1, room2) { | |
Door door = prototypeDoor.clone(); | |
door.room1 = room1; | |
door.room2 = room2; | |
return door; | |
} | |
} | |
final testMixMatchFactory = MixMatchFactory( | |
prototypeDoor: SpellboundDoor(null, null), | |
prototypeWall: StrengthenedWall(strength: 100), | |
); | |
//#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment