Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Strategy Pattern

Strategy Pattern

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

The Strategy Pattern defines a series of algorithms or strategies and encapsulates each algorithm in a separate class so that they can be interchanged. By using the Strategy Pattern, different algorithms can be selected at runtime as needed without modifying the client code.

In the Strategy Pattern, we create objects representing various strategies and a context object whose behavior changes as the strategy object changes. The strategy object changes the execution algorithm of the context object.

Introduction

Intent

Encapsulate each algorithm so that they can be used interchangeably.

Main problem solved

Use Cases

Implementation

Key code

Application Example

  1. Brilliant strategies: Each bag represents a strategy containing different tactics.
  2. Travel mode selection: Riding a bicycle, taking a car, etc., each mode is a replaceable strategy.
  3. Java AWT's LayoutManager: Different layout managers implement the same interface but provide different layout algorithms.

Advantages

  1. Free algorithm switching: Algorithms can be switched at runtime as needed.
  2. Avoid multiple conditional judgments: Eliminates complex conditional statements.
  3. Good extensibility: Adding a new algorithm only requires adding a new strategy class, without modifying existing code.

Disadvantages

  1. Increased number of strategy classes: Each new algorithm requires an additional strategy class.
  2. All strategy classes need to be exposed: Strategy classes need to be exposed so that they can be selected and used.

Usage recommendation

Notes

structures

The Strategy Pattern contains the following core roles:

The Strategy Pattern provides a way to dynamically select different algorithms by decoupling algorithms from the code that uses them. Client code does not need to know the specific algorithm details, but uses the selected strategy by calling the context class.

Create a class that implements

We will create concrete strategy classes of the interface that defines an Strategy interface as the bridge implementation and concrete classes that implement the Strategy activity.Context is a class that uses a certain strategy.

StrategyPatternDemo, and our demo class uses the Context and strategy objects to demonstrate the behavior changes of Context when the strategy it is configured with or uses changes.

Step 1

Create an interface.

Strategy.java

public

interface

Strategy

{

public

int

doOperation

(

int

num1

,

int

num2

)

;

}

Step 2

Create concrete classes that implement the interface.

OperationAdd.java

public

class

OperationAdd

implements

Strategy

{

@

Override

public

int

doOperation

(

int

num1

,

int

num2

)

{

return

num1

num2

;

}

}

OperationSubtract.java

public

class

OperationSubtract

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

creation Context class.

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

Use Context to see when it changes strategy Strategy the behavior changes.

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

OperationSubtract

(

)

)

;

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

Run the program, output result:



10 + 5 = 15

10 - 5 = 5

10 * 5 = 50