Design Pattern _ template method pattern

Source: Internet
Author: User

Design Pattern _ template method pattern

Template Method Pattern
Define the skeleton of an algorithm in an operation, defering some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

Defines the framework of an algorithm for an operation, and delays some steps to the subclass. This allows subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.

 

Template

 

Public abstract class AbstractClass {// basic method protected abstract void doSomething (); // basic method protected abstract void doAnything (); // template method public void templateMethod () {/** call the basic method to complete the related logic */this. doAnything (); this. doSomething ();}}

 

Advantages encapsulation unchanged, scalable variable. Extract some common code for easy maintenance. The behavior is controlled by the parent class and implemented by the Child class.
There are multiple sub-classes in the application scenario. The logic is basically the same as the important and complex algorithms. The core algorithms are designed as templates, and the details are implemented by sub-classes.

 

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.