Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Abstract factory pattern
(section)
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>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)