The rule mode is a commonly used design mode. It is mainly embodied in the fact that objects can have certain behaviors, but different implementation methods are used in different scenarios!
Similar situations are often encountered in software development. There are multiple algorithms or policies for implementing a function. We can select different algorithms or policies based on different environments or conditions to complete this function. Such as search and sorting. A common method is Hard Coding in a class. To provide multiple search algorithms, you can write these algorithms into a class, multiple methods are provided in this class. Each method corresponds to a specific search algorithm. Of course, these search algorithms can also be encapsulated in a unified method, through if... Else... Or case and other condition judgment statements for selection. Both of these methods can be called hard encoding. To add a new search algorithm, you need to modify the source code of the encapsulated algorithm class. Replace the search algorithm, you also need to modify the client call code. This algorithm class encapsulates a large number of search algorithms,
This type of code is complex and difficult to maintain. If we include these policies on the client, this approach is not desirable, it will lead to a large client program and difficult to maintain, if there are a large number of available algorithms, the problem will become more serious.
What we need to do is encapsulate each method in different classes and call corresponding methods as needed!
Here we also use two examples to illustrate the problem:
1: how to implement the standard rule mode!
2: guide the story of the Savior Zhao yundong Wu in the classic Zhuge Liang strategy!
First, let's look at the first one. It's easy to understand and shows the general structure of the Policy mode!
[Cpp]
Class StrategyInterface // abstract interface
{
Public:
Virtual void execute () = 0;
};
Class ConcreteStrategyA: public StrategyInterface // method A, which implements the interface, the same below
{
Public:
Virtual void execute ()
{
Cout <"Called ConcreteStrategyA execute method" <endl;
}
};
Class ConcreteStrategyB: public StrategyInterface
{
Public:
Virtual void execute ()
{
Cout <"Called ConcreteStrategyB execute method" <endl;
}
};
Class ConcreteStrategyC: public StrategyInterface
{
Public:
Virtual void execute ()
{
Cout <"Called ConcreteStrategyC execute method" <endl;
}
};
Class Context {// main operation class
Private:
StrategyInterface * _ strategy;
Public:
Context (StrategyInterface * strategy): _ strategy (strategy)
{
}
Void set_strategy (StrategyInterface * strategy)
{
_ Strategy = strategy;
}
Void execute ()
{
_ Strategy-> execute ();
}
};
Int main (int argc, char * argv [])
{
ConcreteStrategyA concreteStrategyA;
ConcreteStrategyB concreteStrategyB;
ConcreteStrategyC concreteStrategyC;
Context contextA (& concreteStrategyA );
Context contextB (& concreteStrategyB );
Context contextC (& concreteStrategyC );
ContextA.exe cute (); // The function internally calls the corresponding method using polymorphism!
ContextB.exe cute ();
ContextC.exe cute ();
ContextA. set_strategy (& concreteStrategyB );
ContextA.exe cute ();
ContextA. set_strategy (& concreteStrategyC );
ContextA.exe cute ();
Return 0;
}
Class StrategyInterface // abstract interface
{
Public:
Virtual void execute () = 0;
};
Class ConcreteStrategyA: public StrategyInterface // method A, which implements the interface, the same below
{
Public:
Virtual void execute ()
{
Cout <"Called ConcreteStrategyA execute method" <endl;
}
};
Class ConcreteStrategyB: public StrategyInterface
{
Public:
Virtual void execute ()
{
Cout <"Called ConcreteStrategyB execute method" <endl;
}
};
Class ConcreteStrategyC: public StrategyInterface
{
Public:
Virtual void execute ()
{
Cout <"Called ConcreteStrategyC execute method" <endl;
}
};
Class Context {// main operation class
Private:
StrategyInterface * _ strategy;
Public:
Context (StrategyInterface * strategy): _ strategy (strategy)
{
}
Void set_strategy (StrategyInterface * strategy)
{
_ Strategy = strategy;
}
Void execute ()
{
_ Strategy-> execute ();
}
};
Int main (int argc, char * argv [])
{
ConcreteStrategyA concreteStrategyA;
ConcreteStrategyB concreteStrategyB;
ConcreteStrategyC concreteStrategyC;
Context contextA (& concreteStrategyA );
Context contextB (& concreteStrategyB );
Context contextC (& concreteStrategyC );
ContextA.exe cute (); // The function internally calls the corresponding method using polymorphism!
ContextB.exe cute ();
ContextC.exe cute ();
ContextA. set_strategy (& concreteStrategyB );
ContextA.exe cute ();
ContextA. set_strategy (& concreteStrategyC );
ContextA.exe cute ();
Return 0;
}
The above example is enough to understand the general implementation method of the Policy mode. Next, let's look at the second question.
First of all, there are three tips in this scenario. Zhao Yun is the performer who opens the tips method, so the main function should be shown in the UML diagram below.
Next let's take a look at how to use C ++ to implement this scenario:
[Cpp]
Class IStrategy
{
Public:
Virtual void Operator () = 0;
};
Class Fun1: public IStrategy
{
Void Operator ()
{
Cout <"Ask Qiao Guoluo for help, so that Sun Quan does not kill Liu Bei" <endl;
}
};
Class Fun2: public IStrategy
{
Void Operator ()
{
Cout <"ask Wu Guotai to open a green light and allow" <endl;
}
};
Class Fun3: public IStrategy
{
Void Operator ()
{
Cout <"find Mrs sun to break the troops and try to escape" <endl;
}
};
Class ContualClass
{
Private:
IStrategy * m_pSrategy;
Public:
ContualClass (){}
ContualClass (IStrategy * pirevt)
{
This-> m_pSrategy = pirevt;
}
Void Operator ()
{
M_pSrategy-> Operator ();
}
};
Int main ()
{
// Remove the first one when I arrived at Wu Guo
Cout <("----------- remove the first -------------" when I first arrived at Wu Guo) <endl;
ContualClass * context = new ContualClass (new Fun1 (); // get the trick
Context-> Operator (); // disassemble and execute
// Liu Bei is happy, but the second one has been removed.
Cout <("----------- Liu Bei is happy, and the second is split -------------") <endl;
Context = new ContualClass (new Fun2 ());
Context-> Operator (); // The second tip is executed.
// What should I do if Sun Quan's soldier is chasing him? Remove the third
Cout <("----------- What should I do if Sun Quan's soldier is behind me? Remove the third ") <endl;
Context = new ContualClass (new Fun3 ());
Context-> Operator (); // Mrs sun withdraws
System ("pause ");
Return 0;
}
Class IStrategy
{
Public:
Virtual void Operator () = 0;
};
Class Fun1: public IStrategy
{
Void Operator ()
{
Cout <"Ask Qiao Guoluo for help, so that Sun Quan does not kill Liu Bei" <endl;
}
};
Class Fun2: public IStrategy
{
Void Operator ()
{
Cout <"ask Wu Guotai to open a green light and allow" <endl;
}
};
Class Fun3: public IStrategy
{
Void Operator ()
{
Cout <"find Mrs sun to break the troops and try to escape" <endl;
}
};
Class ContualClass
{
Private:
IStrategy * m_pSrategy;
Public:
ContualClass (){}
ContualClass (IStrategy * pirevt)
{
This-> m_pSrategy = pirevt;
}
Void Operator ()
{
M_pSrategy-> Operator ();
}
};
Int main ()
{
// Remove the first one when I arrived at Wu Guo
Cout <("----------- remove the first -------------" when I first arrived at Wu Guo) <endl;
ContualClass * context = new ContualClass (new Fun1 (); // get the trick
Context-> Operator (); // disassemble and execute
// Liu Bei is happy, but the second one has been removed.
Cout <("----------- Liu Bei is happy, and the second is split -------------") <endl;
Context = new ContualClass (new Fun2 ());
Context-> Operator (); // The second tip is executed.
// What should I do if Sun Quan's soldier is chasing him? Remove the third
Cout <("----------- What should I do if Sun Quan's soldier is behind me? Remove the third ") <endl;
Context = new ContualClass (new Fun3 ());
Context-> Operator (); // Mrs sun withdraws
System ("pause ");
Return 0;
}
This example is very intuitive. This is often used to explain the policy mode! It's time for dinner. It's just a flash.