Template method mode for Java design patterns

Source: Internet
Author: User

This article continues to introduce the template method pattern of 23 design pattern series.

OverviewThe 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.
roles in the patternabstract Class (AbstractClass): 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.
ApplicationFor 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
Public abstract class abstractperson{     //Abstract classes define 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
public class Student extends abstractperson{     @Override     protected void Dressup () {          System.out.println ("Wear school uniforms ");     }     @Override     protected void Eatbreakfast () {          System.out.println ("Eat Mother's Breakfast");     }     @Override     protected void takethings () {          System.out.println ("Back schoolbag, bring homework and red scarf");}     
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 ("With the examination papers prepared last Night");}     
public class Client {public     static void Main (string[] args) {     Student Student = new Student ()     Student.prep Aregotoschool ();     Teacher Teacher  = new Teacher ()     teacher.preparegotoschool ();}     }

AdvantagesThe 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.
DisadvantagesEach different implementation needs to define a subclass, which causes the number of classes to increase and the design to be more abstract.
Applicable ScenariosIn some classes of algorithms, the same method is used, 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)

Recommended sweep code pay attention to the public number, to add color to life


Template method mode for Java design patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.