User-friendly design Model - The Template Method Pattern

Foreword

  We learn about finished creational design patterns and structural design pattern, and today we begin to learn and understand the behavior of design patterns. Today, we first look at such a design pattern - template method pattern. The patterns we unconsciously use to the ordinary will always develop. As we usually like all kinds of site templates, build templates, PPT templates and so on. What does it mean? Simple, that is, the common things out, you need to achieve your own specific then plus their own unique behavior it wants. We take a look at detailed explanation introduction.

Template Method pattern presentation

First, the reason

In our usual system development, often encounter some ways, most of the same code appears. There is often some stability framework, but some of them small operations is changing. We are now in the premise of how to go on to achieve a stable framework of different details of it?

Second, the intent

The algorithm defines a skeleton of the operation, some steps to subclasses delay. Template Method lets subclasses may not change the structure of certain steps of an algorithm of the algorithm to redefine.

Third, the case of FIG.

 

Fourth, the template method mode code sample

FIG see the above case, it is seen Template Method consists of two parts:

Abstract class: defines a template method, which comprises steps performed. There are fixing methods and abstract methods

Specific categories: implementation of the abstract methods specific behavior

Let's look at the following code example, help us to better understand its implementation. Take our writing articles, the steps are to open the editor - writing articles - Save release. Different subject matter is we write articles not the same as nothing. Together we see from code to achieve:

namespace Template_Method_Pattern 
{ 
    class TemplateMethodPattern 
    { 
    } 
    #region 
    ///  <the Summary> 
    /// abstract type
     ///  </ the Summary> 
    public  abstract  class WriteArticle 
    { 
        ///  <the Summary> 
        /// This is the template method defines writing order of. Abstract and can not be used herein Virtual, preventing its subclasses modify the order of execution.
        ///  </ the Summary> 
        public  void Article This article was () 
        { 
            OpenEditor (); 
            the Write (); 
            Release (); 
            Console.WriteLine ( " article finished and released! "); 
        } 
        ///  <Summary> 
        /// editor opens
         ///  </ Summary> 
        public  void OpenEditor () 
        { 
            Console.WriteLine ( " Open the editor " ); 
        } 
        ///  <Summary> 
        /// Start writing articles, subclasses implement specific content of the article
         ///  </ the Summary> 
        public  abstract  void the write ();
         ///  <the Summary> 
        /// publish articles
         ///  </ the Summary> 
        public  void release () 
        { 
            Console. the WriteLine ( "Published article " ); 
        } 
    } 
    #endregion 
    ///  <Summary> 
    /// Technical Articles
     ///  </ Summary> 
    public  class TchnologyArticle: WriteArticle 
    { 
        public  the override  void the Write () 
        { 
            Console.WriteLine ( " design pattern related articles " ); 
        } 
    } 
    ///  <Summary> 
    /// life articles
     ///  </ Summary> 
    public  class LifeArticle: WriteArticle 
    { 
        public  the override  voidThe Write () 
        { 
            Console.WriteLine ( " Life article " ); 
        } 
    } 

}

 

    class Program
    {
        static void Main(string[] args)
        {
            //技术文章
            WriteArticle writeTchnology = new TchnologyArticle();
            writeTchnology.Article();

            //生活文章
            WriteArticle writeLife = new LifeArticle();
            writeLife.Article();
        }
    }

 

Usage scenarios and the advantages and disadvantages

First, the use of scenarios

1, comprises a number of sub-classes in the same manner, the common logic

2, an important complex methods can be considered as a template method

Second, the advantage

1, the common part of the method implementation code reuse, maintenance compiled

2, the fixing order of the parent class, the extended sub-class implementation. Flexible and change, in line with the principle of opening and closing

3, the behavior is controlled by the parent class, subclass achieve control

Third, shortcomings

1, did not achieve a different demand will increase a child class, this will lead to increasing the number of classes, increase the size of the system

2, requires developers to spend more time to organize and clean up the relationship between them

to sum up

  Template Method pattern here will introduce over. The method is general template defines the pattern to achieve a step function, and the step of controlling an abstract class, the delay to be expanded to extend its subclasses. So that subclasses can reuse the code of the parent class. Of course, this is also a way to achieve code reuse-based inheritance. In the time we have developed a number of system-defined things that do not meet our needs. They also fixed in the development of some framework, some of which are left out to facilitate our developers to redefine themselves. Such a template method pattern can often be used in our daily development. Sometimes perception of themselves do not use it up. It is also a relatively simple and based model.


Ordinary life of ordinary use to treat heart, your life will be more exciting.

    C # Design Patterns series directory

Welcome to scan the next Fanger Wei code, and I set foot on the road to pass through the design patterns it!

  

Guess you like

Origin www.cnblogs.com/hulizhong/p/11578637.html