Strategy mode and C + + implementation

Source: Internet
Author: User

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?
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4. Using namespace std;
  5. Strategy abstract class, used as an interface
  6. Class strategy
  7. {
  8. Public
  9. virtual string Substitute (string str) = 0;
  10. virtual ~strategy ()
  11. {
  12. cout<<"in the destructor of strategy" <<endl;
  13. }
  14. };
  15. Class Chinesestrategy: PublicStrategy
  16. {
  17. Public
  18. String substitute (String str)
  19. {
  20. int Index=str.find ("520");
  21. String Tempstr=str.replace (index,3,"I Love You");
  22. return tempstr;
  23. }
  24. ~chinesestrategy ()
  25. {
  26. cout<<"in the destructor of Chinesestrategy" <<endl;
  27. }
  28. };
  29. Class Englishstrategy: PublicStrategy
  30. {
  31. Public
  32. String substitute (String str)
  33. {
  34. int Index=str.find ("520");
  35. String Tempstr=str.replace (index,3,"I love ou");
  36. return tempstr;
  37. }
  38. ~englishstrategy ()
  39. {
  40. cout<<"in the destructor of Chinesestrategy" <<endl;
  41. }
  42. };
  43. Context class
[CPP]View PlainCopyprint?
    1. Class Translator
    2. {
    3. Private
    4. Auto_ptr<strategy> strategy;
[CPP]View PlainCopyprint?
  1. //Add a pointer to the algorithm (stategy) type in the customer code.
  2. Public
  3. ~translator ()
  4. {
  5. cout<<"in the destructor of Translator" <<endl;
  6. }
  7. void Set_strategy (auto_ptr<strategy> strategy)
  8. {
  9. this->strategy=strategy;
  10. }
  11. String translate (String str)
  12. {
  13. if (0==strategy.get ())
  14. return "";
  15. return Strategy->substitute (str);
  16. }
  17. };


Strategy.cpp

[CPP]View PlainCopyprint?
    1. #include "Strategy.h"
    2. int main (int argc, char *argv)
    3. {
    4. String str ("321520");
    5. Translator *translator=New Translator;
    6. //When strategy is not specified
    7. cout<<"No strategy" <<endl;
    8. Translator->translate (str);
    9. cout<<"---------------" <<endl;
    10. //Translate into Chinese
    11. auto_ptr<strategy> S1 (new chinesestrategy);
    12. Translator->set_strategy (S1);
    13. cout<<"Chinese strategy" <<endl;
    14. Cout<<translator->translate (str) <<endl;
    15. cout<<"---------------" <<endl;
    16. //Translate into English
    17. Auto_ptr<strategy> S2 (new Englishstrategy);
    18. Translator->set_strategy (S2);
    19. cout<<"中文版 strategy" <<endl;
    20. Cout<<translator->translate (str) <<endl;
    21. cout<<"----------------" <<endl;
    22. Delete translator;
    23. return 0;
    24. }

Strategy mode and C + + implementation

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.