Template Method

xiaoxiao2021-03-06  38

definition

Name: Template Method Structure: Intent: Define the skeleton of the algorithm in an operation, and delay some steps into the subclass. Template Method allows subclasses to redefine certain specific steps of the algorithm without changing the structure of an algorithm. Applicability: Distribute a constant portion of an algorithm in one time, and leave a variable behavior to subclasses. Public behavior in each subclass should be extracted and concentrated into a public parent class to avoid code repeat. This is a good example of "heavy decomposition" described by O P D Y K E and J O H N S O N [O J 9 3]. First identify the differences in the existing code and separately separate from the new operation. Finally, these different code is replaced with a template method that calls these new operations. Control subclass extension. The template method only calls the "H O K" operation at a particular point (see the effect section), which allows only to expand these points. effect:

Classic Case

1. The famous Framework "Hollywood US, We'll Call You) is the" reverse control "(Superclass call subclass) that is mainly supported by Template Method.

Best Practices

1,

Application Framework allows you to inherit from one or a series of classes, and create a new application, you can reuse most of the code of the class, and to reload certain methods in your own needs, Customization of the program. Template Method is a basic concept of the application framework, which is typically hidden behind (frame), drive applications by calling a group of methods (some methods you may have overloaded) by calling the base class.

An important feature of Template Method is:

It is defined in the base class and cannot be changed by (derived class). Sometimes it is private method, but in fact it is often declared as Final. It works by calling other base classes (overloaded), but it is often called as part of the initialization process, so it is not necessary to let client programmers can call it directly.

Related mode

1,

Reference article

1. Dialogue with prawns: Recognize the design pattern - TMPLATE METHOD / VISITOR

http://jjhou.9cbs.net/myan-deesign-patterns-big5.htm

2,

http://lcspace.nease.net/d-pattern/gof-tips2.xml

转载请注明原文地址:https://www.9cbs.com/read-75297.html

New Post(0)