---
title: Prototype pattern
slug: prototype-pattern
qid: 
snapshot: enwiki-001328167436
content_hash: 
license: CC BY-SA 4.0
source: https://en.wikipedia.org/w/index.php?oldid=1328167436
tags: [22922585, 23236616, 23603337, 24043274, 24745121, 26157654, 26220375, 26748027, 27695885]
---

# Prototype pattern

> [!note] Provenance
> Snapshot `enwiki-001328167436` · content root `…`
> Derived from <https://en.wikipedia.org/w/index.php?oldid=1328167436> · Licensed CC BY-SA 4.0

The prototype pattern is a creational design pattern in software development. It is used when the types of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to avoid subclasses of an object creator in the client application, like the factory method pattern does, and to avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application. To implement the pattern, the client declares an abstract base class that specifies a pure virtual clone() method. Any class that needs a "polymorphic constructor" capability derives itself from the abstract base class, and implements the clone() operation. The client, instead of writing code that invokes the "new" operator on a hard-coded class name, calls the clone() method on the prototype, calls a factory method with a parameter designating the particular concrete derived class desired, or invokes the clone() method through some mechanism provided by another design pattern. The mitotic division of a cell — resulting in two identical cells — is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself.

The prototype pattern is a creational design pattern in software development. It is used when the types of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to avoid subclasses of an object creator in the client application, like the factory method pattern does, and to avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application. To implement the pattern, the client declares an abstract base class that specifies a pure virtual clone() method. Any class that needs a "polymorphic constructor" capability derives itself from the abstract base class, and implements the clone() operation. The client, instead of writing code that invokes the "new" operator on a hard-coded class name, calls the clone() method on the prototype, calls a factory method with a parameter designating the particular concrete derived class desired, or invokes the clone() method through some mechanism provided by another design pattern. The mitotic division of a cell — resulting in two identical cells — is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself. ==Overview== The prototype design pattern is one of the 23 Gang of Four design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse. The prototype design pattern solves problems like: How can objects be created so that the specific type of object can be determined at runtime? How can dynamically loaded classes be instantiated? Creating objects directly within the class that requires (uses) the objects is inflexible because it commits the class to particular objects at compile-time and makes it impossible to specify which objects to create at run-time. The prototype design pattern describes how to solve such problems: Define a Prototype object that returns a copy of itself. Create new objects by copying a Prototype object. This enables configuration of a class with different Prototype objects, which are copied to create new objects, and even more, Prototype objects can be added and removed at run-time. See also the UML class and sequence diagram below. ==Structure== import std; using std::array; using std::shared_ptr; using std::unique_ptr; using std::vector; enum class Direction: char { NORTH, SOUTH, EAST, WEST }; class MapSite { public: virtual void enter() = 0; virtual unique_ptr clone() const = 0; virtual ~MapSite() = default; }; class Room: public MapSite { private: int roomNumber; shared_ptr , 4>> sides; public: explicit Room(int n = 0): roomNumber{n}, sides{std::make_shared , 4>>()} {} ~Room() = default; Room& setSide(Direction d, shared_ptr ms) { (*sides)[static_cast (d)] = std::move(ms); std::println("Room::setSide {} ms", d); return *this; } virtual void enter() override {} virtual unique_ptr clone() const override { return std::make_unique (*this); } Room(const Room&) = delete; Room& operator=(const Room&) = delete; }; class Wall: public MapSite { public: Wall(): MapSite() {} ~Wall() = default; virtual void enter() override {} nodiscard virtual unique_ptr clone() const override { return std::make_unique (*this); } }; class Door: public MapSite { private: shared_ptr room1; shared_ptr room2; public: explicit Door(shared_ptr r1 = nullptr, shared_ptr r2 = nullptr): MapSite(), room1{std::move(r1)}, room2{std::move(r2)} {} ~Door() = default; virtual void enter() override {} nodiscard virtual unique_ptr clone() const override { return std::make_unique (*this); } void initialize(shared_ptr r1, shared_ptr r2) { room1 = std::move(r1); room2 = std::move(r2); } Door(const Door&) = delete; Door& operator=(const Door&) = delete; }; class Maze { private: vector > rooms; public: Maze() = default; ~Maze() = default; Maze& addRoom(shared_ptr r) { std::println("Maze::addRoom {}", reinterpret_cast (r.get())); rooms.push_back(std::move(r)); return *this; } nodiscard shared_ptr roomNo(int n) const { for (const Room& r: rooms) { // actual lookup logic here... } return nullptr; } nodiscard virtual unique_ptr clone() const { return std::make_unique (*this); } }; class MazeFactory { public: MazeFactory() = default; virtual ~MazeFactory() = default; nodiscard virtual unique_ptr makeMaze() const { return std::make_unique (); } nodiscard virtual shared_ptr makeWall() const { return std::make_shared (); } nodiscard virtual shared_ptr makeRoom(int n) const { return std::make_shared (n); } nodiscard virtual shared_ptr makeDoor(shared_ptr r1, shared_ptr r2) const { return std::make_shared (std::move(r1), std::move(r2)); } }; class MazePrototypeFactory: public MazeFactory { private: unique_ptr prototypeMaze; shared_ptr prototypeRoom; shared_ptr prototypeWall; shared_ptr prototypeDoor; public: MazePrototypeFactory(unique_ptr m, shared_ptr w, shared_ptr r, shared_ptr d): MazeFactory(), prototypeMaze{std::move(m)}, prototypeRoom{std::move(r)}, prototypeWall{std::move(w)}, prototypeDoor{std::move(d)} {} ~MazePrototypeFactory() = default; virtual unique_ptr makeMaze() const override { return prototypeMaze->clone(); } nodiscard virtual shared_ptr makeRoom(int n) const override { return prototypeRoom->clone(); } nodiscard virtual shared_ptr makeWall() const override { return prototypeWall->clone(); } nodiscard virtual shared_ptr makeDoor(shared_ptr r1, shared_ptr r2) const override { shared_ptr door = prototypeDoor->clone(); door->initialize(std::move(r1), std::move(r2)); return door; } MazePrototypeFactory(const MazePrototypeFactory&) = delete; MazePrototypeFactory& operator=(const MazePrototypeFactory&) = delete; }; class MazeGame { public: MazeGame() = default; ~MazeGame() = default; nodiscard unique_ptr createMaze(MazePrototypeFactory& factory) { unique_ptr maze = factory.makeMaze(); shared_ptr r1 = factory.makeRoom(1); shared_ptr r2 = factory.makeRoom(2); shared_ptr door = factory.makeDoor(r1, r2); maze->addRoom(std::move(r1)) .addRoom(std::move(r2)); r1->setSide(Direction::NORTH, factory.makeWall()) .setSide(Direction::EAST, door) .setSide(Direction::SOUTH, factory.makeWall()) .setSide(Direction::WEST, factory.makeWall()); r2->setSide(Direction::NORTH, factory.makeWall()) .setSide(Direction::EAST, factory.makeWall()) .setSide(Direction::SOUTH, factory.makeWall()) .setSide(Direction::WEST, door); return maze; } }; int main(int argc, char* argv[]) { MazeGame game; MazePrototypeFactory simpleMazeFactory( std::make_unique (), std::make_shared (), std::make_shared (0), std::make_shared () ); unique_ptr maze = game.createMaze(simpleMazeFactory); } The program output is: 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
