banner
jzman

jzman

Coding、思考、自觉。
github

Strategy Design Pattern

The Strategy pattern corresponds to a collection of algorithms for solving a particular problem. It allows users to choose the appropriate algorithm from this collection. Based on specific business requirements, new algorithm strategies can be added without modifying the existing code. This not only achieves algorithm or business independence but also enables unified switching and calling.

  1. Related Concepts
  2. Use Cases
  3. Implementation
  • Abstract Strategy Role: Generally defined using an interface to unify the definition of strategy methods.
  • Concrete Strategy Role: Encapsulates specific strategy algorithms.
  • Context Role: Provides a unified external method for strategy algorithms to be called by clients.

Use Cases#

  • Suitable for businesses of the same type but with different behavioral expressions, such as providing different purchase prices based on differences in purchase quantity.
  • Facilitates switching between different algorithms, avoiding modification of existing business code. For example, in Android development, switching between multiple image frameworks can be easily achieved by encapsulating them using the strategy design pattern, avoiding the need to modify the existing code.

Implementation#

Below is an example of implementing the strategy design pattern using different modes of travel for travelers:

Create the abstract strategy role as follows:

/**
 * Abstract strategy role
 */
public interface IStrategy {
	// Travel
	void trip();
}

Then, create concrete strategy roles:

  • By airplane:
/**
 * Concrete strategy role
 */
public class AirStrategy implements IStrategy{
	@Override
	public void trip() {
		System.out.println("Travel by airplane!");
	}
}
  • By walking:
/**
 * Concrete strategy role
 */
public class PersonStrategy implements IStrategy{

	@Override
	public void trip() {
		System.out.println("Travel by walking!");
	}
}
  • By train:
/**
 * Concrete strategy role
 */
public class TrainStrategy implements IStrategy{

	@Override
	public void trip() {
		System.out.println("Travel by train!");
	}
}

Next, create the context role:

/**
 * Context role
 * Mainly used to interact with specific strategies, separating algorithms from client calls, making algorithms independent of clients, and facilitating the switching of algorithm strategies.
 */
public class Context {
	private IStrategy strategy;

	public Context(IStrategy strategy) {
		super();
		this.strategy = strategy;
	}

	public void setStrategy(IStrategy strategy) {
		this.strategy = strategy;
	}
	
	// Specific business logic
	public void tripType() {
		strategy.trip();
	}
}

Finally, the client can call the context as follows:

/**
 * Client call
 */
public class StrategyClient {
	public static void main(String[] args) {
		// Create concrete strategy role
		IStrategy strategy = new AirStrategy();
		// Create context role, can switch to the required strategy freely
		Context context = new Context(strategy);
		// Call the specific algorithm
		context.tripType();
	}
}

The output of the above code is:

Travel by airplane!

This is a relatively simple example that may not have practical significance. Today, I summarized the use of the strategy design pattern, which is a review of the strategy design pattern and a plan to encapsulate the image loading framework in Android development using the strategy design pattern. That's all for today's article.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.