1 The course explains
1.1 Type:
1.2 Definitions:
1.3 application scenarios:
1.4 Advantages:
1.5 Disadvantages:
1.6 Template Method expansion:
1.7 Relationship with other design patterns:
1 The course explains
1.1 Type:
Behavioral
1.2 Definitions:
◆ definitions: definition of a skeleton algorithms and allow subclasses to implement one or more steps to provide
◆ subclasses template method makes possible without changing the structure of the algorithm, some of the steps of the algorithm to redefine
Such as: a refrigerator door to open, b mounted to the refrigerator door elephant c
1.3 application scenarios:
◆ Each subclass common behavior is extracted and concentrated to a common parent class, so as to avoid duplication of code
◆ one-time realization of a portion of the same algorithm, and variable behavior left a subclass to achieve
For example: I open the refrigerator door and the refrigerator door shut common part of extracted, with some of the subclass inherits these templates, specific equipment elephant or tiger installed, implemented by these subclasses.
1.4 Advantages:
◆ improve reusability
◆ improve the scalability
◆ in line with the principle of opening and closing
1.5 Disadvantages:
◆ inheritance their shortcomings, if the parent class to add a new abstract method, all subclasses must be changed again
◆ increase the number of class
◆ increases the complexity of system implementation
1.6 Template Method expansion:
Hook method: Detail Reference Code
1.7 Relationship with other design patterns:
Template Method pattern and factory method:
Factory method is a special implementation template method.
Template Method and Strategy Mode:
Template Method pattern does not change the algorithm processes, strategic approach can change the algorithm processes, and between strategy is to be interchangeable.
Both calls have a packaging method. The method object of the policy is to model different algorithms can be replaced by each other, and does not affect the application layer of the client.
Template Method pattern is the definition of an algorithm for the process, not the same as some of the specific steps to subclasses to achieve,