Design Mode training 4: Strategy Mode

Source: Internet
Author: User

View myselfArticleIndex Via http://www.cnblogs.com/seesea125/archive/2012/04/17/2453256.html

I. Definition

Policy mode defines a seriesAlgorithmAnd encapsulate each algorithm and make them replace each other. The rule mode allows algorithms to change independently of customers who use it.

Ii. Overview

Application scenarios:

1. Multiple classes only differ in performance behavior. You can use the Strategy Mode to dynamically select the behavior to be executed during running.

2. Different policies (algorithms) need to be used in different situations, or they may be implemented in other ways in the future.

3. Hiding implementation details of specific policies (algorithms) from customers is completely independent of each other.

III,CodeImplementation

Requirement: the billing system of the mall. Based on the unit price and quantity of the goods, the following conclusions can be obtained: "normal charges", "300 off", "100 off ", "Off" and so on.

The Code is as follows:

 Abstract   Class  Cashsuper {  Public  Abstract   Double Acceptcash ( Double  Money );}  //  Normal billing subclass     Class  Cashnormal: cashsuper {  Public   Override   Double Acceptcash ( Double  Money ){  Return  Money ;}} //  Discount fee subclass     Class  Cashrebate: cashsuper {  Private   Double Moneyrebate = 1D;  Public Cashrebate ( String  Moneyrebate ){  This . Moneyrebate = Double  . Parse (moneyrebate );}  Public  Override   Double Acceptcash ( Double  Money ){  Return Money * Moneyrebate ;}}  //  Rebate billing subclass     Class  Cashreturn: cashsuper {  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 (Money> = Moneycondition) Result = Money-math. Floor (money/moneycondition )* Moneyreturn;  Return Result ;}} 

 

Context class:

 Class  Cashcontext {  Private  Cashsuper Cs;  Public Cashcontext ( String  Type ){  Switch  (Type ){  Case   "  Normal charges  " : Cashnormal CS0 = New  Cashnormal (); CS = CS0;  Break  ;  Case   "  300 to 100  "  : Cashreturn CR1 = New Cashreturn ( "  300 " , "  100  "  ); CS = CR1;  Break  ;  Case   "  Discount  "  : Cashrebate CR2 = New Cashrebate ( " 0.8  "  ); CS = Cr2;  Break  ;}}  Public   Double Getresult ( Double  Money ){  Return  CS. acceptcash (money );}} 

 

Client call:

Cashcontext csuper =NewCashcontext (cbxtype. selecteditem. tostring ());DoubleTotalprices =0d; totalprices= Csuper. getresult (convert. todouble (txtprice. Text) * convert. todouble (txtnum. Text ));

 

Iv. Differences with factories:

The above code can also be implemented using the factory method, but there is a big difference between the strategy and the factory.

If it is implemented by the factory, the core factory is like this, and the code remains unchanged elsewhere:

 Class  Cashfactory {  Public   Static Cashsuper createcashaccept ( String  Type) {cashsuper CS =Null  ;  Switch  (Type ){  Case   "  Normal charges  "  : CS = New  Cashnormal ();  Break  ;  Case   "  300 to 100 "  : CS = New Cashreturn ( "  300  " , "  100  "  );  Break  ;  Case   "  Discount  " : CS = New Cashrebate ( "  0.8  "  );  Break  ;}  Return  CS ;}} 

When using the factory, the client calls

Cashsuper csuper =Cashfactory. createcashaccept (cbxtype. selecteditem. tostring ());
DoubleTotalprices =0d;//Factory mode solutionTotalprices = csuper. acceptcash (convert. todouble (txtprice. Text) * convert. todouble (txtnum. Text ));

We can see that the two implementations are similar in their code, but there is only one more method.

 
Public DoubleGetresult (DoubleMoney ){ReturnCS. acceptcash (money );}

The difference is that they return different content. The factory returns different instances to the client, while the policy instance only serves its own class. It returns the algorithm result getresult Based on the parameter, therefore, they have different purposes. One is to return instances, and the other is to encapsulate different algorithms and adapt to algorithm changes.

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.