What is the template method in base classes?

Ok, I went through this great MSDN article on "using the base class." Although I understand the concept of a base class and interfaces, I cannot understand the use of the Template methods in the second paragraph of this article (" Protected Methods and Constructors ").

Can someone help me understand this concept with a simple practical example? Perhaps understanding the Template Method concept is a good place to start.

Thanks in advance.

+5
source share
3 answers

This is a very old article, in my opinion, I do not remember what Impl was called.

, wikipedia :
:

  • ( ) ,

  • : , .

  • , () . , , , .

( ), , : " , ". , , .

, .

, , .
, Publisher, , .
, , .
, , - ftp http, .
dababase , , , .
derrived Publisher, .

public abstract class Publisher 
{
      private address;
      // if you wish to force implementation in derived class, make method abstract
      private abstract void Initialize();
      // if you wish optional implementation in derived class, make it virtual
      protected virtual void SendChangesToWeb() 
      {
         // ...
         webClient.Upload(address, data)
      }

      // if you wish that some step could not be changed from outside 
      private void LogSentChangesToDatabase() 
      {
         // ... save date time when was send and what was sent
      }

      // this sequence is the same for all derives, no point to duplicate 
      public void PublishUpdates() 
      {
           Initialize();
           SendChangesToWeb();
           LogSentChangesToDatabase();
      }
}

public class GooglePublisher : Publisher {
     private override Initialize() 
     {
         address = "http://www.google.com";
     }         
}

public class FtpPublisher : Publisher {
     private override Initialize() 
     {
         address = "ftp://test.com";
     }     

     protected override SendChangesToWeb() 
     {
        FtpClient.Upload(address, data)
     }
}
+2

, , . , . .

, , , , , .

, , , . , , , .

, , . . , , , , , .

+1

You can find the template template method templates. This template includes the Template method, which provides a sequence of method skeleton calls. One or more steps can be deferred to subclasses that implement these steps without changing the overall sequence of calls. Example:

// Template Method pattern -- Structural example

using System;



namespace DoFactory.GangOfFour.Template.Structural

{

  /// <summary>

  /// MainApp startup class for Real-World

      /// Template Design Pattern.

      /// </summary>

      class MainApp

  {

    /// <summary>

    /// Entry point into console application.

    /// </summary>

    static void Main()

    {

      AbstractClass aA = new ConcreteClassA();

      aA.TemplateMethod();



      AbstractClass aB = new ConcreteClassB();

      aB.TemplateMethod();



      // Wait for user

      Console.ReadKey();

    }

  }



  /// <summary>

  /// The 'AbstractClass' abstract class

  /// </summary>

  abstract class AbstractClass

  {

    public abstract void PrimitiveOperation1();

    public abstract void PrimitiveOperation2();



    // The "Template method"

    public void TemplateMethod()

    {

      PrimitiveOperation1();

      PrimitiveOperation2();

      Console.WriteLine("");

    }

  }



  /// <summary>

  /// A 'ConcreteClass' class

  /// </summary>

  class ConcreteClassA : AbstractClass

  {

    public override void PrimitiveOperation1()

    {

      Console.WriteLine("ConcreteClassA.PrimitiveOperation1()");

    }

    public override void PrimitiveOperation2()

    {

      Console.WriteLine("ConcreteClassA.PrimitiveOperation2()");

    }

  }



  /// <summary>

  /// A 'ConcreteClass' class

  /// </summary>

  class ConcreteClassB : AbstractClass

  {

    public override void PrimitiveOperation1()

    {

      Console.WriteLine("ConcreteClassB.PrimitiveOperation1()");

    }

    public override void PrimitiveOperation2()

    {

      Console.WriteLine("ConcreteClassB.PrimitiveOperation2()");

    }

  }

}

Link: template template template

0
source

All Articles