A summary of the Strategy Model

Source: Internet
Author: User
  1. Package CN. Zhao. Moshi. Strategy;
  2. Public interface idiscountstrategy
  3. {
  4. Float getdiscount (productbean probean );
  5. }
  6. Package CN. Zhao. Moshi. Strategy;
  7. /**
  8. * Discounts are made based on the unit price of the purchased item.
  9. **/
  10. Public class impldiscountbyprice implements idiscountstrategy
  11. {
  12. Public float getdiscount (productbean probean)
  13. {
  14. Float reint = 1.0f;
  15. // If the unit price exceeds 1000, you will be given a 95 discount.
  16. If (probean. getunitprice ()> = 1000f)
  17. {
  18. Reint = 0.95f;
  19. }
  20. Return reint;
  21. }
  22. }
  23. Package CN. Zhao. Moshi. Strategy;
  24. /**
  25. * Discounts are made based on the quantity purchased by the user.
  26. **/
  27. Public class impldiscounttotal implements idiscountstrategy
  28. {
  29. Public float getdiscount (productbean probean)
  30. {
  31. Float reint = 1.0f;
  32. // If the quantity purchased exceeds 10, you will be given an 85 discount.
  33. If (probean. getcount ()> = 10)
  34. {
  35. Reint = 0.85f;
  36. }
  37. Return reint;
  38. }
  39. }
  40. Package CN. Zhao. Moshi. Strategy;
  41. /**
  42. * Context bean of product information
  43. **/
  44. Public class productbean
  45. {
  46. /**
  47. * Product Name
  48. **/
  49. Private string name = NULL;
  50. /**
  51. * Product unit price
  52. **/
  53. Private int unitprice = 0;
  54. /**
  55. * Quantity purchased
  56. **/
  57. Private int COUNT = 0;
  58. /**
  59. * Discount ratio
  60. **/
  61. Private float Dist = 1.0f;
  62. Public float gettotalcount ()
  63. {
  64. Return this. unitprice * This. Count * This. Dist;
  65. }
  66. /**
  67. * Discounts
  68. **/
  69. Public void dodiscount (idiscountstrategy strate)
  70. {
  71. This. Dist = strate. getdiscount (this );
  72. }
  73. Public String getname ()
  74. {
  75. Return name;
  76. }
  77. Public void setname (string name)
  78. {
  79. This. Name = Name;
  80. }
  81. Public int getunitprice ()
  82. {
  83. Return unitprice;
  84. }
  85. Public void setunitprice (INT unitprice)
  86. {
  87. This. unitprice = unitprice;
  88. }
  89. Public int getcount ()
  90. {
  91. Return count;
  92. }
  93. Public void setcount (INT count)
  94. {
  95. This. Count = count;
  96. }
  97. }
  98. Package CN. Zhao. Moshi. Strategy;
  99. Public class teststrategy
  100. {
  101. Public static void main (string [] ARGs)
  102. {
  103. Productbean probean = new productbean ();
  104. Probean. setcount (1 );
  105. Probean. setunitprice (2000 );
  106. Probean. setname ("Rolex ");
  107. // Set the discount by quantity
  108. Probean. dodiscount (New impldiscountbyprice ());
  109. System. Out. println (probean. gettotalcount () + "$ ");
  110. }
  111. }
1 Strategy Mode strategy

It is used to encapsulate a series of algorithms, instead of some if else judgments, and implemented using OO ideas.

The advantage is that you no longer need to change the client program because of algorithm expansion. However, if the algorithm is not necessary for expansion, the use of the Policy mode may not be better than if else.

Key Point: Only algorithm encapsulation, excluding other information.

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.