Builder
package designPattern;
//builder base class
//为创建product的各个部件指定抽象接口
class MazeBuilder{
public void BuildMaze()
{
}
public void BuildRoom(int n)
{
}
public void BuildDoor(int roomFrom, int roomTo)
{
}
public Maze GetMaze()
{
return null;
}
protected MazeBuilder()
{
}
}
//Director use builder
//可以控制创建产品的过程
class MazeGame2{
public Maze CreateMaze(MazeBuilder builder)
{
builder.BuildMaze();
builder.BuildRoom(1);
builder.BuildRoom(2);
builder.BuildDoor(1, 2);
return builder.GetMaze();
}
public Maze CreateComplexMaze(MazeBuilder builder)
{
builder.BuildRoom(1);
builder.BuildRoom(2);
builder.BuildRoom(3);
builder.BuildRoom(4);
builder.BuildRoom(5);
builder.BuildRoom(6);
builder.BuildRoom(7);
builder.BuildRoom(8);
builder.BuildRoom(9);
builder.BuildRoom(10);
builder.BuildRoom(11);
builder.BuildRoom(12);
builder.BuildRoom(13);
builder.BuildRoom(14);
builder.BuildRoom(15);
builder.BuildRoom(16);
builder.BuildRoom(17);
return builder.GetMaze();
}
}
//derived builder
//实现product的各个部件,并完成装配该产品个各部件
//提供一个检索产品的接口
class StandardMazeBuilder extends MazeBuilder{
public void BuildMaze()
{
_currentMaze = new Maze();
}
public void BuildRoom(int n)
{
if (!_currentMaze.RoomNo(n))
{
Room room = new Room();
}
_currentMaze.AddRoom(room);
room.SetSide(North, new Wall());
room.SetSide(East, new Wall());
room.SetSide(South, new Wall());
room.SetSide(West, new Wall());
}
public void BuildDoor(int n1, int n2)
{
Room r1 = _currentMaze.RoomNo(n1);
Room r2 = _currentMaze.RoomNo(n2);
Door d = new Door(r1, r2);
r1.SetSide(CommonWall(r1,r2), d);
r2.SetSide(CommonWall(r2,r1), d);
}
public Maze GetMaze()
{
return _currentMaze;
}
private Direction CommanWall(Room r1, Room r2)
{
}
private Maze _currentMaze;
}
class CountingMazeBuilder extends MazeBuilder{
private int _doors;
private int _rooms;
public void BuildRoom(int n)
{
_rooms++;
}
public void BuildDoor(int n1, int n2)
{
_doors++;
}
public void GetCounts(int rooms, int doors)
{
rooms = _rooms;
doors = _doors;
}
}
public class Builder {
public static void main(String[] args)
{
Maze maze;
MazeGame game;
StandardMazeBuilder builder;
game.CreateMaze(builder);
maze = builder.GetMaze();
int rooms, doors;
MazeGame game1;
CountingMazeBuilder builder_c;
game.CreateMaze(builder_c);
builder_c.GetCount(rooms, doors);
}
}
//builder base class
//为创建product的各个部件指定抽象接口
class MazeBuilder{
public void BuildMaze()
{
}
public void BuildRoom(int n)
{
}
public void BuildDoor(int roomFrom, int roomTo)
{
}
public Maze GetMaze()
{
return null;
}
protected MazeBuilder()
{
}
}
//Director use builder
//可以控制创建产品的过程
class MazeGame2{
public Maze CreateMaze(MazeBuilder builder)
{
builder.BuildMaze();
builder.BuildRoom(1);
builder.BuildRoom(2);
builder.BuildDoor(1, 2);
return builder.GetMaze();
}
public Maze CreateComplexMaze(MazeBuilder builder)
{
builder.BuildRoom(1);
builder.BuildRoom(2);
builder.BuildRoom(3);
builder.BuildRoom(4);
builder.BuildRoom(5);
builder.BuildRoom(6);
builder.BuildRoom(7);
builder.BuildRoom(8);
builder.BuildRoom(9);
builder.BuildRoom(10);
builder.BuildRoom(11);
builder.BuildRoom(12);
builder.BuildRoom(13);
builder.BuildRoom(14);
builder.BuildRoom(15);
builder.BuildRoom(16);
builder.BuildRoom(17);
return builder.GetMaze();
}
}
//derived builder
//实现product的各个部件,并完成装配该产品个各部件
//提供一个检索产品的接口
class StandardMazeBuilder extends MazeBuilder{
public void BuildMaze()
{
_currentMaze = new Maze();
}
public void BuildRoom(int n)
{
if (!_currentMaze.RoomNo(n))
{
Room room = new Room();
}
_currentMaze.AddRoom(room);
room.SetSide(North, new Wall());
room.SetSide(East, new Wall());
room.SetSide(South, new Wall());
room.SetSide(West, new Wall());
}
public void BuildDoor(int n1, int n2)
{
Room r1 = _currentMaze.RoomNo(n1);
Room r2 = _currentMaze.RoomNo(n2);
Door d = new Door(r1, r2);
r1.SetSide(CommonWall(r1,r2), d);
r2.SetSide(CommonWall(r2,r1), d);
}
public Maze GetMaze()
{
return _currentMaze;
}
private Direction CommanWall(Room r1, Room r2)
{
}
private Maze _currentMaze;
}
class CountingMazeBuilder extends MazeBuilder{
private int _doors;
private int _rooms;
public void BuildRoom(int n)
{
_rooms++;
}
public void BuildDoor(int n1, int n2)
{
_doors++;
}
public void GetCounts(int rooms, int doors)
{
rooms = _rooms;
doors = _doors;
}
}
public class Builder {
public static void main(String[] args)
{
Maze maze;
MazeGame game;
StandardMazeBuilder builder;
game.CreateMaze(builder);
maze = builder.GetMaze();
int rooms, doors;
MazeGame game1;
CountingMazeBuilder builder_c;
game.CreateMaze(builder_c);
builder_c.GetCount(rooms, doors);
}
}