Design Mode (17) --- template method mode, design mode ---

Source: Internet
Author: User

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.

Related Article

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.