This article is to supplement the following article, you can go to see below.
http://www.9cbs.net/develop/read_article.asp?id=20943
The example in the GOF book uses the source code implemented by C #:
Using system;
Namespace AbstractFactory_maze {
USING MAZE;
Public interface abstractFactory {
Mazeclass makemaze ();
Wall makewall ();
Room Makeroom (INT N);
Door Makedoor (room liveoom, room otherroom);
}
PUBLIC CLASS MAZEFACTORY: AbstractFactory {
Public mazeclass makemaze () {
Return new mazeclass ();
}
Public wall made makewall () {
Return New Wall ();
}
Public room makeroom (int N) {
Return New Room (n);
}
Public Door Makedoor (Room OnRoom, Room Otherroom) {
Return New Door (OneRoom, Otherroom);
}
}
// this is aclient
Public class mazegame {
Public MazeClass Mazecreate (AbstractFactory Factory) {
MazeClass Amaze = Factory.makemaze ();
Room r1 = factory.makeroom (1);
Room r2 = factory.makeroom (2);
Door adoor = factory.makedoor (R1, R2);
Amaze.Addroom (R1);
Amaze.addroom (r2);
R1.setside (Direction.North, Factory.makewall ());
R1.setside (Direction.east, Adoor);
R1.setside (Direction.South, Factory.makewall ());
R1.setside (Direction.west, Factory.makewall ());
R2.setside (Direction.North, Factory.makewall ());
R2.setside (Direction.east, Factory.makewall ());
R2.setside (Direction.South, Factory.makewall ());
R2.setside (Direction.West, Adoor);
Return Amaze;
}
}
}
Namespace maze {
Using system.collections;
Public class mapsite {
Public Virtual Void Enter () {}
}
Public Enum Direction {North, South, EAST, West}
Public class room: Mapsite {
Public String Print () {
String result = "";
For (int i = 0; i <= 3; i ) {
Switch (i) {
Case (int) Direction.east: {
Result = "EAST IS:" THIS.GETSIDE (DIRECTION.EAST) "/ T";
Break;
}
Case (int) direction.north: {result = "north is:" this.getside (direction.north) "/ t";
Break;
}
Case (int) Direction.South: {
Result = "South IS:" this.getside (direction.Sout) "/ t";
Break;
}
Case (int) Direction.west: {
Result = "West is:" this.getside (direction.west) "/ t";
Break;
}
}
}
Return Result;
}
Public Room (int N) {
THIS.M_ROOMNUMBER = N;
}
Public mapsite getside (direction dir) {
Return this.m_sides [(int) DIR];
}
Public void setside (direction dir, mapsite maps) {
THIS.M_SIDES [(int) DIR] = Mapsite;
}
Public override void enter () {}
Private mapsite [] m_sides = new mapsite [4];
Int m_roomnumber;
}
Public class wall: Mapsite {
Public wall () {}
Public override void enter () {}
}
PUBLIC CLASS DOOR: MapSite {
Public Door (room inroom, room odiot) {}
Public override void enter () {}
Public room {
Get {return this.m_oneroom;
Set {this.m_oneroom = value;
}
PRIVATE ROOM M_ONEROOM;
Public room Otherroom {
Get {return this.m_otherroom;}
Set {this.m_otherroom = value;
}
PRIVATE ROOM M_OTHERROOM
PRIVATE BOOL M_ISOPEN;
Public bool isopen {
Get {return this.m_isopen;
Set {this.m_isopen = value;
}
}
Public class mazeclass {
Public mazeclass () {}
Public String Print () {
String result = "";
For (int i = 0; i <= this.m_maze.count-1; i ) {
Result = this.Roomnumber (i) .print () "/ n";
}
Return Result;
}
Public void addroom (room room) {
M_maze.add (room);
}
Public room roomnumber (int roomnumber) {
Return (Room) this.m_maze [roomnumber];
}
PRIVATE ARRAYLIST M_MAZE = New ArrayList ();
}
}
Private void form1_load (object sender, system.eventargs e) {AbstractFactory factory = new mazeFactory ();
Mazegame Game = new mazegame ();
MazeClass Amaze = Game.mazecreate (Factory);
This.RichtextBox1.AppendText (Amaze.print ());
}
The following is the output result:
North Is: Maze.wall South Is: Maze.Wall East Is: Maze.door West Is: Maze.wall
North Is: Maze.wall South IS: Maze.wall East Is: Maze.wall West Is: Maze.door