Template Method mode in Design Mode

Source: Internet
Author: User

I. Application scenarios

When an algorithm flow a () is designed in our code, the algorithm flow depends on several other flows B (), c (), d (), and so on, in addition, the dependency processes B (), c (), and d () in the system may be implemented in a variety of ways. At this time, we can use the method template mode. For the moment, we have implemented this design model. Let's discuss its advantages.

Ii. code example implementation

There is now a People class that implements a life-summary method mylife (). The implementation process of the method contains three items: study (), work (), marry (), but there are different implementation methods for the three methods for different groups of People. In order to realize the reuse of the mylife code, we () the study (), work (), and marry () methods are called respectively, but only the three methods are declared in the People object instead of implementing them, next we will give these three methods for different groups of People to implement them in different ways, and, based on the principle of polymorphism, we will apply them to the subclasses of People (different groups of People) in the future, we do not need to rewrite the mylife () method. We only need to rewrite the three methods that mylife () depends on to achieve the effects of different groups of mylife.

The following code is provided:

public abstract class People {public void mylife(){study();work();marry();}public abstract void study();public abstract void work();public abstract void marry();}

The implementation of mylife () is actually the so-called template method. In the subclass of People, we no longer need to implement mylife (). All we need to do is override study (), work (), marry.

Public class Banker extends People {@ Overridepublic void marry () {System. out. println ("My wife is beautiful, I am happy");} @ Overridepublic void study () {System. out. println ("Learning and falling in love") ;}@ Overridepublic void work () {System. out. println ("nothing, a lot of money ");}}

This is the life of a banker.

Public class SoftwareEngineer extends People {@ Overridepublic void marry () {System. out. println ("married, but no time to accompany his wife") ;}@ Overridepublic void study () {System. out. println ("learning software design, especially hard to understand, hair fell") ;}@ Overridepublic void work () {System. out. println ("working overtime every day, so tired ");}}

This is a dear ape. They live in the hot water.

The following is a class diagram of the template method mode:

Iii. Summary

Despite their hard work, after learning the method template mode, we can have a few less lines of code. After all, we have reused the code of mylife.


Zookeeper

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.