Design Pattern, Software Development

The Prototype Design Pattern In Simple Words

The Prototype design pattern creates an object that serves as a template when we need copies of it. Then, we slightly modify these copies at runtime by giving them new properties and behaviors. Unlike other design patterns, our usage of the Prototype pattern focuses more on creating copies of an object and using them.

Sample Pattern Use Cases

When do we use this pattern? One use case could be an email sending facility of an application. Instead of creating new emails from scratch, we have a single object of an email template. Then, we make copies of that email template object and modify its content at run-time before we send them. For example, we can set its recipients, format and replace placeholders in the email body with appropriate values.

There may be reasons why we do not want to create emails from scratch. One reason could be that building an email object is expensive and takes more time than required. Another reason could be to ensure we have an immutable base configuration for all email objects. Moreover, any changes to the base configuration reflect only after an application restarts.

The Prototype Pattern Off The Gang of Four

What does the Gang of Four tell about the Prototype design pattern?

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

Prototype objects are unique because we use them as templates, and they exist as prototypes. Then, we use them to create new objects with the same properties and behaviors. Although we still create new objects of the same type as the prototype objects, we give them their initial properties and behaviors from the existing prototype objects. After that, we modify the copies for their purposes.

Other Design Patterns

The Prototype design pattern is only one of the creational design patterns for developing maintainable software applications. Other design patterns are available on Design Patterns Are The Ultimate Toolkit For Programmers.

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like