No nonsense C # design Pattern 14: Template method

Source: Internet
Author: User
Tags abstract

Intention

Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Template method enables subclasses to redefine certain steps of the algorithm without altering the structure of an algorithm.

Scene

The template approach is a very easy to understand design pattern, because it does not have too many structural interleaving, and secondly because this code reuse technology is very easy to think of people who have knowledge of OO, and you probably already use the template method in many places. In the use of some design patterns often will also use the template method, and even some design pattern itself with the idea of template method.

Today, we give such a practical example. People who have done bank payments and paid Alipay know that a payment process is based on two interfaces. The submission interface and gateway return interface, although the main gateway payment interface format is different, such as some of the gateway to pay the parameter is money, and some gateways are amount, but from the payment process, we will generally experience the following steps:

L Obtain order information, verify the legality of order

L Generate forms for submission to major gateways

L Record Log

L submit the form to the corresponding gateway

For each gateway, the resulting form and logging are not the same, but the entire payment process and the process of obtaining order information and submitting the form are the same. In this way, the template method pattern is used to use the invariant part, and the variable part is left to the subclass to implement.

Sample code

using System;

using System.Collections.Generic;

using System.Text;

Namespace Templatemethodexample

{

Class program

{

static void Mai N (string[] args)

{

Paygateway pg = new Ipsgateway ();

pg. SubmitOrder (New Order ());

}

}

Class order

{

}

Class SUBMITFORM

{

}

Abstract class Paygateway

{

protected abstract void Writelog (submitform SF);

Protected abstract SubmitForm generateorderform (order);
           
public void SubmitOrder (order)

{

if (order = = null)

{

Console.WriteLine ("Invalid Order");

return;

}

SubmitForm SF =Generateorderform (order);

if (SF = null)

{

Console.WriteLine ("Generate Submit Form Failed ");

return;

}

Writelog (SF);

}

}

Class Ipsgateway:paygateway

{

protected Overrid e void Writelog (submitform sf)

{

Console.WriteLine ("Log wrote");
           
}

protected override SubmitForm Generateorderform (order)

{

Console.WriteLine ("Submit Form generated");

return new SubmitForm ();

}

}

}

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.