6.25. Strategy mode

发布时间 : 2025-10-25 13:34:18 UTC      

Page Views: 9 views

In Strategy Pattern, the behavior of a class or its algorithm can be changed at run time. This type of design pattern belongs to behavioral pattern.

In policy mode, we create objects that represent various policies and a context object whose behavior changes as the policy object changes. The policy object changes the execution algorithm of the context object.

6.25.1. Introduction

意图: Define a series of algorithms, encapsulate them one by one, and make them interchangeable.

主要解决: When many algorithms are similar, the complexity and difficulty of using if…else is difficult to maintain.

何时使用: There are many classes in a system, and what distinguishes them is their direct behavior.

如何解决: Encapsulate these algorithms into a class and replace them arbitrarily.

关键代码: Implement the same interface.

应用实例: 1. Zhuge Liang’s ingenious plan, each bag is a strategy. 2, the way to travel, choose to ride a bike, take a car, each way of travel is a strategy. 3. LayoutManager in JAVA AWT.

优点: 1. The algorithm can be switched freely. 2. Avoid using multiple conditional judgments. 3. Good expansibility.

缺点: 1. The number of policy classes will increase. 2. All policy classes need to be exposed.

使用场景: 1. If there are many classes in a system, and the only difference between them is their behavior, then using the policy pattern can dynamically make an object choose one behavior among many behaviors. 2. A system needs to choose one of several algorithms dynamically. 3. If an object has a lot of behavior, if the appropriate pattern is not used, these behaviors have to be realized by using multiple conditional selection statements.

注意事项: If a system has more than four policies, you need to consider using a mixed mode to solve the problem of inflated policy classes.

6.25.2. Realize

We will create a that defines the activity Strategy Interface and implementation of the Strategy The entity policy class of the interface. Context Is a class that uses some kind of strategy.

StrategyPatternDemo Our demo class uses the Context And policy objects to demonstrate how Context behaves when the policy it configures or uses changes.

策略模式的 UML 图

6.25.3. Step 1

Create an interface.

Strategy.java

publicinterfaceStrategy{publicintdoOperation(intnum1,intnum2);} 

6.25.4. Step 2

Create an entity class that implements the interface.

OperationAdd.java

publicclassOperationAddimplementsStrategy{@OverridepublicintdoOperation(intnum1,intnum2){returnnum1+num2;}} 

OperationSubtract.java

publicclassOperationSubtractimplementsStrategy{@OverridepublicintdoOperation(intnum1,intnum2){returnnum1-num2;}} 

OperationMultiply.java

publicclassOperationMultiplyimplementsStrategy{@OverridepublicintdoOperation(intnum1,intnum2){returnnum1\*num2;}} 

6.25.5. Step 3

Create Context Class.

Context.java

publicclassContext{privateStrategystrategy;publicContext(Strategystrategy){this.strategy=strategy;}publicintexecuteStrategy(intnum1,intnum2){returnstrategy.doOperation(num1,num2);}} 

6.25.6. Step 4

Use Context To see when it changes strategy Strategy A change in behavior at the time.

StrategyPatternDemo.java

publicclassStrategyPatternDemo{publicstaticvoidmain(String[]args){Contextcontext=newContext(newOperationAdd());System.out.println("10 + 5 ="+context.executeStrategy(10,5));context=newContext(newOperationSubtract());System.out.println("10 - 5 ="+context.executeStrategy(10,5));context=newContext(newOperationMultiply());System.out.println("10 \* 5 ="+context.executeStrategy(10,5));}} 

6.25.7. Step 5

Execute the program and output the result:

10 + 5 = 15 10 - 5 = 5 10 * 5 = 50 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。