My design Tour [2]:template method pattern (OO)

Source: Internet
Author: User

Abstract

Template method I learned the second pattern, which is a very easy to understand pattern, but very practical.

Intent

For operation, only the outline of the algorithm is defined first, and some steps are left to the subcategory to fill, so that the subcategories go through some steps without changing the overall architecture of the algorithms.

Its UML notation

In practice, we may have a full-featured class, but because of "change in Requirements", the new class and the original class is almost 60% the same, only 40% different, so we want to have 60% of the same part of the program to stay, just rewrite 40% of the place.

If a company only produces "Automatic tea machine", in order to increase product line, want to produce "automatic coffee machine", after analysis, two machines similar structure, production flow is similar.

Automatic tea Making Machine

Step1: Boil the boiled water

Step2: Add < tea > into boiling water

Step3: < tea > poured into cups

Step4: Plus < lemon >

Automatic coffee maker

Step1: Boil the boiled water

Step2: Add < Coffee powder > put into boiling water

Step3: < coffee > pour into cups

Step4: Plus < sugar > and < cream >

Obviously step1 the same, but Step2 ~ step4 is not the same, so just rewrite Step2 ~ STEP4,STEP1 can continue to use. First design a drinkmachine prototype, defined the production process and STEP1, because Step2 ~ step4 each have differences, it is left in the inheritance of Drinkmachine class to rewrite, this is template method patterns.

Let's take a look at this architecture, and if a new drink is added after the day, Drinkmachine,teamachine,coffeemachine is not modified to conform to the OCP closed for modification principle, to add a new class, Only the process and rewrite drinkmachine can be, in line with the OCP open for extension principle, so it is very good to maintain the architecture.

In short, template method pattern is to wrap different member function with class and rewrite it by derived 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.