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.

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?
  1. Public abstract class abstractperson{
  2. //Abstract class defines the entire process skeleton
  3. public void Preparegotoschool () {
  4. Dressup ();
  5. Eatbreakfast ();
  6. Takethings ();
  7. }
  8. //The following are specific steps for different subclasses to complete according to their own characteristics
  9. protected abstract void Dressup ();
  10. protected abstract void Eatbreakfast ();
  11. protected abstract void takethings ();
  12. }
Concrete Class Concreteclass[Java]View PlainCopyprint?
  1. Public class Student extends abstractperson{
  2. @Override
  3. protected void Dressup () {
  4. System.out.println ("Wear school uniforms");
  5. }
  6. @Override
  7. protected void Eatbreakfast () {
  8. System.out.println ("Eat Your Mother's Breakfast");
  9. }
  10. @Override
  11. protected void Takethings () {
  12. System.out.println ("Back schoolbag, with homework and red scarf");
  13. }
  14. }
[Java]View PlainCopyprint?
  1. Public class Teacher extends abstractperson{
  2. @Override
  3. protected void Dressup () {
  4. System.out.println ("wear overalls");
  5. }
  6. @Override
  7. protected void Eatbreakfast () {
  8. System.out.println ("Make breakfast, take care of the children to have breakfast");
  9. }
  10. @Override
  11. protected void Takethings () {
  12. System.out.println ("Bring the exam paper prepared last night");
  13. }
  14. }
[Java]View PlainCopyprint?
    1. public class  client {  
    2.      public  static void main (string[] args)  {   
    3.      student student =  new student ()   
    4.       Student.preparegotoschool ();   
    5.   
    6.       teacher teacher  = new teacher ()   
    7. Li class= "alt" >     teacher.preparegotoschool ();   
    8.       }  
    9. }  
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

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.