Design Mode (17) --- template method mode, design mode ---
I. Definition
Template Method mode: defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
Explanation: in simple terms, a general base class needs to be defined, but different operations in the base class, so the template method mode writes different operations into an abstract function and places it in the subclass for implementation, in this way, the general base class can be completed.
Ii. UML class diagram and basic code
Basic code:
Abstract class AbstractClass {public abstract void PrimitiveOperation1 (); public abstract void PrimitiveOperation2 (); public void TemplateMethod () {PrimitiveOperation1 (); PrimitiveOperation2 (); Console. writeLine ("basic class behavior") ;}} class ConcreteClassA: AbstractClass {public override void PrimitiveOperation1 () {Console. writeLine ("Implementation of specific Class A method 1");} public override void PrimitiveOperation2 () {Console. writeLine ("Implementation of class A method 2");} class ConcreteClassB: AbstractClass {public override void PrimitiveOperation1 () {Console. writeLine ("Implementation of specific class B method 1");} public override void PrimitiveOperation2 () {Console. writeLine ("Implementation of Class B method 2 ");}}
Client call and result:
AbstractClass ac; ac = new ConcreteClassA (); ac. TemplateMethod (); Console. WriteLine (""); ac = new ConcreteClassB (); ac. TemplateMethod ();View Code
Iii. instance description
Although the methods for cooking different dishes are different in daily life, the vast majority of the steps are the same. In this example, we use fried spinach and fried cabbage as examples to describe the template method, the basic code is as follows:
Public abstract class Vegetable {public void CookVegetable () {Console. writeLine ("general method of burning vegetables:"); this. poilOil (); this. heatOil (); PourVegetable ();} public void PoilOil () {Console. writeLine ("Pour oil");} public void HeatOil () {Console. writeLine ("boil oil");} public abstract void PourVegetable ();} public class Spinach: Vegetable {public override void PourVegetable () {Console. writeLine ("spinach in the pot") ;}} public class Cabbage: Vegetable {public override void PourVegetable () {Console. writeLine ("Cabbage in the pot ");}}View Code
Client call and running result:
Vegetable sp = new Spinach(); sp.CookVegetable();
Iv. Advantages and Disadvantages and applicable scenarios
Advantages:
1) code reuse
2) Be able to flexibly cope with changes in subclass steps, and comply with the open-closed Principle
Disadvantages:
An abstract class is introduced to make the system logic more complex.
Applicable scenarios:
The template method mode moves the unchanged behavior to the superclass, removing repeated code in the subclass to reflect its advantages. Therefore, the template method mode can be used to reflect this advantage.