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
Abstract factory 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 == This [[C++23]] implementation is based on the pre-C++98 implementation in the book. <syntaxhighlight lang="c++"> import std; enum class Direction {North, South, East, West}; class MapSite { public: virtual void enter() = 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() {} Room(const Room&) = delete; // rule of three Room& operator=(const Room&) = delete; private: int roomNumber; }; class Wall: public MapSite { public: Wall() {} virtual void enter() {} }; class Door: public MapSite { public: Door(Room* r1 = nullptr, Room* r2 = nullptr): room1(r1), room2(r2) {} virtual void enter() {} Door(const Door&) = delete; // rule of three 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; } }; 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); } }; // If createMaze is passed an object as a parameter to use to create rooms, walls, and doors, then you can change the classes of rooms, walls, and doors by passing a different parameter. This is an example of the Abstract Factory (99) pattern. class MazeGame { public: Maze* createMaze(MazeFactory& factory) { Maze* 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; } }; int main() { MazeGame game; MazeFactory factory; game.createMaze(factory); } </syntaxhighlight> The program output is: <syntaxhighlight lang="output"> Maze::addRoom 0x1317ed0 Maze::addRoom 0x1317ef0 Room::setSide 0 0x1318340 Room::setSide 2 0x1317f10 Room::setSide 1 0x1318360 Room::setSide 3 0x1318380 Room::setSide 0 0x13183a0 Room::setSide 2 0x13183c0 Room::setSide 1 0x13183e0 Room::setSide 3 0x1317f10 </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
Abstract factory pattern
(section)
Add topic