This article continues to introduce the template method pattern of 23 design pattern series.
The overview template method pattern is the behavior pattern of the class. Prepare an abstract class, implement some of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the intent of the template method pattern. For example, the skeleton of an algorithm in an operation is defined, and the steps are deferred to the subclass. The template method allows subclasses to redefine some specific steps of an algorithm without altering the structure of an algorithm. The Role abstract class (AbstractClass) in the pattern: the template method is implemented, and the skeleton of the algorithm is defined. Concrete Class (Concreteclass): Implements the abstract method in the abstract class, has completed the complete algorithm. Application for example, to prepare to go to school to do the work (Preparegotoschool) as an example, suppose it takes three steps: Dress (dressup), eat breakfast (eatbreakfast), Bring something (takethings). There must be a difference between what students and teachers do. Abstract class AbstractClass[Java]View PlainCopy print?
- Public abstract class abstractperson{
- //Abstract class defines the entire process skeleton
- public void Preparegotoschool () {
- Dressup ();
- Eatbreakfast ();
- Takethings ();
- }
- //The following are specific steps for different subclasses to complete according to their own characteristics
- protected abstract void Dressup ();
- protected abstract void Eatbreakfast ();
- protected abstract void takethings ();
- }
Concrete Class Concreteclass[Java]View PlainCopyprint?
- Public class Student extends abstractperson{
- @Override
- protected void Dressup () {
- System.out.println ("Wear school uniforms");
- }
- @Override
- protected void Eatbreakfast () {
- System.out.println ("Eat Your Mother's Breakfast");
- }
- @Override
- protected void Takethings () {
- System.out.println ("Back schoolbag, with homework and red scarf");
- }
- }
[Java]View PlainCopyprint?
- Public class Teacher extends abstractperson{
- @Override
- protected void Dressup () {
- System.out.println ("wear overalls");
- }
- @Override
- protected void Eatbreakfast () {
- System.out.println ("Make breakfast, take care of the children to have breakfast");
- }
- @Override
- protected void Takethings () {
- System.out.println ("Bring the exam paper prepared last night");
- }
- }
[Java]View PlainCopyprint?
- public class client {
- public static void main (string[] args) {
- student student = new student ()
- Student.preparegotoschool ();
-   
- teacher teacher = new teacher ()
Li class= "alt" > teacher.preparegotoschool ();
- }
- }  
The advantage Template method pattern removes duplicate code from subclasses by moving the invariant behavior to the superclass. Subclasses implement some of the details of the algorithm, which facilitates the expansion of the algorithm. By invoking the subclass implementation of a parent class, the new behavior is added through the subclass extension, which conforms to the open-close principle. Disadvantages each of the different implementations needs to define a subclass, which causes the number of classes to increase and the design to be more abstract. Applicable scenarios in some classes of algorithms, using the same method, resulting in code duplication. Control subclass extensions, subclasses must adhere to algorithmic rules.
More Design Patterns: 23 design Mode Series
jason0539
Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)
Template method mode for Java design patterns