Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Template Method

Pattern Properties

Type: Behavioral

Level: Object

Purpose

To provide a method that allows subclasses to override parts of the method without rewriting it.

Introduction

When working with projects, you frequently need to estimate the expense involved in performing a certain task or producing a deliverable. The Personal Information Manager uses a number of classes to represent its projects. At a minimum, the Task and Deliverable classes would be used to represent project elements. As a project grew more complex, you might create additional classes like Project or DependentTask to satisfy more sophisticated modeling needs.

While it is possible to create a getCostEstimate method for each of the classes, such an approach involves a lot of code duplication. As the number of classes increases, it becomes more and more difficult to maintain the code in all of the project classes.

A better approach is to group all of the project-related classes under a superclass, and define the method getCostEstimate there. But what would you do if parts of the getCostEstimate method depended on information that was specific to each of the Project classes? What if the Task had a different way of calculating hours from the Deliverable?

In that case, define getCostEstimate so that it calls an abstract method, getTimeRequired, and allows the Task and Deliverable classes to define the method as appropriate. This approach, called the Template Method, provides the benefits of code reusability, while still allowing classes to modify certain parts of the behavior to meet their needs.

Applicability

Use the Template Method pattern:

To provide a skeleton structure for a method, allowing subclasses to redefine specific parts of the method.

To centralize pieces of a method that are defined in all subtypes of a class, but which always have a small difference in each subclass.

To control which operations subclasses are required to override.

Description

When you are building complex class hierarchies for your application, code is often duplicated at several places. This is undesirable, since you want to reuse as much code as you can. Refactoring your code so that the common methods are in a superclass is a step in the right direction. The problem is that sometimes an operation that has been refactored relies on specific information that is only available in a subclass. Because of this, developers often decide not to refactor and accept the duplicate code in multiple subclasses.

When many methods in related classes have a similar structure, the Template Method can help. First, determine which parts of the method are similar. These parts should be centralized in the superclass, while the other operations should remain in the subclasses.

The newly defined template method contains the structure of the operation. For each part of the operation that can vary, an abstract method is defined in the superclass. Subclasses override these methods to provide their own implementation. When the template method is called on a subclass, the code in the superclass is executed.

When you make the template method in the superclass final, subclasses are limited in what parts of their superclass they can override.

93