1. Definition
Policy mode: Defines a set of algorithms that encapsulate each algorithm so that they can be replaced with each other. The policy pattern allows the algorithm to change independently of the client that calls it. The policy pattern enables this set of algorithms to
change when the client calls them.
2. There are three characters in the strategy mode
(1) abstract strategy (strategy): typically implemented by an interface or abstract class. A common interface that defines a number of specific policies, the different algorithms in a particular policy class implement this interface in different ways, and the context uses these interfaces to invoke algorithms of different implementations.
(2) Specific strategy (CONCRETESTRATEGY): Implement the Strategy interface or inherit from the abstract class strategy, encapsulating the specific algorithm and behavior.
(3) Environment Class (Contex): holds a reference to a public policy interface that is called directly to the client.
3. Structure diagram of the strategy mode
4. The strategy mode realizes the thought
(1) Define a common interface (strategy) for a policy object
1 Public Interface Strategy {2 Public void algorithm (); 3 }
(2) Define a specific policy class (Concretestrategy) to implement the above interface
Public class Implements Strategy { @Override publicvoid algorithm () { System.out.println ("first algorithm.") ); }}
Public class Implements Strategy { @Override publicvoid algorithm () { System.out.println ( "Second algorithm." ); } }
(3) Define an environment class (Contex), which holds a reference to the public interface, and the corresponding get, set method, constructor method
Public classContext {strategy strategy; PublicContext (Strategy strategy) {//constructor Function Super(); This. Strategy =strategy; } PublicStrategy Getstrategy () {//Get Method returnstrategy; } Public voidSetstrategy (Strategy strategy) {//Set Method This. Strategy =strategy; } Public voidalgorithm () {strategy.algorithm (); }}
Finally, the client freely invokes the policy ^_^
Public classstrategyclient { Public Static voidMain (string[] args) {//Selecting a policy using a constructorContext context=NewContext (Newfirststrategy ()); Context.algorithm (); //using the GET, set method to switch policies//Context Context=new context (); //Contex.set (New Firststrategy ()); //Context.algorithm (); //Switch to another policyContext secondcontext=NewContext (Newsecondstrategy ()); Secondcontext.algorithm (); //Context Secondcontext=new context (); //Secondcontext.set (New Secondstrategy ()); //secondcontext.algorithm (); }}
5. Summary
(1) The
key point of the strategy mode is what kind of action is performed on what kind of strategy is passed to the object. (2) Strategy mode
Advantages : Can easily expand and change the strategy, can dynamically change the behavior of the object. (3) Strategy mode
disadvantage : The client must know all the policy classes and decide which one to use at its own discretion. Each specific strategy produces a new class, which results in a number of policy classes.
6, I wrote a complete strategy mode demo, decompression can run, the need to leave the mailbox.
----------------------------------I have a bottom line----------------------------------
The strategy mode of Java design pattern