Definition of Strategy Pattern
The strategy mode is relatively simple, it refers to the definition of a series of algorithms, and encapsulates each algorithm, and makes them interchangeable. The strategy pattern lets the algorithm change independently of the clients using it.
Model:
-
Context: context role, which acts as an encapsulation and holds a reference to a Strategy object
-
Strategy: Strategy role (abstract) usually interface
-
ConcreteStrategy: Concrete Strategy Roles
Example description
The strategy mode is easy to understand, and here we take Baidu network disk as an example, everyone should understand it better. For example, ordinary users of Baidu's network disk now have no extra space, members will get an additional 2T space, and super members will get an additional 5T space + various benefits. This is also a strategy. Let's demonstrate it with code:
1. Strategy, an abstract strategy, that is, the extra space acquired
public interface AbstractStrategy {
//Method to get extra space
public void getExSpace () ;
}
2. ConcreteStrategy, that is, ordinary users, members, super members
public class OrdinaryUser implements AbstractStrategy {
@Override
public void getExSpace () {
Log.d( "qzs" , "Ordinary users have no extra space to get" );
}
}
public class Vip implements AbstractStrategy {
@Override
public void getExSpace () {
Log.d( "qzs" , "Member users have 2T extra space to get" );
}
}
public class SuperVip implements AbstractStrategy {
@Override
public void getExSpace () {
Log.d( "qzs" , "Super member users have 5T extra space to get" );
}
}
3. Context, context, in order to encapsulate
public class SpaceContext {
private AbstractStrategy abstractStrategy;
public SpaceContext (AbstractStrategy abstractStrategy) {
this .abstractStrategy=abstractStrategy;
}
//Call the method in the abstract strategy role
public void getExSpace () {
this .abstractStrategy.getExSpace();
}
}
4. Call:
SpaceContext spaceContext;
//If it is an ordinary user
spaceContext= new SpaceContext( new OrdinaryUser());
spaceContext.getExSpace();
//If it is a member
spaceContext= new SpaceContext( new Vip());
spaceContext.getExSpace();
// If it is a super member
spaceContext= new SpaceContext( new SuperVip());
spaceContext.getExSpace();
operation result:
Advantages and disadvantages of the strategy pattern and more
1. Advantages
-
The strategy mode provides a way to manage related algorithm families, and the algorithms can be switched
-
Avoid using multiple conditional branch statements
2. Disadvantages
-
The client knows all policy classes and decides which one to use. The strategy class is exposed
-
There are sometimes too many policy classes
3. Applicable scenarios
-
Multiple classes only differ in their performance behaviors. You can use the Strategy mode to dynamically select the specific behavior to be executed at runtime.
-
Different strategies (algorithms) need to be used in different situations, or strategies may also be implemented in other ways in the future.
-
The implementation details of specific strategies (algorithms) are hidden from clients and are completely independent of each other.
Emphasis: The typical application of strategy mode in Android is Adapter; in addition, there are applications of strategy enumeration. If you are interested, you can take a look at it yourself; I will write another article about the specific application.
Definitions and advantages and disadvantages refer to online information