First, the definition
Template method Pattern: Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
Explanation: Simply speaking, you need to define a common base class, but different operations in the base class, so the template method pattern writes different operations into an abstract function to be implemented in a subclass so that the purpose of the common base class can be accomplished.
Ii. UML class diagram and Basic code
Basic code:
Abstract classAbstractClass { Public Abstract voidPrimitiveOperation1 (); Public Abstract voidPrimitiveOperation2 (); Public voidTemplatemethod () {PrimitiveOperation1 (); PrimitiveOperation2 (); Console.WriteLine ("behavior of the base class"); } } classConcreteclassa:abstractclass { Public Override voidPrimitiveOperation1 () {Console.WriteLine ("implementation of concrete Class A method 1"); } Public Override voidPrimitiveOperation2 () {Console.WriteLine ("implementation of concrete Class A method 2"); } } classConcreteclassb:abstractclass { Public Override voidPrimitiveOperation1 () {Console.WriteLine ("implementation of Concrete Class B method 1"); } Public Override voidPrimitiveOperation2 () {Console.WriteLine ("implementation of Concrete Class B method 2"); } }
Client calls and results:
abstractclass ac; New Concreteclassa (); Ac. Templatemethod (); Console.WriteLine (""); New CONCRETECLASSB (); Ac. Templatemethod ();
View Code
Iii. Description of the case
Cooking in daily life, although the way to do different dishes, but the vast majority of steps are the same, this example with fried spinach and fried cabbage as an example to illustrate the template method pattern, the basic code is as follows:
Public Abstract classVegetable { Public voidcookvegetable () {Console.WriteLine ("general methods of burning vegetables:"); This. Poiloil (); This. Heatoil (); Pourvegetable (); } Public voidPoiloil () {Console.WriteLine ("pour oil"); } Public voidHeatoil () {Console.WriteLine ("burn the oil hot"); } Public Abstract voidpourvegetable (); } Public classspinach:vegetable { Public Override voidpourvegetable () {Console.WriteLine ("spinach poured into the pot"); } } Public classcabbage:vegetable { Public Override voidpourvegetable () {Console.WriteLine ("the cabbage pours into the pot"); } }
View Code
Client invoke and run result:
New spinach (); Sp. Cookvegetable ();
Iv. advantages and disadvantages and application scenarios
Advantages:
1) Implementation of code reuse
2) flexibility to respond to changes in sub-class steps in accordance with the open-closed principle
Disadvantages:
Introduces an abstract class that makes system logic more complex.
Applicable scenarios:
The template method pattern shows its advantages by moving the invariant behavior to the superclass and removing the duplicated code in the subclass. Therefore, the template method pattern can be used wherever this advantage can be demonstrated.
Design pattern (---Template method mode