Design Pattern: Rule Pattern

Source: Internet
Author: User

Design Pattern: Rule Pattern

In GOF's design patterns: the basis for reusable object-oriented software, policy patterns are like this: Define a series of algorithms and encapsulate them one by one, and make them replaceable. This mode allows algorithms to change independently of customers who use it.

Composition: (1) Strategy-abstract policy role: Policy class, usually implemented by an interface or abstract class.
{
A public interface is defined. Different algorithms implement this interface in different ways. Context uses this interface to call different algorithms, which are generally implemented using interfaces or abstract classes.
}

(2) ConcreteStrategy1, ConcreteStrategy2, ConcreteStrategy3-specific policy role: encapsulates related algorithms and behaviors.
{
Implements the interfaces defined by Strategy and provides specific algorithm implementations.
}

(3) context-Environment role: Hold a reference to the policy class and call it to the user.
{
1. The ConcreteStrategy algorithm is required. 2. Maintain a Strategy instance internally. 3. dynamically sets the specific implementation algorithm of Strategy during runtime. 4. Interaction and data transmission with Strategy.
}


Application scenarios: 1. Multiple classes only differ in performance behavior. You can use the Strategy Mode to dynamically select the behavior to be executed during running. 2. Different policies (algorithms) need to be used in different situations, or they may be implemented in other ways in the future. 3. Hiding implementation details of specific policies (algorithms) from customers is completely independent of each other.
UML diagram:


The Code is as follows:
Here I have combined the simple engineering mode with the policy mode, which can reduce and simplify the new operation of the object;
# Include
 
  
Using namespace std; enum strategy {Strategy1; Strategy2; Strategy3 ;}; // Context: Use a ConcreteStrategy object for configuration. Maintain a reference to the Stategy object, you can define an interface to allow Stategy to access its data. Class context {public: context (strategy type) {switch type {case Strategy1: pStrategy = new ConcreteStrategy1 (); break; case Strategy2: pStrategy = new ConcreteStrategy2 (); break; case Strategy3: pStrategy = new ConcreteStrategy3 (); break; default: break ;}};~ Context () {delete pStrategy ;}; virtual void ContextInterface () {if (pStrategy) pStrategy-> alogrithmInterface () ;}; private: Strategy * pStrategy; /* data */}; // Strategy base class Strategy {public: Strategy ();~ Strategy (); virtual void alogrithmInterface () = 0;/* data */}; // specific policy 1 class ConcreteStrategy1: Strategy {public: ConcreteStrategy1 ();~ ConcreteStrategy1 (); void alogrithmInterface () {cout <"Im ConcreteStrategy1! "}/* Data */}; // specific policy 2 class ConcreteStrategy2: Strategy {public: ConcreteStrategy2 ();~ ConcreteStrategy2 (); void alogrithmInterface () {cout <"Im ConcreteStrategy2! "}/* Data */}; // specific policy 3 class ConcreteStrategy3: Strategy {public: ConcreteStrategy3 ();~ ConcreteStrategy3 (); void alogrithmInterface () {cout <"Im ConcreteStrategy3! "}/* Data */}; int main (int argc, char const * argv []) {context * cont = new context (Strategy1); cont-> ContextInterface (); delete cont; cont = NULL; cont = new context (Strategy2); cont-> ContextInterface (); delete cont; cont = NULL; cont = new context (Strategy3 ); cont-> ContextInterface (); delete cont; cont = NULL; 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.