Source: http://ttitfly.iteye.com/blog/136467
1. Taking an arithmetic operation as an example, the traditional practice is:
Java code
Package Org.common; public class Calculate {public String getresult (float a,float b,char c) { float add = a+b; Float Sub = A-B; float mult = a*b; FLOAT Division = A/b; Switch (c) {case ' + ': return ' Adds the result: "+ add; Case '-': return ' Subtracts the result: "+ sub; Case ' * ': return ' Multiplies the result: ' + mult; Case '/': return ' divides the result: ' + Division; Default: return "error";}}}
Java code:
PackageOrg.common; Public classTest { Public Static voidMain (string[] args) {floatA = 200; floatb = 50; Calculate Cal=NewCalculate (); System.out.println (Cal.getresult (A, B,+)); System.out.println (Cal.getresult (A, B,‘-‘)); System.out.println (Cal.getresult (A, B,‘*‘)); System.out.println (Cal.getresult (A, B,‘/‘)); System.out.println (Cal.getresult (A, B,‘(‘)); } }
The result of the operation is:
The sum result is: 250.0
The subtraction result is: 150.0
The result of multiplying is: 10000.0
Divide the result to: 4.0
Error
2. Use the strategy mode to achieve:
The intent of the policy pattern is to encapsulate each algorithm in a separate class with a common interface for a set of algorithms, so that they can be replaced with each other.
The policy pattern allows the algorithm to change without affecting the client. Using the policy model, you can separate behavior from the environment.
The Environment class is responsible for maintaining and querying the behavior classes, and various algorithms are provided in the specific strategy. Because the algorithm and the environment are independent, the modification of the algorithm will not affect the environment and the client
Policy mode:
Made up of three parts
A: Abstract policy role: A policy class, usually implemented by an interface or abstract class
B: Specific policy roles: wrapping up related algorithms and behaviors
C: Environment role: holds a reference to a policy class that is eventually called to the client.
An abstract policy role:
Java code
Package org.strategy; /** * Abstract policy role * /public abstractclass Abstractstrategy { publicabstract String calculate (float A,float b); }
Several specific policy roles:
Java code
Package org.strategy; /** * Specific policy roles */public classextends abstractstrategy{ public String calculate (float A,float b) { float result = A-b ; return "Subtract result:" + result; } }
Java code
Package org.strategy; /** * Specific policy roles */public classextends abstractstrategy{ public String calculate (float A,float b) { float result = a +b ; return "Add Result:" + result; } }
Java code
Package org.strategy; /** * Specific policy roles */public classextends abstractstrategy{ public String calculate (float A,float b) { float result = A *b ; return "Multiply result:" + result; } }
Java code
Package org.strategy; /** * Specific policy roles */public classextends abstractstrategy{ public String calculate (float A,float b) { float result = A/b ; return "Divide result:" + result; } }
Environmental role:
Java code
PackageOrg.strategy; /*** Environment role, which is ultimately called to the client*/ Public classContextrole {/*** Have a reference to a policy class*/ PrivateAbstractstrategy Abstactstrategy; Publiccontextrole (Abstractstrategy abstactstrategy) { This. Abstactstrategy =Abstactstrategy; } PublicString Calculate (floatAfloatb) {String result=Abstactstrategy.calculate (A, b); returnresult; } }
Client calls:
Java code
PackageOrg.strategy; /*** Client*/ Public classTest { Public Static voidMain (string[] args) {floatA = 200; floatB = 25; Contextrole ContextRole1=NewContextrole (Newaddstrategy ()); System.out.println (Contextrole1.calculate (A, b)); Contextrole ContextRole2=NewContextrole (Newsubstrategy ()); System.out.println (Contextrole2.calculate (A, b)); Contextrole ContextRole3=NewContextrole (Newmultstrategy ()); System.out.println (Contextrole3.calculate (A, b)); Contextrole contextRole4=NewContextrole (Newdivisionstrategy ()); System.out.println (Contextrole4.calculate (A, b)); } }
The output is:
The sum result is: 225.0
The subtraction result is: 175.0
The result of multiplying is: 5000.0
Divide the result to: 8.0
Summarize:
Strategy Mode Advantages:
1. It is convenient to dynamically change the algorithm or behavior
2. Avoid using multiple conditional transfer statements
Policy Mode Disadvantages:
1. The client must know all of the policy classes and decide for itself which policy class to use.
2. Cause a lot of strategy classes.
The policy mode in Java strategy