Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Prototype pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Example == === C++11 Example === This [[C++23]] implementation is based on the pre-C++98 implementation in the book. Discussion of the design pattern along with a complete illustrative example implementation using polymorphic class design are provided in the [https://fbb-git.gitlab.io/cppannotations/cppannotations/html/cplusplus14.html#l318 C++ Annotations]. <syntaxhighlight lang="c++"> import std; enum class Direction {North, South, East, West}; class MapSite { public: virtual void enter() = 0; virtual MapSite* clone() const = 0; virtual ~MapSite() = default; }; class Room : public MapSite { public: Room(): roomNumber(0) {} Room(int n): roomNumber(n) {} void setSide(Direction d, MapSite* ms) { std::println("Room::setSide {} ms", d); } virtual void enter() {} virtual Room* clone() const { // implements an operation for cloning itself. return new Room(*this); } Room& operator=(const Room&) = delete; private: int roomNumber; }; class Wall: public MapSite { public: Wall() {} virtual void enter() {} virtual Wall* clone() const { return new Wall(*this); } }; class Door: public MapSite { public: Door(Room* r1 = nullptr, Room* r2 = nullptr) :room1(r1), room2(r2) {} Door(const Door& other) :room1(other.room1), room2(other.room2) {} virtual void enter() {} virtual Door* clone() const { return new Door(*this); } virtual void initialize(Room* r1, Room* r2) { room1 = r1; room2 = r2; } Door& operator=(const Door&) = delete; private: Room* room1; Room* room2; }; class Maze { public: void addRoom(Room* r) { std::println("Maze::addRoom {}", r); } Room* roomNo(int) const { return nullptr; } virtual Maze* clone() const { return new Maze(*this); } virtual ~Maze() = default; }; class MazeFactory { public: MazeFactory() = default; virtual ~MazeFactory() = default; virtual Maze* makeMaze() const { return new Maze; } virtual Wall* makeWall() const { return new Wall; } virtual Room* makeRoom(int n) const { return new Room(n); } virtual Door* makeDoor(Room* r1, Room* r2) const { return new Door(r1, r2); } }; class MazePrototypeFactory: public MazeFactory { public: MazePrototypeFactory(Maze* m, Wall* w, Room* r, Door* d): prototypeMaze(m), prototypeRoom(r), prototypeWall(w), prototypeDoor(d) {} virtual Maze* makeMaze() const { // creates a new object by asking a prototype to clone itself. return prototypeMaze->clone(); } virtual Room* makeRoom(int) const { return prototypeRoom->clone(); } virtual Wall* makeWall() const { return prototypeWall->clone(); } virtual Door* makeDoor(Room* r1, Room* r2) const { Door* door = prototypeDoor->clone(); door->initialize(r1, r2); return door; } MazePrototypeFactory(const MazePrototypeFactory&) = delete; MazePrototypeFactory& operator=(const MazePrototypeFactory&) = delete; private: Maze* prototypeMaze; Room* prototypeRoom; Wall* prototypeWall; Door* prototypeDoor; }; // If createMaze is parameterized by various prototypical room, door, and wall objects, which it then copies and adds to the maze, then you can change the maze's composition by replacing these prototypical objects with different ones. This is an example of the Prototype (133) pattern. class MazeGame { public: Maze* createMaze(MazePrototypeFactory& m) { Maze* aMaze = m.makeMaze(); Room* r1 = m.makeRoom(1); Room* r2 = m.makeRoom(2); Door* theDoor = m.makeDoor(r1, r2); aMaze->addRoom(r1); aMaze->addRoom(r2); r1->setSide(Direction::North, m.makeWall()); r1->setSide(Direction::East, theDoor); r1->setSide(Direction::South, m.makeWall()); r1->setSide(Direction::West, m.makeWall()); r2->setSide(Direction::North, m.makeWall()); r2->setSide(Direction::East, m.makeWall()); r2->setSide(Direction::South, m.makeWall()); r2->setSide(Direction::West, theDoor); return aMaze; } }; int main() { MazeGame game; MazePrototypeFactory simpleMazeFactory(new Maze, new Wall, new Room, new Door); game.createMaze(simpleMazeFactory); } </syntaxhighlight> The program output is: <syntaxhighlight lang="c++"> Maze::addRoom 0x1160f50 Maze::addRoom 0x1160f70 Room::setSide 0 0x11613c0 Room::setSide 2 0x1160f90 Room::setSide 1 0x11613e0 Room::setSide 3 0x1161400 Room::setSide 0 0x1161420 Room::setSide 2 0x1161440 Room::setSide 1 0x1161460 Room::setSide 3 0x1160f90 </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Prototype pattern
(section)
Add topic