Learning big talk Design Pattern 02 _ factory pattern advanced to strategy Pattern

Source: Internet
Author: User
1/** 2 * Cash charge abstract class 3 * @ author Monica 4*5 */6 public abstract class cashsuper {7/** 8 * abstract method of cash charge superclass 9 *@ param money collects cash, the parameter is the original price 10 * @ return. The current price 11 */12 public abstract double acceptcash (double money) is returned ); 13} 14 15/** 16 * normal fee subclass 17 * @ author Monica 18*19 */20 public class cashnormal extends cashsuper {21 22/*** 23 * normal fee, the original price returns 24 */25 @ override 26 Public double acceptcash (double money) {27 return money; 28} 29 30} 31 32/** 33 * discount fee subclass 34 * @ author Monica 35*36 */37 public class cashrebate extends cashsuper {38 private double moneyrebate = 1D; 39 40 public cashrebate (string moneyrebate) {41/* 42 * discount fee. during initialization, you must enter the discount fee, for example, 0.8, */44 This. moneyrebate = double. parsedouble (moneyrebate); 45} 46 47 @ override 48 Public double acceptcash (double money) {49 return money * moneyrebate; 50} 51 52} 53 54/** 55 * return the billing subclass 56 * @ author Monica 57*58 */59 Public class cashreturn extends cashsuper {60 // The return condition 61 private double moneycondition = 0.0d; 62 // return value 63 private double moneyreturn = 0.0d; 64 65 public cashreturn (string moneycondition, string moneyreturn) {66 this. moneycondition = double. parsedouble (moneycondition); 67 This. moneyreturn = double. parsedouble (moneyreturn); 68} 69 70 @ override 71 public double acceptcash (double money) {72 double result = money; 73 If (money> = moneycondition) {74 // if the return condition is greater than the return condition, the return value is 75. Result = money-math. floor (money/moneycondition) * moneyreturn; 76} 77 return result; 78} 79} 80 81/** 82 * cash payment factory class 83 * @ author Monica 84*85 */86 public class cashfactory {87 public static cashsuper createcashaccept (string type) {88 cashsuper cs = NULL; 89 switch (type) {90 case "normal charge": 91 cs = new cashnormal (); 92 break; 93 case "300 full return 100 ": 94 cs = new cashreturn ("300", "100"); 95 break; 96 case "0.8 off": 97 cs = new cashrebate (""); 98 break; 99} 100 return Cs; 101} 102}

Disadvantage: the factory must be changed every time you maintain and expand the billing method.
Advanced: Rule Mode

 

/*** Policy and simple factory combination * @ author Monica **/public class cashcontext {cashsuper cs = NULL; Public cashcontext (string type) {Switch (type) {Case "normal charge": cs = new cashnormal (); break; Case "300 full return 100": cs = new cashreturn ("300", "100"); break; case "0.8 off": cs = new cashrebate (""); break;} public double getresult (double money) {return CS. acceptcash (money) ;}// client public static void main (string [] ARGs) {cashcontext csuper = new cashcontext (" 8 8 8"); double totalprices = 0d; totalprices = csuper. getresult (double. parsedouble ("800 "));}

 

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 coupling between various algorithm classes and algorithm classes.

Advantages:

The strategy class hierarchy of the Policy mode defines a series of reusable algorithms or actions for context. Inheritance helps to analyze the common functions of these algorithms.

This simplifies unit testing because each algorithm has its own class and can be tested independently through its own interface.

When different behaviors are stacked in a class, it is difficult to avoid using conditional statements to select appropriate behaviors. Encapsulate these behaviors in independent strategy classes and eliminate conditional statements in the classes that use these actions.

Rule mode is used to encapsulate algorithms, but in practice, we can use it to encapsulate rules of almost task types, as long as you hear that different business rules need to be applied at different times during the analysis process, you can consider the possibility of using the Policy mode to handle such changes.

In the Basic Policy mode, the responsibility for selecting the specific implementation is borne by the client object and transferred to the context object in the Policy mode.

 

Changes to task requirements all require costs.

(The switch needs to be changed. reflection technology can be used in this method)

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.