Strategy mode (component collaboration type)

1. Motivation of the strategy mode During the
software construction process, the algorithms used by some objects may often change . If these algorithms are written into the object, the object will become extremely complicated, and sometimes it is also a problem to support inapplicable algorithms. Performance burden. So we need a method ( strategy mode ) that can change the object's algorithm as needed at runtime to decouple the object from the algorithm.

2. The application scenario of the strategy mode.
When a code structure like "if...else if...else" appears in the method of the object, with the development of time, the demand may undergo various changes. We need to respond to the "if... Else if...else" this kind of code is modified or extended, but this would violate the "open and closed principle". We should find ways to extend the original software instead of modifying it. At this time, we can use the " strategic model " to deal with this change in demand.

3. Mode definition
Define a series of algorithms, encapsulate them one by one, and make them interchangeable (change). This mode allows the algorithm to change (extend, subclass) independently of the client program (stable). -"Design Patterns" GoF

4. Structure diagram
Structure chart

5. Summary of main points
(1) Strategy and its subclasses provide a series of reusable algorithms for components, so that the type can be easily switched between various algorithms as needed at runtime.
(2) The Strategy mode provides an alternative to using conditional judgment statements. Eliminating conditional judgment statements is decoupling. Code containing many conditional judgment statements usually requires Strategy mode.
(3) If the Strategy object has no instance variables, then each context can share the same Strategy object, thereby saving object overhead.

6.
Advantages and disadvantages (1) Advantages: suitable for members of the classMainly based on methods, algorithms often change; Simplified unit testing (because each algorithm has its own class, it can be tested separately through its own interface). The strategy mode is basically the same as the simple factory, but the simple factory mode can only solve the problem of object creation, and the strategy mode should be used for algorithms that change frequently.
(2) Disadvantages: The client has to make judgments.

7. Code example

//策略基类
class COperation 
{
    
     
public: 
	int m_nFirst;
	int m_nSecond; 
	virtual double GetResult()
	{
    
     
		double dResult = 0; 
		return dResult; 
	} 
	virtual ~COperation(){
    
    } 
};

//策略具体类—加法类
class AddOperation : public COperation 
{
    
    
public: 
	AddOperation(int a,int b) 
	{
    
     
		m_nFirst =
		m_nSecond =} 
	virtual double GetResult()
	{
    
     
		return m_nFirst + m_nSecond; 
	} 
};

class Context 
{
    
     
private:
	COperation* op; //strategy的多态调用,可以是指针或者引用(不推荐),不能是一个对象,否则会失去多态性
public: 
	Context(COperation* temp)//这里可以和工厂方法结合使用,直接使用工厂类在类中生成所需的策略对象
	{
    
     
		op = temp; 
	} 
	double GetResult()
	{
    
     
		return op­->GetResult(); 
	} 
};
//客户端
int main()
{
    
     
	int a,char
	cin>>a>>
	cout<<”请输入运算符:; 
	cin>>switch(c)
	{
    
     
	case+: 
		Context *context=new Context(new AddOperation(a,b)); 
		cout<<context-­>GetResult()<<endl; 
		break; 
	default: 
		break; 
	}
	return 0; 
} 

Guess you like

Origin blog.csdn.net/gaggagaasd/article/details/107565955