C ++ implementation policy Mode

Source: Internet
Author: User

Strategy: it defines the algorithm family and encapsulates them separately so that they can replace each other. This pattern changes the algorithm and does not affect the customers who use the algorithm.

The above is the definition given in "big talk Design Patterns". I feel that this concept is not good. After reading it, I cannot fully understand this pattern (maybe the reason for my own level being too watery ). In Objective C ++, it is pointed out that the policy mode is the replacement method of virtual functions. Think about it. If you want to implement different algorithms in different classes, define a virtual function in the base class and the derived class provides different implementations. There is a problem here. If there are too many algorithms to be implemented (different algorithm parameters and return values), virtual declarations must be made in the base class, the implementation of specific functions is provided in a specific class. Therefore, each derived class must inherit so many virtual functions (and most of them are useless). The derived class is very bloated. Some people have given a derived-type weight loss method. The factory mode is actually that each algorithm provides an implementation class plus the use of polymorphism. Other methods are that each algorithm implements a class, pass this class into a specific process class, and use the algorithm class as an attribute of the Process class. You can set it as required. Of course, this algorithm is somewhat similar to the factory mode. (There is a link between the two)

First, describe the application scenario and design the billing software for the mall. the requirements can be met, with different discount requirements and different rebates that can be used to deliver hundreds of dollars. For example, 300 off for the celebration, 100 off for the National Day;

Let's take a look at the relationship between the factory mode and the policy mode to see the differences between them:

Factory model:

Rule mode:

The implementation code of the traditional policy mode is as follows:

#include<iostream>#include<string>using namespace std;class CashSuper{public:virtual double acceptCash(double money) = 0;};class CashNormal : public CashSuper{public :double acceptCash(double money){return money;}};class CashRebate : public CashSuper{public:CashRebate(double d = 1) : moneyRebate(d){}double acceptCash(double money);private:double moneyRebate; };double CashRebate::acceptCash(double money){return money * moneyRebate;}class CashReturn : public CashSuper{public:CashReturn(double mc = 0.0, double mr = 0.0) : moneyCondition(mc), moneyReturn(mr){}double acceptCash(double money);private:double moneyCondition;double moneyReturn;};double CashReturn::acceptCash(double money){double result = money;if (money > moneyCondition)result = money - (int)(money / moneyCondition)* moneyReturn;return result;}class CashContext{public :CashContext(CashSuper *cashuper) : cs(cashuper){}double GetResult(double money);private:CashSuper *cs;};double CashContext::GetResult(double money){return cs->acceptCash(money);}

The above is the implementation code of the traditional policy mode, but it is only a framework, very nice, not used; no specific environment for execution.

The following is the code that combines the policy mode and the factory mode. The scenario is described above: 1 indicates charging in normal mode; 2 indicates charging in full mode: 300 to 100; 3 indicates off.

Replace the above cashcontext with concretecashcontext to run it directly.

class ConcreteCashContext{public:ConcreteCashContext(int type);double GetResult(double money);private:CashSuper *cs;};double ConcreteCashContext::GetResult(double money){return cs->acceptCash(money);}ConcreteCashContext::ConcreteCashContext(int type){switch(type){case 1:cs = new CashNormal();break;case 2:cs = new CashReturn(300,100);break;case 3:cs = new CashRebate(0.8);break;default:cout << " cant not recognize the type " << endl;}}int main(int argc, char **argv){ConcreteCashContext *ccc = new ConcreteCashContext(2);double money = 850.45;cout << " The moeny is : " << money << endl;cout << "you have to pay : " << ccc->GetResult(money) << endl;system("pause");return 0;}

Refer:

1. Big talk Design Model

2. Objective C ++

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.