Author image

Prototype Design Pattern


Difficulty:
1/5


Implementing Prototype Design Pattern like a boss and then laughing it out.
Bonus: lewd way of spawning arrays of objects from a prototypical instance.

The prototype pattern is used when the type of object(s) 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, like the factory pattern does
  • avoid the inherent cost of creating a new object in the standard way (like using new) when can be prohibitively expensive for a given application

Design

  1. define a Prototype class with a clone() method that returns a copy of itself
  2. create subclasses of Prototype which describe the concrete types. Implement the clone method (and whichever other method you want of course)
  3. create a Spawner/Factory class with a private Prototype pointer (sort of like the Queen bee who never leaves the hive) and a spawnObject() method. The Spawner class resembles a PrototypeFactory class
  4. in the Spawner class call the prototype's clone method to return a copy of the prototype object based on the concrete subobject.

Notice there is a 1-to-1 relationship between a prototype and a factory/spawner. So create as many prototypical objects and their respective factory/spawner objects as the concrete subclasses you want to create.

Prototype objects can be added and removed at run-time. One neat aspect about this pattern is that it doesn't just clone the class of the prototype, it clones its state too. This means we could make a spawner for fast ghosts, weak ghosts, or slow ghosts just by creating an appropriate prototype Ghost object.

I used Windows 8.1 x86_64, Visual Studio 2017, C++17 to build the project.

Github

Github repository link.


0 likes