avatarItchimonji

Summary

The Prototype design pattern is a creational pattern that facilitates the creation of new objects by copying an existing instance, allowing for runtime flexibility and reduction of concrete class declarations.

Abstract

The Prototype pattern is a design strategy that simplifies object creation by cloning a pre-existing prototype instance, thus avoiding the need for new class declarations. This approach is particularly beneficial for dynamic loading scenarios where the classes to be instantiated are determined at runtime. It provides flexibility in adding and removing products, minimizes maintenance efforts, and reduces the reliance on factories. The pattern is demonstrated with an example in TypeScript, where the difference between simple and deep cloning is highlighted. Deep cloning, which copies both attributes and methods, can be achieved with a combination of Object.assign and Object.create, or by using a utility like Lodash's cloneDeep. This pattern is part of a series on design patterns, which are essential tools for developers to solve common design problems, ensure consistent communication, and introduce principles like object composition and dependency inversion.

Opinions

  • The Prototype pattern is preferable over factories when dealing with a large number of variants, as it minimizes the need for concrete classes and maintenance.
  • Deep cloning is recommended for use cases that require copying both the attributes and methods of an object.
  • The pattern is highly flexible, allowing for the addition or removal of product variants at runtime, which is not as readily achievable with other creational patterns.
  • Design Patterns are considered crucial for developers, as they provide a shared language for discussing system design and are foundational for understanding object composition and dependency inversion.
  • The Prototype pattern encourages a shift from inheritance-based design to object composition, which can lead to more maintainable and flexible code.
  • Utilizing libraries like Lodash can simplify the implementation of complex cloning operations, making the Prototype pattern easier to apply in practice.

Prototype | Cheat Sheet

Creational Pattern — Design Patterns Series

The Prototype pattern is also known as Cloning. It creates new objects by copying a prototypical instance. The new object differs only in few things from their predecessors, so it is unnecessary to declare concrete classes. This leads to a class mess.

Product addition and removal at runtime enables more flexibility to add new variants in the form of classes, minimizes maintenance effort, and reduces the need for factories.

Real-life examples

  • Cell division
  • Product manufacturing: new products often differ from their predecessors in only a few respects (cell phones, tablets, laptops, …)
  • Car production: with different extras (color, interior, rims, …)

Meaning

  • Specification of a prototypical instance
  • Creation of new objects by copying the prototype

Applicability

  • Classes to be instantiated are specified only at runtime (dynamic loading)
  • Avoidance of factories, because otherwise there are too many concrete classes
  • Instances have few state combinations

Assets and Drawbacks

  • Product addition and removal at runtime (more flexibility than other Creational Patterns)
  • Specification of new objects by means of value variation (and thus reduction of concrete classes)
  • Reduced subclassing

Example

Depending on your programming language or framework, there are differences between simple cloning and deep cloning. Simple cloning enables only copying attributes/properties. However, deep cloning makes it possible to copy attributes and methods. So, be sure you use the right one for your use case.

Deep cloning in TypeScript can be realized by the combination of Object.assign and Object.create. Alternatively you can use Lodash.

Car class that can clone itself

The Prototype interface and concrete class look very simple.

Prototype interface

The client can copy an old car easily and change some properties at the new one.

Client clones a car

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and are a significant introduction into object composition (besides inheritance) and dependency inversion.

Creational Patterns are indispensable when abstracting the instantiation process of objects. Of high importance are encapsulation of concrete classes and hiding the creational process.

GitHub

Learn More

Resources

Design Patterns
Prototype
Creational Patterns
Cheatsheet
Object Composition
Recommended from ReadMedium