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/