Small talk on design patterns (3)—Strategy pattern
- Column introduction
- strategy pattern
-
- main character
- main idea
- Java code implementation - an example of a game character's attack method
-
- First, we define an abstract strategy class AttackStrategy, which declares an attack() method:
- Then, we define two specific attack strategy classes: MeleeAttackStrategy and RangedAttackStrategy, which implement the AttackStrategy interface respectively:
- Next, we define an environment class Character, which holds an AttackStrategy object and provides a setAttackStrategy() method for dynamically setting the attack strategy:
- Finally, we can use strategy pattern in the client:
- code analysis
- Advantages and Disadvantages Analysis
Column introduction
Column address
Column introduction
It mainly analyzes and summarizes the 23 common design patterns currently on the market one by one. I hope that interested friends can take a look and it will be continuously updated. I hope you can supervise me and we can learn and make progress together. Come on, everyone.
strategy pattern
Strategy Pattern is a behavioral design pattern that defines a series of algorithms and encapsulates each algorithm so that they can be replaced with each other. The Strategy pattern allows the algorithm to change independently of the client using the algorithm.
main character
When we use the Strategy Pattern, there are usually three main roles involved: Context, Abstract Strategy, and Concrete Strategy.
Context
The environment class is the core of the strategy pattern. It holds a reference to a strategy object and calls the algorithm of the strategy object according to specific needs at runtime. The environment class provides an interface or method for setting and getting policy objects. When using the Strategy pattern on the client side, you often need to interact with environment classes.
Abstract Strategy
The abstract strategy class is the interface or abstract class of the strategy pattern, which defines the algorithm that the specific strategy class must implement. An abstract strategy class usually contains one or more abstract methods that describe the behavior of the strategy. Clients use concrete strategies by calling methods in abstract strategy classes.
Concrete Strategy
The concrete strategy class is the implementation class of the strategy pattern and implements the algorithm defined in the abstract strategy class. Specific strategy classes implement different algorithm logic based on specific business needs. When using policy mode on the client, you can select the appropriate specific policy class according to your needs.
Role summary
The environment class holds a reference to a strategy object and calls the algorithm of the strategy object according to specific needs at runtime. The abstract strategy class defines the algorithm that the specific strategy class must implement, and the specific strategy class implements the specific algorithm logic. By using the strategy pattern, the definition and use of the algorithm can be separated, improving the flexibility, maintainability and scalability of the code.
main idea
The core idea of the Strategy Pattern is to separate the definition and use of algorithms. In the strategy pattern, we encapsulate different algorithms into different strategy classes and hold a reference to a strategy object through the environment class. At runtime, the appropriate strategy object is selected according to specific needs and its algorithm is called.
Encapsulation algorithm
The strategy pattern encapsulates different algorithms into different strategy classes. Each strategy class implements a specific algorithmic logic. By encapsulating the algorithm, we can separate it from other code, making the definition of the algorithm clearer, readable, and maintainable.
Define abstract strategies
The strategy pattern defines an abstract strategy class, which contains the methods that the strategy class must implement. An abstract strategy class can be an interface or an abstract class. By defining abstract strategies, we can unify the interfaces of different strategy classes so that clients can use different strategies in a unified way.
Use environment class
The strategy pattern holds a reference to a strategy object through the environment class. At runtime, the client can select the appropriate policy object based on specific needs and set it to the environment class. The environment class calls the algorithm of the policy object according to specific needs during runtime, realizing dynamic switching of the algorithm.
Thought summary
By separating the definition and use of algorithms, the Strategy pattern improves code flexibility, maintainability, and scalability. It makes it easier to modify and add algorithms without modifying the original code. At the same time, the strategy mode also conforms to the opening and closing principle, and new strategy classes can be easily added.
Java code implementation - an example of a game character's attack method
First, we define an abstract strategy class AttackStrategy, which declares an attack() method:
public interface AttackStrategy {
void attack();
}
Then, we define two specific attack strategy classes: MeleeAttackStrategy and RangedAttackStrategy, which implement the AttackStrategy interface respectively:
public class MeleeAttackStrategy implements AttackStrategy {
@Override
public void attack() {
System.out.println("近战攻击");
}
}
public class RangedAttackStrategy implements AttackStrategy {
@Override
public void attack() {
System.out.println("远程攻击");
}
}
Next, we define an environment class Character, which holds an AttackStrategy object and provides a setAttackStrategy() method for dynamically setting the attack strategy:
public class Character {
private AttackStrategy attackStrategy;
public void setAttackStrategy(AttackStrategy attackStrategy) {
this.attackStrategy = attackStrategy;
}
public void attack() {
attackStrategy.attack();
}
}
Finally, we can use strategy pattern in the client:
public class Client {
public static void main(String[] args) {
Character character = new Character();
character.setAttackStrategy(new MeleeAttackStrategy());
character.attack(); // 输出:近战攻击
character.setAttackStrategy(new RangedAttackStrategy());
character.attack(); // 输出:远程攻击
}
}
code analysis
In the above code, we set different attack strategies so that the character can use different attack methods. In this way, when new attack methods need to be added, you only need to implement a new specific strategy class and set a new attack strategy in the client, without modifying the original code.
Advantages and Disadvantages Analysis
advantage
Algorithm encapsulation
The strategy pattern encapsulates different algorithms into different strategy classes, so that each strategy class only focuses on its own algorithm logic, improving the readability and maintainability of the code.
Substitutability of strategies
Since the strategy mode separates the definition and use of the algorithm, different strategy objects can be selected according to specific needs during runtime to achieve dynamic switching of the algorithm. This allows the algorithm to be easily replaced and extended without modifying the original code.
Algorithm scalability
The strategy mode conforms to the opening and closing principle and can easily add new strategy classes. When you need to add a new algorithm, you only need to add a new strategy class and set the strategy object in the environment class. There is no need to modify the original code.
Algorithm reusability
The strategy pattern encapsulates algorithms into independent strategy classes and can reuse the same algorithm in different scenarios. This avoids duplication of code and improves code reusability.
shortcoming
Increased the number of classes
Using the strategy pattern will increase the number of classes, and each specific strategy class needs to be defined separately. If there are too many strategies, it may result in too many classes and increase the complexity of the code.
The client needs to know about the different policy classes
When the client uses the policy mode, it needs to understand different policy classes and select the appropriate policy object. If there are more policies, it may increase the complexity of the client.
Strategy selection logic
When using the strategy pattern, you need to select the appropriate strategy object based on specific needs. This selection logic may be complex and requires multiple factors to be considered, increasing the complexity of the code.
Summary of advantages and disadvantages
Overall, the strategy pattern improves code flexibility, maintainability, and scalability by separating the definition and use of algorithms. It encapsulates different algorithms into different strategy classes and realizes dynamic switching and reuse of algorithms. However, the strategy pattern also increases the number of classes, increases the complexity of the client, and requires consideration of the strategy selection logic. When using the strategy pattern, you need to weigh its advantages and disadvantages and choose the appropriate usage method.