Strategy ):
It defines the algorithm family and encapsulates them separately so that they can replace each other. This pattern changes the algorithm and does not affect the customers who use the algorithm.
Structure:
// The Strategy abstract class that defines the public interfaces of all supported algorithms.
Class strategy
{
Public:
Strategy (){};
// Algorithm Method
Virtual void algorithminterface (){};
};
// Encapsulates specific algorithms or actions that inherit from strategy
// Algorithm
Class concretestrategya: public strategy
{
Public:
Concretestrategya (){};
// Algorithm A Implementation Method
Virtual void algorithminterface ()
{
Cout <"algorithm A Implementation Method" <Endl;
}
};
// Algorithm B
Class concretestrategyb: public strategy
{
Public:
Concretestrategyb (){};
// Algorithm B Implementation Method
Virtual void algorithminterface ()
{
Cout <"algorithm B Implementation Method" <Endl;
}
};
// Specific algorithm C
Class concretestrategyc: public strategy
{
Public:
Concretestrategyc (){};
// Algorithm C Implementation Method
Virtual void algorithminterface ()
{
Cout <"algorithm C Implementation Method" <Endl;
}
};
// Context: Use a concretestrategy to configure and maintain a reference to the strategy object.
Class Context
{
PRIVATE:
Strategy * m_strategy;
Public:
Context (){};
// Input a specific policy object during initialization
Context (Strategy * Strategy): m_strategy (Strategy)
{
}
// Method of calling the algorithm based on the specific policy object
Void contextinterface ()
{
M_strategy-> algorithminterface ();
}
};
// Client code
Int main ()
{
Context * context;
Strategy * m_strategy = new concretestrategya ();
Context = new context (m_strategy );
Context-> contextinterface ();
Delete context;
Delete m_strategy;
M_strategy = new concretestrategyb ();
Context = new context (m_strategy );
Context-> contextinterface ();
Delete context;
Delete m_strategy;
M_strategy = new concretestrategyc ();
Context = new context (m_strategy );
Context-> contextinterface ();
Delete context;
Delete m_strategy;
Return 0;
}
Rule mode implementation
# Include "stdafx. H"
# Include "stdio. H"
# Include <memory>
Using namespace STD;
// A supermarket discount instance
// Abstract class
Class cashsuper
{
Public:
Virtual double acceptcash (double money) = 0;
};
// Normal billing subclass
Class cashnormal: Public cashsuper
{
Public:
Virtual double acceptcash (double money)
{
Return money;
}
};
// Discount fee subclass
Class cashrebate: Public cashsuper
{
PRIVATE:
Double moneyrebate;
Public:
Cashrebate (double rebate = 0.0): moneyrebate (rebate)
{
}
Virtual double acceptcash (double money)
{
Return money * moneyrebate;
}
};
// Rebate billing subclass
Class cashreturn: Public cashsuper
{
PRIVATE:
Double moneycondition;
Double moneyreturn;
Public:
Cashreturn (double condition = 0.0, double return = 0.0): moneycondition (condition ),
Moneyreturn (return)
{
}
Virtual double acceptcash (double money)
{
Double result = money;
If (money> = moneycondition)
Result = money-(money/moneycondition) * moneyreturn;
Return result;
}
};
Class cashcontext
{
PRIVATE:
Auto_ptr <cashsuper> Cs;
Public:
Cashcontext (){};
// Combine simple factory and Policy Modes
Void createcashaccept (INT type)
{
Switch (type)
{
// Normal charges
Case 0:
Cs = auto_ptr <cashsuper> (New cashnormal ());
Break;
Case 1:
Cs = auto_ptr <cashsuper> (New cashreturn (300,100 ));
Break;
Case 2:
Cs = auto_ptr <cashsuper> (New cashrebate (0.8 ));
Break;
}
}
Double getresult (double money)
{
Return CS-> acceptcash (money );
}
};
Int main (INT argc, char * argv [])
{
Cashcontext * cs = new cashcontext ();
CS-> createcashaccept (0 );
Printf ("% 2.2f/N", CS-> getresult (1000 ));
Delete Cs;
Return 0;
}
Summary:
Policy mode is a method that defines a series of algorithms. From a conceptual point of view, all these algorithms perform the same job, but they only implement differently, it can call all algorithms in the same way, reducing the coupling between various algorithm classes and algorithm classes. The strategy class hierarchy of the Policy mode defines a series of reusable algorithm behaviors for context. Inheritance helps you analyze the public functions of these algorithms.
Another advantage of the Policy mode is that unit testing is simplified because each algorithm has its own class and can be tested independently through its own interface.