Traditional design pattern (eight) template method pattern

Source: Internet
Author: User
Tags abstract bool

The template method defines the steps of an algorithm and allows subclasses to provide implementations for one or more steps.

The common design algorithm is controlled directly by the implementation class, and there are duplicates in many functions similar objects. And because the organization of the class is not flexible, it is often necessary to make a lot of redundant steps when new similar modules are added.

We are still the electricity dealer's order submission to say the matter ~ ~

<summary>///template class///</summary> public abstract class Ordersubmit {public order
            Submit () {//Check cart Checkcart ();
            Check the payment method Checkpaytype ();
            Check the user address checkaddress ();
            Submit Order Submitorderinfo ();
            Hooks Implement if (Hookflag ()) {//Generate invoice Createinvoice () based on subclass needs;
        } public abstract void Checkcart ();
        public abstract void Checkpaytype ();
            public void Createinvoice () {//Generate invoice specific method} public void Checkaddress () {
        User address Check} public void Submitorderinfo () {//order submission details}
        public bool Hookflag () {return true; }///<summary>///main Station order submission///</summary> public class WeborDersubmit:ordersubmit {public override void Checkcart () {//Check the shopping cart information belonging to the Web site} 
        public override void Checkpaytype () {//Check the payment method belonging to the Web site} public void Createinvoice ()
    
    {//Generate invoice}}
        <summary>///Mobile Terminal order submission///</summary> public class Moblieordersubmit:ordersubmit { public override void Checkcart () {//Check the shopping cart information belonging to the mobile terminal} public override void Checkpay
            Type () {//Check the payment method belonging to the moving terminal}//Do not invoice public bool Hookflag () {
        return false; }
          
    
    }

Summary: Template method Patterns define the skeleton of an algorithm in one method and delay some steps into subclasses. Template methods allow subclasses to redefine certain steps in an algorithm without changing the structure of the algorithm. In contrast to other schemas, the template method subclasses determine how to implement the steps in the algorithm, the policy pattern is to encapsulate the interchangeable behavior, and then use the delegate to determine which behavior to take; the factory pattern determines which specific class to instantiate by subclasses.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

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.