Design mode (2)--Strategy mode

Source: Internet
Author: User
Tags case statement

Tag:type   div    include    cond    call     log   efault    modify    csu   

2. Policy mode//ver1//Cash charges abstract class Cashsuper{public:cashsuper () {}~cashsuper () {}public:virtual double Acceptcash (double Money) {return money;}};/ /normal charge class Cashnormal:public Cashsuper{public:cashnormal () {}~cashnormal () {}public:virtual double Acceptcash ( Double money) {return money;}};/ /Discounted Fees class Cashrebate:public cashsuper{private:double _moneyrebate;public:cashrebate (double moneyrebate) {_ Moneyrebate = moneyrebate;} Virtual Double Acceptcash (double money) {return money * _moneyrebate;}};/ /Rebate Fee class Cashreturn:public cashsuper{private:double _moneycondition;double _moneyreturn;public:cashreturn ( Double moneycondition, double moneyreturn) {_moneycondition = Moneycondition;_moneyreturn = Moneyreturn;} Virtual Double Acceptcash (double money) {Double result = money;if (Money >= _moneycondition) {money = Money-floor (money /_moneycondition) * _moneyreturn; Floor down rounding; Ceil rounding up}return money;}}; Enum Ecashtype{enormal = 1,erebate = 2,ereturn = 3};//Cash charge factory class Cashfactory{public:static Cashsuper Createcashaccept (Ecashtype eType) {Cashsuper * cs = Null;switch (eType) {case enormal:cs = new Cashnormal (); Break;case EReb Ate:cs = new Cashrebate (0.8); Break;case Ereturn:cs =new Cashreturn (+); break;default:break;} return *cs;}; void Main1 () {Double total = 0.0; Cashsuper csuper = cashfactory::createcashaccept (erebate); total = Csuper.acceptcash (350); The inconvenience here is that there is no control over how many folds; Parameters cannot be passed in;//solution: Here the processing is defined in the definition of enumeration, such as 80 percent eRebate8, 50 percent eRebate5; Add the corresponding Case statement;//Disadvantage: The factory contains all the charging methods, each add one or modify a charge method, you need to change the factory content;}

Policy mode: Define the algorithm family, separately encapsulated, so that they can replace each other, this mode allows the algorithm to change, does not affect the algorithm of the customer.

2. Strategy mode//abstract algorithm class strategy{public:virtual void algorithminterface () = 0;};/ /Specific algorithm aclass concretestrategya:public strategy{public:virtual void Algorithminterface () {//algorithm a}};//specific algorithm Bclass Concretestrategyb:public strategy{public:virtual void Algorithminterface () {//algorithm b}};//context; for managing Strategyclass context {Private:strategy * _STRATEGY;PUBLIC:CONTEXT (Strategy * strategy) {_strategy = strategy;} Context interface void Contextinterface () {_strategy->algorithminterface ();}}; void Main21 () {Context *pcontext = new Context (new Concretestrategya ());p context->contextinterface ();p Context = new Context (New Concretestrategyb ());p context->contextinterface ();} 2. Policy mode//ver2//Cash charges abstract class Cashsuper{public:cashsuper () {}~cashsuper () {}public:virtual double Acceptcash (double Money) {return money;}};/ /normal charge class Cashnormal:public Cashsuper{public:cashnormal () {}~cashnormal () {}public:virtual double Acceptcash ( Double money) {return money;}};/ /Discounted Fees class Cashrebate:public cashsuper{private:double _moneyrebate;publIc:cashrebate (double moneyrebate) {_moneyrebate = moneyrebate;} Virtual Double Acceptcash (double money) {return money * _moneyrebate;}};/ /Rebate Fee class Cashreturn:public cashsuper{private:double _moneycondition;double _moneyreturn;public:cashreturn ( Double moneycondition, double moneyreturn) {_moneycondition = Moneycondition;_moneyreturn = Moneyreturn;} Virtual Double Acceptcash (double money) {Double result = money;if (Money >= _moneycondition) {money = Money-floor (money /_moneycondition) * _moneyreturn; Floor down rounding; Ceil rounding up}return money;}}; Enum Ecashtype{enormal = 1,erebate = 2,ereturn = 3};class Cashcontext{private:cashsuper _csuper;public:cashcontext ( Cashsuper cs) {_csuper = cs;} Double GetResult (double money) {return _csuper.acceptcash (money);}};/ /Client call void Main22 () {Cashcontext *PCC = NULL; Cashsuper *pcs = Null;ecashtype EType = Erebate;switch (eType) {case enormal:pcs = new Cashnormal (); *PCC = Cashcontext (*pcs) ; Break;case erebate:pcs = new Cashrebate (0.8); *PCC = Cashcontext (*pcs); case EREturn:pcs = new Cashreturn (+), *PCC = Cashcontext (*pcs);d Efault:break;} Double Money = 350;double result = Pcc->getresult (money);//Disadvantage: The judgment process is placed on the client, exposing too much detail;}

  

Design mode (2)--Strategy mode

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.