Design Mode-template method mode

Source: Internet
Author: User

Design Mode-template method mode

Template Method: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. template Method let subclasses redefine certain steps of an algorithm without changing the algorithm's structure. defines the framework of an algorithm in an operation, and delays some steps to the subclass. This allows the subclass to redefine some specific steps of the Algorithm without changing the structure of an algorithm.

Two Roles in the template method mode:

Abstract Template: this role defines one or more Abstract operations for sub-classes to implement. These Abstract operations are basic operations, one or more template methods need to be defined. Generally, the specific method is used to define a framework.

A specific Template role (Concrete Template): implements one or more abstract methods defined in an abstract Template. Each abstract Template can have multiple Specific templates, each specific template role can provide different implementations of these abstract methods.

Class diagram of the template method mode:

Various implementation codes:

Abstract template class:

Package com. zz. template;/*** abstract template * Copyright May 15, 2015 * created by txxs * all right reserved */public abstract class AbstractClass {// basic method protected abstract void operation (); // template method public void templateMethod () {// call the basic method to complete the related logic this. operation ();}}
TEMPLATE 1:

 

 

Package com. zz. template; /*** specific template role 1 * Copyright May 15, 2015 * created by txxs * all right reserved */public class ConcreteClass1 extends AbstractClass {// basic implementation method @ Overrideprotected void operation () {// business logic processing }}

Template 2:

 

 

Package com. zz. template;/*** specific template class 2 * Copyright May 15, 2015 * created by txxs * all right reserved */public class ConcreteClass2 extends AbstractClass {// implement basic business method @ Overrideprotected void operation () {// business logic processing }}

Client:

 

 

Package com. zz. template;/*** test class * Copyright May 15, 2015 * created by txxs * all right reserved */public class Client {public static void main (String [] args) {AbstractClass class1 = new ConcreteClass1 (); AbstractClass class2 = new ConcreteClass2 (); // call the template method class1.templateMethod (); class2.templateMethod ();}}

Advantages of the template method mode:

 

1. encapsulate the unchanged part and expand the variable part. The unchanged part can be encapsulated into the parent class, and the variable part can be extended through inheritance.

2. Extracting public code is conducive to maintenance. Put the public code in the parent class. during maintenance, you only need to modify the parent class code.

3. The behavior is controlled by the parent class and implemented by sub-classes. The basic methods in the template method mode are implemented by sub-classes. Therefore, sub-classes can add corresponding functions through extensions to comply with the open and closed principles.

Use Cases of the template method:

1. Multiple child classes have common methods, and the logic is basically the same.

2. You can design important, complex, and Core algorithms as template methods. The details of these algorithms are implemented by sub-classes.

3. During refactoring, the same code can be extracted to the parent class.

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.