Step by step. NET Design Mode learning notes 18, Template (Template Mode)

Source: Internet
Author: User

Overview

Changes have always been an eternal topic of software design. In XP programming, we advocate embracing changes and actively coping with them. How can we better grasp the change point and respond to the change? How can we improve code reuse? By learning the Template Method mode, you should have a new understanding.

Intention

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. [-GOF design patterns]

Structure chart

Figure 1 Structure of the Template Method mode

Examples in life

The template method defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. Architects use the template method when developing new projects. A typical plan includes some floor plans, each of which reflects different parts. In a plan, the foundation, structure, water loading, and cabling are the same for each room. Different house styles are generated only when there is a difference in the later stage of the building.

Figure 2 use the Template Method mode of the building diagram as an Example

 

Example

Define a supermarket abstract class. Each supermarket inherits this abstract class to form our template mode. The example is shown below:

 

Code Design

Create ISuperMarket. cs First:

01 public interface ISuperMarket
02 {
03     /// <summary>
04     /// Activity start time
05     /// </summary>
06     /// <returns></returns>
07     DateTime PreferencesStartTime();
08  
09     /// <summary>
10     /// Type
11     /// </summary>
12     /// <returns></returns>
13     int Category();
14  
15     /// <summary>
16     /// Discount
17     /// </summary>
18     /// <returns></returns>
19     double Discount();
20  
21     /// <summary>
22     /// Supermarket name
23     /// </summary>
24     /// <returns></returns>
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.