Factorymeth.C
/*
*/
class Product {};
#ifdef Implementation1
class MyProduct: public Product {};
class YourProduct: public Product {};
class TheirProduct: public Product {};
typedef int ProductId;
const int MINE = 1;
const int YOURS = 2;
const int THEIRS = 2;
/*
*/
class Creator {
public:
virtual Product* Create(ProductId);
};
/*
*/
Product* Creator::Create (ProductId id) {
if (id == MINE) return new MyProduct;
if (id == YOURS) return new YourProduct;
// repeat for remaining products...
return 0;
}
/*
*/
class MyCreator: public Creator {
public:
virtual Product* Create(ProductId);
};
/*
*/
Product* MyCreator::Create (ProductId id) {
if (id == YOURS) return new MyProduct;
if (id == MINE) return new YourProduct;
// N.B.: switched YOURS and MINE
if (id == THEIRS) return new TheirProduct;
return Creator::Create(id); // called if all others fail
}
/*
*/
#endif
#ifdef Implementation2
/*
*/
class Creator {
public:
Product* GetProduct();
protected:
virtual Product* CreateProduct();
private:
Product* _product;
};
/*
*/
Product* Creator::GetProduct () {
if (_product == 0) {
_product = CreateProduct();
}
return _product;
}
/*
*/
#endif
#ifdef Implementation3
/*
*/
class Creator {
public:
virtual Product* CreateProduct() = 0;
};
/*
*/
template <class TheProduct>
class StandardCreator: public Creator {
public:
virtual Product* CreateProduct();
};
/*
*/
template <class TheProduct>
Product* StandardCreator<TheProduct>::CreateProduct () {
return new TheProduct;
}
/*
*/
class MyProduct: public Product {
public:
MyProduct();
//...
};
StandardCreator<MyProduct> myCreator;
/*
*/
#endif
/*
*/
#include "C++/MazeParts.H"
/*
*/
class MazeGame {
public:
Maze* CreateMaze();
/*
*/
// factory methods:
/*
*/
virtual Maze* MakeMaze() const
{ return new Maze; }
virtual Room* MakeRoom(int n) const
{ return new Room(n); }
virtual Wall* MakeWall() const
{ return new Wall; }
virtual Door* MakeDoor(Room* r1, Room* r2) const
{ return new Door(r1, r2); }
};
/*
*/
Maze* MazeGame::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);
/*
*/
return aMaze;
}
/*
*/
class BombedMazeGame: public MazeGame {
public:
BombedMazeGame();
/*
*/
virtual Wall* MakeWall() const
{ return new BombedWall; }
/*
*/
virtual Room* MakeRoom(int n) const
{ return new RoomWithABomb(n); }
};
/*
*/
class EnchantedMazeGame: public MazeGame {
public:
EnchantedMazeGame();
/*
*/
virtual Room* MakeRoom(int n) const
{ return new EnchantedRoom(n, CastSpell()); }
/*
*/
virtual Door* MakeDoor(Room* r1, Room* r2) const
{ return new DoorNeedingSpell(r1, r2); }
protected:
Spell* CastSpell() const;
};
/*
*/
Builder.C
#include "MazeParts.H"
#include "MazeGame.H"
#include <iostream.h>
class MazeBuilder {
public:
virtual void BuildMaze() { }
virtual void BuildRoom(int room) { }
virtual void BuildDoor(int roomFrom, int roomTo) { }
virtual Maze* GetMaze() { return 0; }
protected:
MazeBuilder();
};
Maze* MazeGame::CreateMaze (MazeBuilder& builder) {
builder.BuildMaze();
builder.BuildRoom(1);
builder.BuildRoom(2);
builder.BuildDoor(1, 2);
return builder.GetMaze();
}
Maze* MazeGame::CreateComplexMaze (MazeBuilder& builder) {
builder.BuildRoom(1);
//...
builder.BuildRoom(1001);
return builder.GetMaze();
}
class StandardMazeBuilder: public MazeBuilder {
public:
StandardMazeBuilder();
virtual void BuildMaze();
virtual void BuildRoom(int);
virtual void BuildDoor(int, int);
virtual Maze* GetMaze();
private:
Direction CommonWall(Room*, Room*);
Maze* _currentMaze;
};
StandardMazeBuilder::StandardMazeBuilder () {
_currentMaze = 0;
}
void StandardMazeBuilder::BuildMaze () {
_currentMaze = new Maze;
}
Maze *StandardMazeBuilder::GetMaze () {
Maze* maze = _currentMaze;
return maze;
}
void StandardMazeBuilder::BuildRoom (int n) {
if (!_currentMaze->RoomNo(n)) {
Room* room = new Room(n);
_currentMaze->AddRoom(room);
room->SetSide(North, new Wall);
room->SetSide(South, new Wall);
room->SetSide(East, new Wall);
room->SetSide(West, new Wall);
}
}
void StandardMazeBuilder::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);
}
void dummy() {
Maze* maze;
MazeGame game;
StandardMazeBuilder builder;
game.CreateMaze(builder);
maze = builder.GetMaze();
}
class CountingMazeBuilder: public MazeBuilder {
public:
CountingMazeBuilder();
virtual void BuildMaze();
virtual void BuildRoom(int);
virtual void BuildDoor(int, int);
virtual void AddWall(int, Direction);
void GetCounts(int&, int&) const;
private:
int _doors;
int _rooms;
};
CountingMazeBuilder::CountingMazeBuilder () {
_rooms = _doors = 0;
}
void CountingMazeBuilder::BuildRoom (int) {
_rooms++;
}
void CountingMazeBuilder::BuildDoor (int, int) {
_doors++;
}
void CountingMazeBuilder::GetCounts (
int& rooms, int& doors
) const {
rooms = _rooms;
doors = _doors;
}
void dummy1() {
int rooms, doors;
MazeGame game;
CountingMazeBuilder builder;
game.CreateMaze(builder);
builder.GetCounts(rooms, doors);
cout << "The maze has "
<< rooms << " rooms and "
<< doors << " doors" << endl;
}
MazeParts.H
#ifndef MazeParts_H
#define MazeParts_H
#include "defs.H"
enum Direction { North, East, South, West };
#ifndef MapSite_H
#define MapSite_H
class MapSite {
public:
virtual void Enter() = 0;
};
#endif
#ifndef _H
#define _H
class Room: public MapSite {
public:
Room(int = 0);
Room(const Room&);
virtual Room* Clone() const;
void InitializeRoomNo(int);
MapSite* GetSide(Direction);
void SetSide(Direction, MapSite*);
virtual void Enter();
private:
MapSite* _sides[4];
int _roomNumber;
};
#endif
#ifndef Wall_H
#define Wall_H
class Wall: public MapSite {
public:
Wall();
Wall(const Wall&);
virtual Wall* Clone() const;
virtual void Enter();
};
#endif
#ifndef Door_H
#define Door_H
class Door: public MapSite {
public:
Door(Room* = 0, Room* = 0);
Door(const Room&);
virtual Door* Clone() const;
void Initialize(Room*, Room*);
virtual void Enter();
Room* OtherSideFrom(Room*);
private:
Room* _room1;
Room* _room2;
bool _isOpen;
};
#endif
#ifndef Maze_H
#define Maze_H
class Maze {
public:
Maze();
Maze(const Maze&);
Room* RoomNo(int);
void AddRoom(Room*);
virtual Maze* Clone() const;
private:
//...
};
#endif
#ifndef BombedWall_H
#define BombedWall_H
class BombedWall: public Wall {
public:
BombedWall(bool bombed = false);
BombedWall(const BombedWall&);
virtual Wall* Clone() const;
void Intialize(bool);
virtual void Enter();
private:
bool _bomb;
};
#endif
#ifndef RoomWithABomb_H
#define RoomWithABomb_H
class RoomWithABomb: public Room {
public:
RoomWithABomb(int = 0, bool bombed = false);
RoomWithABomb(const RoomWithABomb&);
bool HasBomb();
private:
bool _bomb;
};
#endif
#ifndef EnchantedRoom_H
#define EnchantedRoom_H
class Spell;
class EnchantedRoom: public Room {
public:
EnchantedRoom(int, Spell* = 0);
EnchantedRoom(const EnchantedRoom&);
bool HasSpell();
Spell PickUpSpell();
private:
Spell* _spell;
};
#endif
#ifndef DoorNeedingSpell_H
#define DoorNeedingSpell_H
class DoorNeedingSpell: public Door {
public:
DoorNeedingSpell(Room*, Room*);
DoorNeedingSpell(const DoorNeedingSpell&);
bool TrySpell(Spell);
};
#endif
#endif
MazeGame.H
#ifndef MazeGame_H
#define MazeGame_H
class Maze;
class Wall;
class Door;
class Room;
class MazeFactory;
class MazeBuilder;
class MazeGame {
public:
Maze* CreateMaze();
Maze* CreateSimpleMaze();
Maze* CreateMaze(MazeFactory&);
Maze* CreateMaze(MazeBuilder&);
Maze* CreateComplexMaze (MazeBuilder& builder);
// factory methods
virtual Maze* MakeMaze() const;
virtual Room* MakeRoom(int n) const;
virtual Wall* MakeWall() const;
virtual Door* MakeDoor(Room* r1, Room* r2) const;
};
#endif