Big talk design mode-template mode (learning notes)

Source: Internet
Author: User

Template mode structure:Template Method ModeDefines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to offset certain steps of an algorithm without changing the structure of an algorithm.


Basic prototype code: // abstract class
Abstract class AbstractClass
{// Some abstract behaviors are implemented in sub-classes.
Public abstract void PrimitiveOperation1 ();
Public abstract void PrimitiveOperation2 ();

// The template method provides the skeleton of the template, and the logical composition is some corresponding abstract operations, they are all postponed to the subclass solid stuffing? Http://www.bkjia.com/kf/yidong/wp/ "target =" _ blank "class =" keylink "> keys + keys = "");
}
}

// Implementation of specific object classes
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 ");
}
} // Front-end code
Static void Main (string [] args)
{
AbstractClass c;
C = new ConcreteClassA ();
C. TemplateMethod ();


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


Console. Read ();
}
Example: structure:

Code: class TestPaper
{
Public void TestQuestion1 (){
Console. WriteLine (", is [] a. Ball Mill Xuan iron B. tinplate c. High Speed alloy steel d. Carbon Fiber ");
Console. WriteLine ("Answer:" + Answer1 ());
}


Public void TestQuestion2 ()
{
Console. writeLine ("Yang Guo, Cheng Ying wiped out love flowers, resulting in []. make this plant harmless B. extinction of a rare plant c. damage the ecological balance of the ecosystem d. ");
Console. WriteLine ("Answer:" + Answer2 ());
}


Public void TestQuestion3 ()
{
Console. writeLine ("The Blue Phoenix caused Huashan shitu to vomit. If you are a doctor, what medicine will you give them? []. aspirin B. niuhuang Jiedu tablets c. covering acid d. let them drink a lot of raw milk e. none of the above ");
Console. WriteLine ("Answer:" + Answer3 ());
}
// A virtual Method
Protected virtual string Answer1 (){
Return "";
}
Protected virtual string Answer2 ()
{
Return "";
}
Protected virtual string Answer3 ()
{
Return "";
}
}


Class TestPagerA: TestPaper {
Protected override string Answer1 ()
{
Return "B ";
}


Protected override string Answer2 ()
{
Return "B ";
}


Protected override string Answer3 ()
{
Return "B ";
}


}


Class TestPagerB: TestPaper
{
Protected override string Answer1 ()
{
Return "c ";
}


Protected override string Answer2 ()
{
Return "c ";
}


Protected override string Answer3 ()
{
Return "c ";
}


}
// Front-end
Static void Main (string [] args)
{
Console. WriteLine ("Exam copied by Student :");
TestPaper studentA = new TestPagerA ();
StudentA. TestQuestion1 ();
StudentA. TestQuestion2 ();
StudentA. TestQuestion3 ();


Console. WriteLine ("Exam copied by students :");
TestPaper studentB = new TestPagerB ();
StudentB. TestQuestion1 ();
StudentB. TestQuestion2 ();
StudentB. TestQuestion3 ();


Console. Read ();
}

Running result:


Summary:
Since we use inheritance and are sure that this inheritance is meaningful, we should become a subclass template, and all repeated code should be raised to the parent class, instead of repeating each subclass.
When we want to complete a process or a series of steps at a specific level of detail, but the implementation of individual steps at a more detailed level may be different, we usually need to consider using the template method mode for processing.

The template method mode demonstrates its advantages by moving the unchanged behavior to the superclass and removing repeated code from the subclass. The template method is to provide a good platform for getting code.
When the immutable and mutable behaviors are mixed in the implementation of the method subclass, the immutable behavior will appear repeatedly 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.


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.