Template Method

Cocoa Design Pattern pdf  chapter 4

In many cases, an algorithm or process consists of several steps that are needed by many applicaions and one or more steps that may be unique to each applicaiton. The Template Method pattern implements the common steps within a reusable class while still enabling application-specific customizaion. (methods with the same name, but different implementation)

The Template Method pattern is also called the "Hollywood Pattern" because of the Hollywood cliche, "Don't call us; we'll call you." The Template Method pattern identifies one or more methods tath will be called automaticaaly as needed by existing code but should not be called directly by application code.

A Template Method is nothing more than a special cse of a method that is expected to be overridden in subclasses. The -dealloc method is a Template Method. When automatic garbage collection is not used, the -dealloc method must be overridden in each subclass of NSObject that needs to explicitly de-allocate previously allocated resources. The -dealloc method should almost never be called directly from code you write except to invoke the superclass' behavior from within an overridden implementation.         pp43

 

 

Motivation

Use the Template Method pattern to implement common algorithms or processed in a highly reusable way while stilll enabling customization of some process steps. Application programmers override Template Methods to customize an algorithm or process while benefiting from substantial reuse of code.

 

 

Soluion   pp44

不直接调用的方法 被其他代码自动调用。

 

Designing with Template Methods  pp46

To use the Template Method patttern in your own reusable classes, follow this design process:

1. Identify the steps of an algorithm and one or more methods to implement each step.

2. Implement the algorithm as a sequence of calls to the identified methods.

3. Factor out the cstomzable steps of the algorithm into Template Methods and provide a reasonable default implementation for each Templage Method.

4. Document whether the base class' implementation of each Template Method can, should, or must be called by subclasses that override them.

 

For example, -drawRect: is called by -display,  and the -drawRect is overriden by subclass for different customizations.

 

NSResponder, the superclass of NSView, provides the basic support for handling most user input events in Cocoa applications. It includes Template Methods that you override to control event handling and respond to user input events.Template Methods also customize management of the Responder Chain.

Whenever you use Apple's Cocoa documentation, keep an eye out for Template Methods. Apple doesn't often use the term Template in class reference documentation, but method descriptions for Template Methods usually contain a phrase such as "..overridden in subclasses to..." or "...subclasses can override..." along with information regarding whether your override can, should, or must call the superclass' implementation. Often there is also a warning that you should not directly call the method in question.

 

 

Consequences

Negative consequences to using the Template Method pattern.

1. the pattern requires the creation of subclasses to override template methods. Subclassing produces the tightest possible coupling between the subclass and its superclass. As always, it's advisable to avoid coupling. If overriding a Template Method is the only reation a subclass is created, there are almost certainly better patterns to apply. In particular, the Delegate pattern described in Chapter 15 os probably more suitable.

2. Documentaion for the intended use of Template Methods.When, can, should or must be overriden by subclasses.

3. If you need to subclass to customize an algorithm, what happens when seeral unrlated customizations are required? The Delegate Pattern in Chapter 15 exists in part to avoid the creation of a combinatorial number of subclasses needed to mix and match the different customizations an application might need.

 

If you find yourself contemplating the creation of Template Methods in code inteded for reuse, first onsider Delegates. In many cases, Dlegates provide a more flexible alternative to Template Methods.

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2013-02-18 23:00  Chansonyan  阅读(150)  评论(0)    收藏  举报

导航