Strategy Model of big talk Design Model

Source: Internet
Author: User

When you do one thing to complete a task, there are often many methods, each of which can be called a policy, we will according to different environments or conditions, select an appropriate method from multiple methods to complete this task or task. Just like when we go home, there are many ways to go home, such as taking a plane, taking a train, taking a bus, and walking. However, due to our own restrictions or environmental restrictions, we often choose a suitable way to go home. The method we choose can be called a policy. In fact, the so-called policy pattern is an object behavior pattern, which defines the algorithm family and encapsulates the algorithms separately so that the algorithms can be replaced with each other. This mode changes the algorithm and does not affect the customers who use the algorithm. It is also called a policy model. Policy pattern structure: Strategy class: defines a series of reusable algorithms or actions for Context. ConcreteStrategy class: a specific policy class that encapsulates specific algorithms or behaviors and inherits the Strategy class, helping to analyze the common functions of these algorithms. Example of policy mode application: code structure diagram: Form Design Drawing: code implementation: [csharp] double total = 0.0d; private void btnOK_Click (object sender, EventArgs e) {CashContext csuper = new CashContext (cmbType. selectedItem. toString (); double totalPrices = 0d; totalPrices = csuper. getResult (Convert. toDouble (txtPrice. text) * Convert. toDouble (txtNum. text); total = total + totalPrices; listBox1.Items. add ("unit price:" + txtPrice. text + "Quantity:" + txtNum. tex T + "Total:" + totalPrices. toString (); lblTotal. text = Convert. toString (total);} private void btnRef_Click (object sender, EventArgs e) {txtPrice. text = ""; txtNum. text = ""; listBox1.Text = ""; lblTotal. text = "";} private void Form1_Load (object sender, EventArgs e) {cmbType. items. addRange (new object [] {"normal charges", "300 off", "100 off", "off", "off"}); cmbType. selectedIndex = 0 ;}} class CashContext {CashSuper cs = null; public CashContext (string type) // string type is not a specific billing policy, but a string that indicates the billing type {switch (type) {case "normal charge": CashNormal cs0 = new CashNormal (); cs = cs0; break; case "300 full return 100": CashReturn cs1 = new CashReturn ("300 ", "100"); cs = cs1; break; case "": CashRebate cs2 = new CashRebate ("0.8"); cs = cs2; break ;}} public double GetResult (double money) {return cs. acceptCash (money );}} Abstract class CashSuper // an abstract class for cash receipt {public abstract double acceptCash (double money);} class CashNormal: CashSuper // a subclass of the normal toll class {public override double acceptCash (double money) {return money ;}} class CashRebate: CashSuper // discount class {private double moneyRebate = 1d; public CashRebate (string moneyRebate) {this. moneyRebate = double. parse (moneyRebate);} public override double acceptCash (doub Le money) {return money * moneyRebate;} class CashReturn: CashSuper // The number of classes returned when full {private double moneyCondition = 0.0d; private double moneyReturn = 0.0d; public CashReturn (string moneyCondition, string moneyReturn) {this. moneyCondition = double. parse (moneyCondition); this. moneyReturn = double. parse (moneyReturn);} public override double acceptCash (double money) {double result = money; if (m Oney> = moneyCondition) result = money-moneyReturn; return result ;}} policy mode: 1: if a system contains many classes, their difference lies in their behavior. In this case, we can use the policy mode to select one of the many actions. Second, a system needs to dynamically select an algorithm from multiple algorithms. Third, the system has a complex algorithm-related data structure, and we do not want clients on the client to know the data structure. In specific policies, we encapsulate algorithms and related data structures, to ensure the security of the algorithm and keep the algorithm confidential. Fourth: when different behaviors are stacked in a class, it is inevitable that a conditional statement will be used to select a suitable behavior during execution. Advantages of the Policy mode: first, the unit test is simplified. Each algorithm has its own class and can be tested independently through its own interface. Second, you can avoid using multiple conditional transfer statements to make them easy to maintain. Third, the policy mode defines a series of algorithm behaviors. All algorithms perform the same job, but they implement different operations. They can call all algorithms in the same way, reduces coupling between various and algorithm classes.

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.