Java design Pattern (16)--template method pattern for behavioral patterns

Source: Internet
Author: User

first, the concept

  Concept

  

UML diagram

  

   Role

  

   Usage Scenarios

  In fact, we often used abstract class is the pattern to use, so basically belong to a better understanding of a pattern.

Second, practice

  According to the above role, give the corresponding code

 Abstract templates

/*** Abstract Template * *@authorAdministrator **/ Public Abstract classAbstracttemplate {/*** Declaration and implementation of template methods*/     Public voidTemplatemethod () {//calling methods implemented by subclassesOperation1 ();        Operation2 (); //call a method that has already been implementedOperation3 (); }     Public Abstract voidOperation1 ();  Public Abstract voidOperation2 (); Private Final voidOperation3 () {//has been implemented    }}

  Specific templates

/*** Specific Template * *@authorAdministrator **/ Public classConcretetemplateextendsabstracttemplate{@Override Public voidOperation1 () {//Basic Method Implementation} @Override Public voidOperation2 () {//Basic Method Implementation    }}

Java design Pattern (16)--template method pattern for behavioral 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.