34.12. Materialization with the Template Method Pattern

If we were to program two or three mapper classes, some commonality in the code would become apparent. The basic repeating algorithm structure for materializing an object is:

						if (object in cache)
						return it
						else
						create the object from its representation in storage
						save object in cache
						return it
					

The point of variation is how the object is created from storage.

We will create the get method to be the template method in an abstract superclass AbstractPersistenceMapper that defines the template, and use a hook method in subclasses for the varying part. Figure 34.7 shows the essential design.

Figure 34.7. Template Method for mapper objects.

As shown in this example, it is common for the template ...

Get Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and the Unified Process, Second Edition 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.