Template Method mode analysis, structure diagram, and basic code

Source: Internet
Author: User

Template Method mode analysis, structure diagram, and basic code
Zookeeper

Definition: The 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.
Structure:


AbstractClass is an abstract class, which is actually an abstract template. It defines and implements a template method. This template method is generally a specific method, which provides a skeleton of top-level logic, and the logical composition steps are postponed to subclass implementation in the corresponding abstract operations. The top-level logic may also call some specific methods.
ConcreteClass implements one or more abstract methods defined by the parent class. Each AbstractClass can have any number of concreteclasses corresponding to it, and each ConcreteClass can provide different implementations of these abstract methods (that is, the building steps of top-level Logic, so that the implementation of the top-level logic is different.
Feature: The template method mode demonstrates its advantages by moving the unchanged behavior to the superclass and removing repeated code in the subclass. The template method mode provides a good code reuse platform. When constant and variable behaviors are mixed in the implementation of method subclass, the unchanged behavior will be repeated in the subclass. We use the template method to move these actions to a single place, which helps the subclass to get rid of the entanglement of repeated unchanged behaviors.
Basic code:

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace template method mode
{
Class Program
{
Static void Main (string [] args)
{
AbstractClass c;

C = new ConcreteClassA ();
C. TemplateMethod ();

C = new ConcreteClassB ();
C. TemplateMethod ();

Console. Read ();

}
}

Abstract class AbstractClass
{
Public abstract void PrimitiveOperation1 ();
Public abstract void PrimitiveOperation2 ();

Public void TemplateMethod ()
{
PrimitiveOperation1 ();
PrimitiveOperation2 ();
Console. WriteLine ("");
}
}

Class ConcreteClassA: AbstractClass
{
Public override void PrimitiveOperation1 ()
{
Console. WriteLine ("Implementation of method 1 of Specific Class ");
}
Public override void PrimitiveOperation2 ()
{
Console. WriteLine ("Implementation of method 2 of Specific Class ");
}
}

Class ConcreteClassB: AbstractClass
{
Public override void PrimitiveOperation1 ()
{
Console. WriteLine ("Implementation of Class B method 1 ");
}
Public override void PrimitiveOperation2 ()
{
Console. WriteLine ("Implementation of method 2 of Class B ");
}
}

}

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.