Design mode – The strategy mode is based on the calculator
The most direct addition and subtraction
Public class Calculator{// Add symbol private final static String ADD_SYMBOL ="+"; Private final static String SUB_SYMBOL ="-"; // Execute public intexec(int a, int b, String symbol){
int result = 0;
if(symbol.equals(ADD_SYMBOL)){// Compare result = this.add(a,b); }else if(symbol.equals(SUB_SYMBOL)){ result = this.sub(a,b); }} public int add(int a, int b){returna + b; } public int sub(int a, int b){returna - b; }}Copy the code
Writing scene class
Public class Client{public static void main(String[] args){// Get input int a = integer.parseInt (args[0]); int b = Integer.parseInt(args[1]); // Create Calculator = new Calculator(); calculator.exec(a,b); }}Copy the code
Introduce the policy pattern
That is, the original addition and subtraction method is split into the strategy mode
protected interface Calculator{
public int exec(int a, int b);
}
Copy the code
Write specific strategies
Public class Add Calculator{// Add @overrider public intexec(int a, int b){
returna + b; }}Copy the code
Writing subtraction operation
Public class implements Calculator{// subtract public intexec(int a, int b){
returna - b; }}Copy the code
why? The reason for encapsulation is that a context is needed to ensure that specific policies can be added at any time.
public class Context{ private Calculator cal = null; // Inject public Context(Calculator _cal){this. CAL = _cal; // finish pointing to} // execute public intexec(int a, int b){
returnthis.cal.exec(a,b); }}Copy the code
scenario
Public class Client{// add public final static String ADD_SYMBOL ="+"; Public final static String SUB_SYMBOL ="-"; public static void main(String[] args){ int a = Integer.parseInt(args[0]); String symbol = args[1]; int b = Integer.parseInt(args[2]); // Initialize Context Context = null; // Select the policyif(symbol.equals(ADD_SYMBOL)){
context = new Context(new Add());
}else if(symbol.equals(SUB_SYMBOL)){ context = new COntext(new SUb()); } // result context.exec(a,b); }}Copy the code
Use enumerated
Public enum Calculator{// enumerator // enumerator ADD("+"){
public int exec(int a, int b){
returna + b; }} // enumerate SUB("-"){
public int exec(int a, int b){
returna - b; }} // Define type String value =""; // Construct private Calculator(String _value){this.value = _value; } // get public StringgetValue() {returnthis.value; } // reserve extension public abstract intexec(int a. int b);
}
Copy the code
Finally write the scene
public class Client{ public static void main(String[] args){ int a = Integer.parseInt(args[0]); String symbol = args[1]; int b = Integer.parseInt(args[2]); Calculator.ADD.exec(a, b); }}Copy the code
conclusion
Split the originally coupled IF into classes and inject the classes when you use them. Or use enumerated types, and implement abstract methods if you need to augment them.
application
The implementation of animation, will be a variety of slow function, write as a strategy mode, and then use when the class can be injected. Form validation can also be decoupled and injected when used.