6.25. Strategy mode

发布时间 :2025-10-25 12:24:47 UTC      

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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.