Strategy-Rule Mode

Source: Internet
Author: User

Strategy-Rule Mode
DefinitionDefine a series of algorithms, encapsulate them one by one, and make them replace each other. The policy pattern allows algorithms to change independently of users.
CaseFor example, a file Editor can be saved in any format when it is saved. All file content conversion is provided by Convertor, and different content conversion support is provided based on different options: TXTConvertor, PDFConvertor, DOCConvertor, and so on. Editor maintains a reference to the Convertor object. Once you want to convert the file content, it will hand over its responsibilities to the Convertor object. Separating the conversion algorithm from the Editor can simplify the Code complexity, facilitate maintenance, and effectively expand new algorithms. You do not have to use a public base class when implementing it. You can also use the template method. Here the base class method is used:

Maintain a Convertor object in the Editor class and call the related methods of this object when saving:

 
 
  1. class Editor
  2. {
  3. public:
  4. void save();
  5. void setConvertor(Convertor* convertor) { m_convertor = convertor; }
  6. private:
  7. string m_text;
  8. Convertor* m_convertor;
  9. };
  10. void Editor::save()
  11. {
  12. String text = m_convertor->convert(m_text);
  13. // save text to file.
  14. }
Convertor provides interfaces for the Editor to call:
 
 
  1. class Convertor
  2. {
  3. public:
  4. vritual string& convert(const string& text) = 0;
  5. };
Its subclass implements the method differently:
 
 
  1. class TXTConvertor : public Convertor
  2. {
  3. public:
  4. virtual string& convert(const string& text)
  5. {
  6. // convert to txt data.
  7. }
  8. };
  9. class PDFConvertor : public Convertor
  10. {
  11. public:
  12. virtual string& convert(const string& text)
  13. {
  14. // convert to pdf data.
  15. }
  16. };
  17. class DOCConvertor : public Convertor
  18. {
  19. public:
  20. virtual string& convert(const string& text)
  21. {
  22. // convert to doc data.
  23. }
  24. };
In use, set different Convertor according to different situations:
 
 
  1. Edtior* editor = new Editor();
  2. Convertor* txtConvertor = new TXTConvertor();
  3. editor->setConvertor(txtConvertor);
  4. editor->save();
  5. ...
  6. Convertor* pdfConvertor = new PDFConvertor();
  7. editor->setConvertor(pdfConvertor);
  8. editor->save();
ApplicabilityIf many classes only have different behaviors, you need to use different variants of an algorithm to use data that you do not know, policy mode avoids exposing complex algorithm-related data structures. A class defines multiple behaviors, and these behaviors appear in this class in the form of multiple conditional statements.

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.