The original article was first published on wechat public account: Practice.

Strategy pattern corresponding to the algorithm to solve the problem of a collection, allows users to choose the appropriate algorithm from the collection of these algorithms, can according to the specific needs of the business, without changing the original code to add new algorithm on the basis of strategy, not only has realized the algorithm or business independently, and can switch, call.

Relevant concepts

  • Abstract policy roles: Generally use interfaces to define policy methods uniformly;
  • Specific policy role: encapsulates a specific policy algorithm.
  • Environment role: Completes the unified external method call of the policy algorithm to the client.

Usage scenarios

  • Suitable for the same type of business but different specific performance of the business, such as purchase price according to the purchase quantity difference to provide different purchase price;
  • It is easy to switch different algorithms and avoid changing the original code. For example, the switch of multiple picture frames in Android development can be easily changed by using the strategic design mode to encapsulate the picture loading frame and avoid modifying the original code again.

implementation

The following takes the travel mode of tourists as an example to realize the strategy design mode, and creates the abstract strategy role as follows:

/** * Abstract policy role *@author jzman
 */
public interface IStratey {
	/ / travel
	void trip(a);
}
Copy the code

Then, create specific policy roles as follows:

  • By air:
/** * Specific policy role *@author jzman
 */
public class AirStrategy implements IStratey{
	@Override
	public void trip(a) {
		System.out.println("By plane!); }}Copy the code
  • Walk:
/** * Specific policy role *@author jzman
 */
public class PersonStrategy implements IStratey{

	@Override
	public void trip(a) {
		System.out.println("By train!); }}Copy the code
  • By train:
/** * Specific policy role *@author jzman
 */
public class TrainStrategy implements IStratey{

	@Override
	public void trip(a) {
		System.out.println("On foot!); }}Copy the code

Then create the environment role as follows:

/** * Environment role * is mainly used to interact with the specific policy, separating the algorithm and the client call, making the algorithm independent of the client, convenient algorithm policy switch *@author jzman
 */
public class Context {
	private IStratey iStratey;

	public Context(IStratey iStratey) {
		super(a);this.iStratey = iStratey;
	}

	public void setiStratey(IStratey iStratey) {
		this.iStratey = iStratey;
	}
	
	// Specific business logic
	public void tripType(a) { iStratey.trip(); }}Copy the code

Finally, the client calls as follows:

/** * the client calls *@author jzman
 */
public class StrategyClient {
	public static void main(String[] args) {
		// Create a specific policy role
		IStratey stratey = new AirStrategy();
		// Create an environment role and switch the desired policy
		Context con = new Context(stratey);
		// Call a specific algorithmcon.tripType(); }}Copy the code

The output of the above code is as follows:

By plane!Copy the code

This is a relatively simple example, may not have practical significance, today summarizes the use of strategic design design pattern, on the one hand is the review of strategic design pattern, on the other hand is planning to use strategic design pattern encapsulation Android development image loading framework. You can choose to pay attention to personal wechat public number: practice to get the latest updates, exchange and study together!