banner
jzman

jzman

Coding、思考、自觉。
github

Strategy Design Pattern

The Strategy pattern corresponds to a collection of algorithms for solving a specific 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 travel methods for travelers:

/**
 * Abstract Strategy Role
 */
public interface IStrategy {
	// Travel
	void trip();
}

Then, create concrete strategy roles:

  • By Air:
/**
 * Concrete Strategy Role
 */
public class AirStrategy implements IStrategy {
	@Override
	public void trip() {
		System.out.println("Travel by airplane!");
	}
}
  • On Foot:
/**
 * Concrete Strategy Role
 */
public class PersonStrategy implements IStrategy {
	@Override
	public void trip() {
		System.out.println("Travel on foot!");
	}
}
  • 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
 * Used for interaction with specific strategies, separating algorithms from client calls, making algorithms independent of clients, and facilitating strategy switching.
 */
public class Context {
	private IStrategy strategy;

	public Context(IStrategy strategy) {
		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
 */
public class StrategyClient {
	public static void main(String[] args) {
		// Create a concrete strategy role
		IStrategy strategy = new AirStrategy();
		// Create the context role and switch to the desired strategy
		Context context = new Context(strategy);
		// Call the specific algorithm
		context.tripType();
	}
}

The output of the above code will be:

Travel by airplane!

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

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