Template mode of design pattern

Source: Internet
Author: User

Template method Mode:defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. This pattern allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. Usage scenarios:1, one-time implementation of an invariant part of the algorithm, and the variable behavior left to the subclass to achieve. 2 . The public behavior of each subclass should be extracted and centralized into a common parent class to avoid code duplication. That is , " re-decomposition to generalize , " first identifying the differences in existing code , and separating the differences into new operations. Finally, replace these different code with a template method that invokes these new operations. 3, control sub-class extension. The template method invokes thehookmethodaction only at a specific point, allowing the extension to be made only at those points. The general class diagram is as follows:

Our class diagram here is a bit more detailed, as follows:

The specific code is implemented as follows:

Abstract classAbstractmethod {

protected abstract void method1 ();
protected abstract void method2 ();

//Template method, unified invocation of the above two methods that will be implemented in subclasses, that is, different implementations  
Public final void Templatemethod () {
/*
* This method can also be used in the body of the "hook method" to achieve the call of different methods according to certain circumstances combination
*/ 
    < Span style= "color: #0000ff;" >if   (Hookmethod ()) { 
      method1 (); &NBSP;
    }else  {&NBSP;
       method1 (); &NBSP;
      method2 (); &NBSP;
    }     &NBSP;
  }&NBSP;

  //hook method, protected permissions can be overridden by the quilt class, default to return True  
protectedBooleanHookmethod () {
returntrue;
}
}

classConcretemethodaextendsabstractmethod{
protectedvoidMethod1 () {
System.out.println ("Subclass A:method1 () ...");
}
protectedvoidMethod2 () {
System.out.println ("Subclass A:method2 () ...");
}
}

classConcretemethodbextendsabstractmethod{
protected void  method1 () {&NBSP;
     system.out.println (); &NBSP;
  }&NBSP;
  protected  void  method2 () {&NBSP;
     system.out.println (); &NBSP;
  }&NBSP;
  //override hook method  
protectedBooleanHookmethod () {
returnfalse;
}
}

PublicclassClient {
Public Staticvoid main (string[] args) {&NBSP;
    abstractmethod MethodA =  new  concretemethoda (); &NBSP;
    methoda.templatemethod (); &NBSP;
    &NBSP;
     abstractmethod methodb=  new  concretemethodb (); &NBSP;
    methodb.templatemethod (); &NBSP;
  }&NBSP;
}

Test results:
sub-class a:method1 () ... sub-class b:method1 () ... sub-class b:method2 () ...
At this point, the template method pattern is simply implemented, and we understand that abstract classesAbstactmethodthe template method in actually provides an externally accessible interface that the external environment enters into for service, andAbstractmethodclass here will potentially provide different services for the encapsulation to hide, that is, to defer the details of some specific services, by its subclasses responsible for implementation. In addition, the rational use of the hook method can also make the template method mode more flexible, not beTemplatemethod ()simply Limited, see the code implementation and test results for the example above.

Design mode template mode (GO)

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.