Design pattern (---Template method mode

Source: Internet
Author: User

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

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.