Rule Mode C ++ implementation

Source: Internet
Author: User

First look at the UML class diagram of the Policy mode:

 

The class diagram shows that there is no difference between the policy mode and the simple factory mode. From my understanding, the two major differences are:The simple factory mode is used to realize the diversity of objects, while the policy mode is suitable for class members to focus on methods. The simple factory mode can only solve the problem of object creation, and the policy mode should be used for frequently changing algorithms.

 

Let's take a look at the Code:

CPP Code
  1. // Policy base class
  2. ClassCoperation
  3. {
  4. Public:
  5. IntM_nfirst;
  6. IntM_nsecond;
  7. Virtual DoubleGetresult ()
  8. {
  9. DoubleDresult = 0;
  10. ReturnDresult;
  11. }
  12. };
  13. // Policy-specific class-addition class
  14. ClassAddoperation:PublicCoperation
  15. {
  16. Public:
  17. Addoperation (IntA,IntB)
  18. {
  19. M_nfirst =;
  20. M_nsecond = B;
  21. }
  22. Virtual DoubleGetresult ()
  23. {
  24. ReturnM_nfirst + m_nsecond;
  25. }
  26. };
  27. ClassContext
  28. {
  29. Private:
  30. Coperation * op;
  31. Public:
  32. Context (coperation * temp)
  33. {
  34. OP = temp;
  35. }
  36. DoubleGetresult ()
  37. {
  38. ReturnOp-> getresult ();
  39. }
  40. };
  41. // Client
  42. IntMain ()
  43. {
  44. IntA, B;
  45. CharC;
  46. Cin> A> B;
  47. Cout <"Enter the operator :;
  48. Cin> C;
  49. Switch(C)
  50. {
  51. Case'+ ':
  52. Context * context =NewContext (NewAddoperation (a, B ));
  53. Cout <context-> getresult () <Endl;
  54. Break;
  55. Default:
  56. Break;
  57. }
  58. Return0;
  59. }
// Policy base class coperation {public: int m_nfirst; int m_nsecond; virtual double getresult () {double dresult = 0; return dresult ;}}; // policy-specific class-addition class addoperation: Public coperation {public: addoperation (int A, int B) {m_nfirst = A; m_nsecond = B;} virtual double getresult () {return m_nfirst + m_nsecond ;}; class context {PRIVATE: coperation * op; public: Context (coperation * temp) {op = temp;} double getresult () {return op-> getresult () ;};// client int main () {int A, B; char C; CIN >>>a> B; cout <"Enter the operator:; CIN> C; Switch (c) {Case '+': Context * context = new context (New addoperation (a, B )); cout <context-> getresult () <Endl; break; default: break;} return 0 ;}

For convenience, I only put an addition class here. You can inherit a subtraction, multiplication, and so on, and then add the relevant classification in the main function switch. Here, we also see the policy MethodDisadvantages: All operations are judged on the client, and the customer's tasks are added.

As we all know, the simple factory model is just to concentrate all the judgment operations on the factory class, so we can think of combining the two models, and the following model emerged-the combination of strategy and factory model, the code is modified based on the above Code:

CPP Code
  1. ClassContext
  2. {
  3. Private:
  4. Coperation * op;
  5. Public:
  6. Context (CharCtype)
  7. {
  8. Switch(Ctype)
  9. {
  10. Case'+ ':
  11. OP =NewAddoperation (3, 8 );
  12. Break;
  13. Default:
  14. OP =NewAddoperation ();
  15. Break;
  16. }
  17. }
  18. DoubleGetresult ()
  19. {
  20. ReturnOp-> getresult ();
  21. }
  22. };
  23. // Client
  24. IntMain ()
  25. {
  26. IntA, B;
  27. Cin> A> B;
  28. Context * test =NewContext ('+ ');
  29. Cout <Test-> getresult () <Endl;
  30. Return0;
  31. }
Class context {PRIVATE: coperation * op; public: Context (char ctype) {Switch (ctype) {Case '+': op = new addoperation (3,8); break; default: OP = new addoperation (); break ;}} double getresult () {return op-> getresult () ;}; // client int main () {int A, B; cin> A> B; Context * test = new context ('+'); cout <Test-> getresult () <Endl; return 0 ;}

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.