.


:




:

































 

 

 

 


1.

2

Builder


1. .. 2

2. .. 8

3. 1. ... 9

 

 

Builder

- , .

 

, :

  • , ;
  • .

 

 

- Builder - :

  • Product;
  • ConcreteBuilder - : Builder; ; ;

- Director - :

  • - , Builder;

- Product (ASCIIText, TeXText, TextWidget) - :

  • . ConcreteBuilder ;
  • , , .

 

  • - Director - Builder;
  • , ;
  • ;
  • .

.

 

 

 

 

:

Ø . Builder , , . , ;

Ø , . , . , , .

 

Builder, , .

. ConcreteBuilder , .

 

- CreateMaze, , MazeBuilder.

MazeBuilder :

 

 

 

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();

};

 

: , , . GetMaze . MazeBuilder .

MazeBuilder . , .

MazeBuilder, - CreateMaze, :

 

 

Maze* MazeGame::CreateMaze (MazeBuilder& builder) {

builder.BuildMaze();

builder.BuiIdRoom(l);

builder.BuiIdRoom(2);

builder.BuildDoor(1, 2);

return builder.GetMaze();

}

 

, , , , . -, , , , . , MazeBuilder .

, ; , MazeBuilder. , MazeBuilder . GreateComplexMaze:

 

 

Maze* MazeGame::CreateComplexMaze (MazeBuilder& builder) {

builder.BuildRoom(l);

builder.BuildRoom(lOOl);

return builder.GetMaze(); }

 

 

, MazeBuilder , - .

. MazeBuilder.

StandardMazeBuilder . , _currentMaze:

 

 

class StandardMazeBuilder: public MazeBuilder {

public:

StandardMazeBuilder();

virtual void BuildMazeO;

virtual void BuildRoom(int);

virtual void BuildDoor(int, int);

virtual Maze* GetMazef();

private:

Direction CommonWall(Room*, Room*);

Maze* _currentMaze;

};

 

 

CommonWall ( ) - , .

StandardMazeBuilder _currentMaze:

 

 

StandardMazeBuilder::StandardMazeBuilder () {

_currentMaze = 0;

}

BuildMaze Maze, , , ( GetMaze):

 

 

void StandardMazeBuilder:: BuildMaze () {

_currentMaze = new Maze;

}

 

Maze* StandardMazeBuilder::GetMaze () {

return _currentMaze;

}

BuildRoom :

 

 

void StandardMazeBuilder::BuildRoom (int n) {

if (!_currentMaze->RoomNo(n)) {

Room* room = new Room(n);

_currentMaze->AddRoom(room);

room->SetSide(North, new Wal l);

room->SetSide(South, new Wal l);

room->SetSide(East, new Wal l);

room->SetSide(West, new Wall);

}

}

 

 

, StandardMazeBuilder :

 

 

void StandardMazeBuilder: rBuildDoor (int nl, int n2) {

Room* rl = _currentMaze->RoomNo (nl);

Room* r2 = _currentMaze->RoomNo (n2);

Door* d = new Door(rl, r2);

rl->SetSide(CommonWall(rl,r2), d);

r2->SetSide(CommonWall(r2,rl), d);

}

 

 

GreateMaze StandardMazeBuilder:

 

 

Maze* maze;

MazeGame game;

StandardMazeBuilder builder;

game. CreateMaze (builder);

maze = builder. GetMaze ();

 

 

StandardMazeBuilder Maze . Maze, , a StandardMazeBuilder Maze. , MazeBuilder, , .

MazeBuiIder CountingMazeBuiIder.

, , :

 

 

 

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;

};

 

 

,

MazeBuilder :

 

 

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;

}

CountingMazeBuilder:

 

 

int rooms, doors;

MazeGame game;

CountingMazeBuilder builder;

game.CreateMaze(builder);

buiIder.GetCount s(rooms, doors);

cout " "

rooms " "

doors " " endl;

 

 

 

 

  1. , Builder;
  2. CountingMazeBuilder ( ) .
  3. - MazeBuiIder CountingMazeBuilder.

 

1.

 

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

 



<== | ==>
| 
:


: 2015-11-23; !; : 377 |


:

:

, .
==> ...

1765 - | 1567 -


© 2015-2024 lektsii.org - -

: 0.186 .