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.