Design Patterns - Template Method Pattern Detailed

First, the concept

  Template Method design pattern belongs to class behavior patterns.

  Definition: The algorithm defines a framework operation, some steps to subclasses delay. Subclasses may not change so that the structure of some of the steps of an algorithm of the algorithm to redefine.

  In simple terms, it is an abstract class (template) and subclasses (concrete implementation) composition, the abstract class defines the functional and logic need to complete this class, subclass method overrides responsible for certain services.

  For example, in the daily development work, there will be a lot of data needs to be saved to the database. Save data generally require checking the legality of data, save data to the database, the data is returned to the client end of these three steps. Saved data is not the same, it is not the same validation rules. In this case, we can put in a three logical abstract class, the method of this check data is written virtual method, for different subclasses to override. So as to achieve code reuse and different data validation rules have different effects.

Second, the realization

  1. Define an abstract class
    public abstract class AbstractSave
       {
           public abstract void BeforeSave();
    
           public abstract void AfterSave();
    
           public void SaveData()
           {
               Console.WriteLine("数据成功保存到数据库");
           }
    
           public void Save()
           {
               this.BeforeSave();
               this.SaveData();
               this.AfterSave();
           }
       }
  2. Defined subclass
     public  class ConcreteSave: AbstractSave 
        { 
            public  the override  void the BeforeSave () 
            { 
                Console.WriteLine ( " check before saving data A " ); 
            } 
    
            public  the override  void afterSave () 
            { 
                Console.WriteLine ( " after the data save operation A " ); 
            } 
        }
  3. Call it
     static void Main(string[] args)
            {
                AbstractSave data1 = new ConcreteSave();
                data1.Save();
    
                Console.WriteLine();
    
                AbstractSave data2 = new ConcreteSave();
                data2.Save();
            }

Third, the summary

  I think this is very good to understand the design patterns. Generally used in what scenario? For example, in practice, a demand, the architect or engineer to manage the business logic is clear, to build a good basic framework, then assign specific tasks to achieve other engineers. This time, the template method pattern will be able to come in handy, both to ensure that the general direction is not wrong, but also be able to achieve the separation of specific tasks to go out, do both.

  The above is relatively simple, in practice, usually used in conjunction with a generic, abstract class, the data to be saved as a generic type declaration, the returned data is also declared as generics.

  Download Code: https://github.com/hzhhhbb/TemplateMethodPattern

Guess you like

Origin www.cnblogs.com/hzhhhbb/p/11489382.html