My views on design patterns-2 strategic models

Source: Internet
Author: User

This article is intended for your own understanding and may cause errors. If there is any error, please point it out.

We have seen various promotion methods in malls, and there are still new promotion methods. If we use a program to help malls calculate the amount of money they should receive after the promotion, A lot of switches may be used for programmers... Case or if... Else if... Else... Statement to list all the promotion methods of the mall into a program. This is true, but the scalability of modification is poor. A better learning method may use a simple factory method, but it also has a drawback. For a simple factory, its idea is to return the corresponding objects, which generally correspond to real things. However, our promotion method here is more like a method. Although a simple factory can be said in the past, it always feels awkward. In addition, if the promotion method of a mall changes, the factory type and corresponding promotion method must be modified. Since we have mentioned that these promotion methods are more like methods, it is more suitable to use the strategy model.

The rule mode defines the algorithm family and encapsulates them separately so that they can be replaced with each other. This mode changes the algorithm and does not affect the customers who use the algorithm.

Next, let's take a look at the class diagram.


First, define CashSuper as a parent class. Then, three classes inherit this class and define different policies in the three inheritance classes.

Let's take a look at the code below:

[Java]
Package Cash;
 
Abstract class CashSuper {
Public abstract double acceptCash (double money );
}
[Java]
Package Cash;
 
Public class CashNormal extends CashSuper {
 
Public double acceptCash (double money ){
Return money;
}
}

[Java]
Package Cash;
Public class CashRebate extends CashSuper {
Private double moneyRebate = 1d;
Public CashRebate (String moneyRebate ){
This. moneyRebate = Double. parseDouble (moneyRebate );
}
Public double acceptCash (double money ){
Return money * moneyRebate;
}
}

[Java]
Package Cash;
Public class CashReturn extends CashSuper {
Private double moneyCondition = 0.0d;
Private double moneyReturn = 0.0d;
Public CashReturn (String moneyCondition, String moneyReturn ){
This. moneyCondition = Double. parseDouble (moneyCondition );
This. moneyReturn = Double. parseDouble (moneyReturn );
}
Public double acceptCash (double money ){
Double result = money;
If (money> = moneyCondition)
Result = money-Math. floor (money/moneyCondition) * moneyReturn;
Return result;
}
}

[Java]
Package Cash;
Class CashContext {
Private CashSuper cs;
Public CashContext (CashSuper csuper ){
This. cs = csuper;
}
Public double GetResult (double money ){
Return cs. acceptCash (money );
}
}
The last step is the main program:

[Java]
Package Cash;
Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStreamReader;
Public class CashUI {
Public static void main (String [] args ){
Try {
CashContext cc = null;
System. out. println ("1. Normal charges ");
System. out. println ("2. 300 to 100 ");
System. out. println ("3. 8 off ");
System. out. println ("select :");
BufferedReader br = new BufferedReader (new InputStreamReader (System. in ));
Int iselect = 0;
Iselect = Integer. parseInt (br. readLine ());
Switch (iselect)
{
Case 1: // normal charges
Cc = new CashContext (new CashNormal ());
Break;
Case 2: // 300 to 100
Cc = new CashContext (new CashReturn ("300", "100 "));
Break;
Case 3: // off
Cc = new CashContext (new CashRebate ("0.8 "));
Break;
}
Double totalPrices = 0d;
System. out. println ("Enter the total amount of money :");
BufferedReader br2 = new BufferedReader (new InputStreamReader (System. in ));
Double nowPrice = Double. parseDouble (br2.readLine ());
TotalPrices = cc. GetResult (nowPrice );
System. out. println (totalPrices );
}
Catch (IOException e ){
System. out. println (e. getMessage ());
}
}
}

The rule mode focuses more on algorithm changes, while the simple factory mode focuses on the changes of specific objects. You can identify the differences between the two points. In addition, these two methods can be used in combination to put the returned policy into a simple factory class.
Author: mengxiangyue

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.