policy mode (strategy): It defines a series of algorithms, encapsulates each algorithm, and allows them to be replaced with each other. The policy pattern allows the algorithm to change without affecting the customer using the algorithm. (Original: The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients this use it.)
UML diagram
Advantages:
1, simplified unit testing, because each algorithm has its own class, can be tested by their own interface alone.
2, avoid the use of multiple conditional transfer statements in the program, make the system more flexible, and easy to expand.
3, abide by most grasp principles and common design principles, high cohesion, low coupling.
Disadvantages:
1, because each specific policy class will produce a new class, it will increase the number of classes that the system needs to maintain.
2, in the basic strategy mode, select the specific implementation of the responsibility by the client object, and transferred to the policy mode of the context object
Source:
Strategy.h
[CPP]View PlainCopyprint?
- #include <iostream>
- #include <string>
- #include <memory>
- Using namespace std;
- Strategy abstract class, used as an interface
- Class strategy
- {
- Public
- virtual string Substitute (string str) = 0;
- virtual ~strategy ()
- {
- cout<<"in the destructor of strategy" <<endl;
- }
- };
- Class Chinesestrategy: PublicStrategy
- {
- Public
- String substitute (String str)
- {
- int Index=str.find ("520");
- String Tempstr=str.replace (index,3,"I Love You");
- return tempstr;
- }
- ~chinesestrategy ()
- {
- cout<<"in the destructor of Chinesestrategy" <<endl;
- }
- };
- Class Englishstrategy: PublicStrategy
- {
- Public
- String substitute (String str)
- {
- int Index=str.find ("520");
- String Tempstr=str.replace (index,3,"I love ou");
- return tempstr;
- }
- ~englishstrategy ()
- {
- cout<<"in the destructor of Chinesestrategy" <<endl;
- }
- };
- Context class
[CPP]View PlainCopyprint?
- Class Translator
- {
- Private
- Auto_ptr<strategy> strategy;
[CPP]View PlainCopyprint?
- //Add a pointer to the algorithm (stategy) type in the customer code.
- Public
- ~translator ()
- {
- cout<<"in the destructor of Translator" <<endl;
- }
- void Set_strategy (auto_ptr<strategy> strategy)
- {
- this->strategy=strategy;
- }
- String translate (String str)
- {
- if (0==strategy.get ())
- return "";
- return Strategy->substitute (str);
- }
- };
Strategy.cpp
[CPP]View PlainCopyprint?
- #include "Strategy.h"
- int main (int argc, char *argv)
- {
- String str ("321520");
- Translator *translator=New Translator;
- //When strategy is not specified
- cout<<"No strategy" <<endl;
- Translator->translate (str);
- cout<<"---------------" <<endl;
- //Translate into Chinese
- auto_ptr<strategy> S1 (new chinesestrategy);
- Translator->set_strategy (S1);
- cout<<"Chinese strategy" <<endl;
- Cout<<translator->translate (str) <<endl;
- cout<<"---------------" <<endl;
- //Translate into English
- Auto_ptr<strategy> S2 (new Englishstrategy);
- Translator->set_strategy (S2);
- cout<<"中文版 strategy" <<endl;
- Cout<<translator->translate (str) <<endl;
- cout<<"----------------" <<endl;
- Delete translator;
- return 0;
- }
Strategy mode and C + + implementation