Factory Methods
package designPattern;
//creator
class MazeGame{
public Maze CreateMaze()
{
Maze aMaze = MakeMaze();
Room r1 = MakeRoom(1);
Room r2 = MakeRoom(2);
Door theDoor = MakeDoor(r1, r2);
aMaze.AddRoom(r1);
aMaze.AddRoom(r2);
r1.SetSide(North, MakeWall());
r1.SetSide(East, theDoor);
r1.SetSide(South, MakeWall());
r1.SetSide(West, MakeWall());
r2.SetSide(North, MakeWall());
r2.SetSide(East, MakeWall());
r2.SetSide(South, MakeWall());
r2.SetSide(West, theDoor);
}
//factory Methods
public Maze MakeMaze()
{
return new Maze();
}
public Door MakeDoor(Room r1, Room r2)
{
return new Door(r1, r2);
}
public Room MakeRoom(int n)
{
return new Room(n);
}
public Wall MakeWall()
{
return new Wall();
}
}
//concrete creator
class BombMazeGame extends MazeGame{
public Wall MakeWall()
{
//concrete product
return new BombWall();
}
public Room MakeRoom(int n)
{
return new BombRoom(n);
}
}
//concrete creator
class EnhantedMazeGame extends MazeGame{
public Wall MakeRoom(int n)
{
return new EnhantedRoom(n, CastSpell());
}
public Door MakeDoor(Room r1, Room r2){
return new DoorNeedingSpell(r1, r2);
}
protected Spell CastSpell();
}
public class FactoryMethods {
}
//creator
class MazeGame{
public Maze CreateMaze()
{
Maze aMaze = MakeMaze();
Room r1 = MakeRoom(1);
Room r2 = MakeRoom(2);
Door theDoor = MakeDoor(r1, r2);
aMaze.AddRoom(r1);
aMaze.AddRoom(r2);
r1.SetSide(North, MakeWall());
r1.SetSide(East, theDoor);
r1.SetSide(South, MakeWall());
r1.SetSide(West, MakeWall());
r2.SetSide(North, MakeWall());
r2.SetSide(East, MakeWall());
r2.SetSide(South, MakeWall());
r2.SetSide(West, theDoor);
}
//factory Methods
public Maze MakeMaze()
{
return new Maze();
}
public Door MakeDoor(Room r1, Room r2)
{
return new Door(r1, r2);
}
public Room MakeRoom(int n)
{
return new Room(n);
}
public Wall MakeWall()
{
return new Wall();
}
}
//concrete creator
class BombMazeGame extends MazeGame{
public Wall MakeWall()
{
//concrete product
return new BombWall();
}
public Room MakeRoom(int n)
{
return new BombRoom(n);
}
}
//concrete creator
class EnhantedMazeGame extends MazeGame{
public Wall MakeRoom(int n)
{
return new EnhantedRoom(n, CastSpell());
}
public Door MakeDoor(Room r1, Room r2){
return new DoorNeedingSpell(r1, r2);
}
protected Spell CastSpell();
}
public class FactoryMethods {
}