Big talk design model Reading Notes 2-Strategy Model

Source: Internet
Author: User

Policy mode is a method that defines a series of algorithms. From a conceptual point of view, all these algorithms perform the same job, but they only implement differently, it can call all algorithms in the same way, reducing the direct coupling between various algorithm classes and algorithm classes.

UML diagram:

According to the big talk design pattern-Chapter 2 shopping mall promotion case code, you can simply record the usage of the Strategy pattern:

/// <Summary> /// cash charge abstract class /// </Summary> public abstract class cashsuper {/// <summary> // The super class abstract method of cash charge /// </Summary> /// <Param name = "money"> original price </param> /// <returns> current price </returns> public abstract double acceptcash (double money );}
Cash charge abstract class
/// <Summary> // normal billing subclass /// </Summary> public class cashnormal: cashsuper {public override double acceptcash (double money) {return money ;}} /// <summary> /// discount billing subclass /// </Summary> public class cashrebate: cashsuper {private double moneyrebate = 1D; Public cashrebate (string moneyrebate) {// discount fee. during initialization, you must enter the discount rate. For example, the discount rate is 0.8 this. moneyrebate = double. parse (moneyrebate);} public override double acceptcash (double money) {return money * moneyrebate ;}} /// <summary> /// rebate billing subclass /// </Summary> public class cashreturn: cashsuper {private double moneycondition = 0.0d; private double moneyretrun = 0.0d; public cashreturn (string moneycondition, string moneyreturn) {// billing. You must enter the rebate condition and value during initialization. For example, if 300 is returned when the value is full, moneycondition is 100, moneyreturn is 100 this. moneycondition = double. parse (moneycondition); this. moneyretrun = double. parse (moneyreturn);} public override double acceptcash (double money) {double result = money; If (money> = moneycondition) {result = money-math. floor (money/moneycondition) * moneyretrun;} return result ;}}
Subclass of various algorithms
/// <Summary> /// context class /// </Summary> public class cashcontext {cashsuper cs = NULL; Public cashcontext (string type) {Switch (type) {Case "normal charge": cashnormal CS0 = new cashnormal (); this. cs = CS0; break; Case "Full 300 return 100": cashreturn CS1 = new cashreturn ("300", "100"); this. cs = CS1; break; Case "0.8 off": cashrebate CS2 = new cashrebate (""); this. cs = CS2; break;} public double getresult (double money) {return CS. acceptcash (money );}}
Context

Client code:

Void btnok_click (Object sender, eventargs e) {cashcontext csuper = new cashcontext (this. cbxtype. text); double totalprices = 0d; totalprices = csuper. getresult (convert. todouble (txtprice. text) * convert. todouble (txtnum. text); Total = total + totalprices; lbxlist. items. add ("unit price:" + txtprice. text + "Quantity:" + txtnum. text + "" + cbxtype. text + "Total:" + totalprices. tostring (); this. lbltotal. TEXT = total. tostring ();}

Interface:

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.