C + + policy mode

Source: Internet
Author: User

Policy mode: It defines the algorithm family, which is encapsulated separately, so that they can be replaced with each other, and this pattern will not affect other customers with the change of algorithm.  This mode is actually what we usually write code, in fact, the design pattern is to tell you how to write code, it is not what to move to use the scheme, if so, for the birds do not directly write in the library as a framework call, because it only provides a solution. It just tells you how to write the code when you have similar requirements to it. The code is well-extensible, easy to maintain, and has a low degree of coupling between classes. The strategy model caters to several object-oriented design ideas:1. Find out where changes may be needed in your application, separate them, and don't mix with code that doesn't need to change. 2, multi-use combination, less with inheritance. 3, for the interface and not to implement programming.
Cite the example in Head first design mode: If we want to set up a duck class: in the base class, there are fly (), swim (), quack () These function interfaces, and then in the subclass as long as the implementation of these interfaces is OK. But here's the problem, for example, we're going to implement a "dry duck" and a "rubber duck". You will find that the sense of logic in the design is a little less right, like duck will be called? Does it have a quack () or fly () function interface? And the duck will have swim () method?    These must be wrong. The problem is that this design approach is not quite consistent: 2, multi-use combination, less with inheritance. principles, the use of inheritance is not so flexible.
Or you can design this: it might be a good design, separate fly () and swim out as a class, and then inherit individually, so the design fits:1. Find out where changes may be needed in your application, separate them, and don't mix with code that doesn't need to change. But the Acton class should also be duck, and the action should belong to the Duck class. Separate to inherit, it may not have much effect on the implementation, but it seems logically not very good, as if someone had to "quartered" you.
So, in summary, this design: that is, the action as a member of the duck, and then in its own method to invoke the interface in action.
    1. voidDuck::peformfly(){
    2. this->action.fly();
    3. }
Above the block diagram, the code is implemented.
  1. #include<iostream>
  2. #include<string>
  3. classStratery{
  4. public:
  5. Stratery(){
  6. }
  7. virtualvoidAlgorithmInterface(){
  8. std::cout<<"AlgorithmInterface"<<std::endl;
  9. }
  10. };
  11. classConcreteStrateryA:publicStratery{
  12. public:
  13. virtualvoidAlgorithmInterface(){
  14. std::cout<<"ConcreteStrateryA"<<std::endl;
  15. }
  16. };
  17. classConcreteStrateryB:publicStratery{
  18. public:
  19. virtualvoidAlgorithmInterface(){
  20. std::cout<<"ConcreteStrateryB"<<std::endl;
  21. }
  22. };
  23. classConcreteStrateryC:publicStratery{
  24. public:
  25. virtualvoidAlgorithmInterface(){
  26. std::cout<<"ConcreteStrateryC"<<std::endl;
  27. }
  28. };
  29. classContext{
  30. private:
  31. Stratery*stratery;
  32. public:
  33. Context(Stratery*_stratery):stratery(_stratery){
  34. }
  35. voidContextInterface(){
  36. stratery->AlgorithmInterface();
  37. }
  38. };
  39. int main(){
  40. Context contextA(newConcreteStrateryA());
  41. contextA.ContextInterface();
  42. Context contextB(newConcreteStrateryB());
  43. contextB.ContextInterface();
  44. Context contextC(newConcreteStrateryB());
  45. contextC.ContextInterface();
  46. }
The above implementation is the classic use of the policy pattern. which
    1. ConcreteStrateryA
    1. ConcreteStrateryB
    1. ConcreteStrateryC
is the algorithm family. There is no coupling between these algorithms. This is also one of the key points of the strategy model. It can be said that the policy pattern is used to encapsulate the algorithm, but it can also be extended to all other requirements similar to the class. For example, in the preceding Duck class, the algorithm encapsulated in the Duck class is the action.



Null



C + + policy 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.