Design pattern-Template Method mode

Source: Internet
Author: User

Design pattern-Template method pattern

Reading notes are not original, summary:

Template method Pattern Definition

Define The skeleton of an algorithm in a operation,deferring some steps to Subclasses,template Method lets subclasses red Efine certain steps of an algorithm without changing the algorithm ' s structure.
Template method Pattern: Defines the skeleton of an algorithm in a method, and delays some steps into subclasses. The template method allows subclasses to redefine some of the steps in the algorithm without changing the structure of the algorithm.

Template Method Mode further introduction

The template pattern is really simple and uses only the Java integration mechanism, but it is a very wide-ranging pattern. Among them, Absstractclass (in the code below) is called abstract template, its methods are divided into two categories;

    • Basic Methods
      The basic method is also called the basic operation, which is the method implemented by the subclass, and the template method is called.

    • Template Method
      Can have one or several, is generally a concrete method, that is, a framework to implement the basic method of scheduling, complete the fixed logic.

    • Code Listing Abstract template class

public abstract class  abstractclass  {//Basic method protected  abstract  void  dosomething  Span class= "Hljs-params" > () ; Basic method protected  abstract  void  doanything   () ; Template method public  void   Templatemethod   ()  {//Call the basic method, complete the related logic dosometh ing   () ; doanything   () ; }
    • Code Listing Concrete Template classes
Publicclass ConcreteClass1 extends AbstractClass{    @Override    protected void dosomething(){//Business logic processing} @Override    protected void doanything(){//Business logic Processing}} Public class ConcreteClass2 extends AbstractClass{    @Override    protected void dosomething(){//Business logic processing} @Override    protected void doanything(){//Business logic Processing}}
    • Code Listing Scenario Classes
Publicclass  client  {public  static  void  Span class= "Hljs-title" >main   (String [] args)  { class1  = new  concreteclass1   () ; abstractclass  class2  = new      ConcreteClass2   () ; Call the template method class1 . templatemethod      () ; class2 . templatemethod   () ;}} 

Design mode-Template method mode

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.