Java design pattern--Behavioral pattern--template method

Source: Internet
Author: User

What is the template method, which needs to be considered, look below:

  

1 Template Method2 Overview3 defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. 4     5 Templatemethod allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. 6  7  8 Applicability91One-time implementation of an invariant part of an algorithm, and the variable behavior is left to subclasses to implement. Ten  One2The public behavior in each subclass should be extracted and centralized into a common parent class to avoid code duplication.  A first identify the differences in the existing code and separate the differences into new operations.  - Finally, replace these different code with a template method that invokes these new operations.  -  the3. Control the subclass extension.  -   -   - participants +1. AbstractClass - defines the abstract primitive operations (primitiveoperation), in which subclasses redefine them to implement the steps of an algorithm.  + implement a template method that defines the skeleton of an algorithm.  A the template method not only invokes primitive operations, it also invokes operations defined in AbstractClass or other objects.  at  -2. Concreteclass -Implements primitive operations to complete the steps associated with a particular subclass in the algorithm.

Test class:

 1  public  class   Test { 2   public  static  void   main (string[] args) { 4  Temp Late temp = new   Templateconcrete ();  5   Temp.update ();  6   7  }
 public  abstract  class   Template { public  abstract  void   print ();  public  void   update () {System.out.println ( "Start printing" );  for  (int  i = 0; i <; I++) {print (); }    }}
1  Public class extends Template {23    @Override4public     void print () {5         System.out.println ("This is the implementation of subclasses"); 6     }7 }

Description: Small series think this kind of template method limits the subclass extension.

Java design pattern--Behavioral pattern--template method

Related Article

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.