Java Design Pattern Template Method (template Method), design pattern Template
This article continues to introduce the template method modes of the 23 design patterns series.
Overview The template method mode is the behavior mode of the class. Prepare an abstract class, implement part of the logic in the form of a specific method and a specific constructor, and then declare some abstract methods to force the subclass to implement the remaining logic. Different sub-classes can implement these abstract methods in different ways to implement the rest logic differently. This is the intention of the template method mode. For example, define the skeleton of an algorithm in an operation and delay the step to the subclass. The template method allows subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
Role abstract class in the mode: implements the template method and defines the skeleton of the algorithm. ConcreteClass: implements the abstract methods in the abstract class and completes the complete algorithm.
For example, taking prepareGotoSchool as an example, it takes three steps: dress up and eatBreakfast ), takeThings ). There must be some difference between students and teachers in doing specific things. Abstract class AbstractClass
Public abstract class AbstractPerson {// abstract class defines the entire process skeleton public void prepareGotoSchool () {dressUp (); eatBreakfast (); takeThings ();} // The following are detailed steps for different sub-classes based on their own characteristics: protected abstract void dressUp (); protected abstract void eatBreakfast (); protected abstract void takeThings ();}
ConcreteClass
Public class Student extends AbstractPerson {@ Override protected void dressUp () {System. out. println ("wear school uniform") ;}@ Override protected void eatBreakfast () {System. out. println ("breakfast prepared by mom");} @ Override protected void takeThings () {System. out. println ("carrying a schoolbag, carrying homework and a red scarf ");}}
Public class Teacher extends actperson {@ Override protected void dressUp () {System. out. println ("wear overalls") ;}@ Override protected void eatBreakfast () {System. out. println ("cook breakfast, take care of your children for breakfast") ;}@ Override protected void takeThings () {System. out. println ("bringing the exam prepared last night ");}}
public class Client { public static void main(String[] args) { Student student = new Student() student.prepareGotoSchool(); Teacher teacher = new Teacher() teacher.prepareGotoSchool(); }}
The advantage template method removes repeated code in the subclass by moving the unchanged behavior to the superclass. Sub-classes implement some details of the algorithm, which is helpful for algorithm expansion. The Operation implemented by a subclass is called by a parent class, and new behaviors are added through the subclass extension, which complies with the "open-closed principle ".
Disadvantages each implementation must define a subclass, which leads to an increase in the number of classes and a more abstract design.
Applicable scenarios use the same method in some class algorithms, resulting in code duplication. Control subclass extension. Subclass must comply with algorithm rules.
More design patterns: 23 Design Patterns
Author: jason0539
Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)
It is recommended to scan the QR code to pay attention to the public account and add some color to your life.