Latest web development tutorials

Strategy Mode

In Strategy Mode (Strategy Pattern), a class act, or algorithm can be changed at runtime. This type of design patterns belong behavioral patterns.

In strategy mode, we create objects and a variety of behavioral change strategies as the policy object to change the context object representation. Policy object to change the algorithm execution context object.

Introduction

Intent: The definition of a family of algorithms, encapsulate each one them, and make them interchangeable.

Mainly to solve: In the case of multiple similar algorithm using if ... else brought complex and difficult to maintain.

When to use: a system has many, many categories, and to distinguish them only their immediate behavior.

How to fix: will these algorithms into a package a class arbitrarily replaced.

The key code: implement the same interface.

Application examples: 1, good ideas Zhuge Liang, each kit is a strategy.2, travel way to travel, choose to ride a bike, by car, each way to travel is a strategy. 3, JAVA AWT the LayoutManager.

Advantages: 1, the algorithm can freely switch.2, to avoid the use of multiple conditional. 3, good scalability.

Disadvantages: 1, the Strategy will increase.2, all classes need of foreign policy exposure.

Usage scenarios: 1, if there is a system in which many classes, the difference between them only in their behavior, then use the strategy pattern can dynamically choose to let an act in many behaviors of an object.2, a system needs to dynamically choose one of several algorithms. 3. If an object has a lot of behavior, if that is not the proper mode, these acts had to use multiple conditions are selected statement to achieve.

Note: If the policy of a system of more than four, you need to consider the use of mixed-mode, to solve the problem of expansion policy class.

achieve

We will create a well-defined interface and implementation activities of the entityStrategyStrategyStrategyinterface.Contextis the use of a certain class policy.

StrategyPatternDemo,our demonstration classes usingContextContext and policy objects to demonstrate behavior when it is used to configure or change the policy change.

Strategy pattern UML diagram

step 1

Create an interface.

Strategy.java

public interface Strategy {
   public int doOperation (int num1, int num2);
}

Step 2

Create entity class that implements the interface.

OperationAdd.java

public class OperationAdd implements Strategy {
   @Override
   public int doOperation (int num1, int num2) {
      return num1 + num2;
   }
}

OperationSubstract.java

public class OperationSubstract implements Strategy {
   @Override
   public int doOperation (int num1, int num2) {
      return num1 - num2;
   }
}

OperationMultiply.java

public class OperationMultiply implements Strategy {
   @Override
   public int doOperation (int num1, int num2) {
      return num1 * num2;
   }
}

Step 3

CreateContextclass.

Context.java

public class Context {
   private Strategy strategy;

   public Context (Strategy strategy) {
      this.strategy = strategy;
   }

   public int executeStrategy (int num1, int num2) {
      return strategy.doOperation (num1, num2);
   }
}

Step 4

UseContextto see when it changes the behavior change strategyStrategy.

StrategyPatternDemo.java

public class StrategyPatternDemo {
   public static void main (String [] args) {
      Context context = new Context (new OperationAdd ());		
      System.out.println ( "10 + 5 =" + context.executeStrategy (10, 5));

      context = new Context (new OperationSubstract ());		
      System.out.println ( "10 - 5 =" + context.executeStrategy (10, 5));

      context = new Context (new OperationMultiply ());		
      System.out.println ( "10 * 5 =" + context.executeStrategy (10, 5));
   }
}

Step 5

Verify output.

10 + 5 = 15
10--5 = 5
10 * 5 = 50