10

The Prototype Pattern

The Prototype pattern is used when creating an instance of a class is very time consuming or complex in some way. Instead of creating more instances, you make copies of the original instance and modify the copies as appropriate.

Prototypes can also be used whenever you need classes that differ only in the type of processing they offer—for example, in parsing strings that represent numbers in different radixes.

Let’s consider the case of an extensive database in which you need to make a number of queries to construct an answer. When you have this answer as a table or ResultSet, you might want to manipulate it to produce other answers without issuing additional queries.

In a case like one we have been working on, let’s ...

Get Python Programming with Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.