A, definitions,
The design pattern we are talking about today is “strategic pattern”. Strategic pattern is easier to understand. First let’s take a look at its definition:
A policy pattern is a way of defining a set of algorithms, all of which conceptually do the same job, but with different implementations, which can call all of them in the same way, reducing the coupling “DEP” between various algorithm classes and using algorithm classes.
From the above definition, we can see that the policy pattern has three elements:
- Policy template: The algorithm of policy pattern is to accomplish the same work, but the trigger conditions may be different, so we can abstract out a policy template, similar to the “product template in factory pattern”;
- Policies: Implement different policies according to the specific scenario to get specific results according to the actual scenario;
- Context: The context determines which policy to use depending on the scenario.
2. Give examples
The strategy mode is also common in daily life, as illustrated below:
When we take the bus and subway in life, the ticket price in Hefei is 2 yuan, half price for students and free ticket for the elderly, which is a common design pattern, we can see:
- All aimed at solving the problem of charging people for rides
- Different age groups calculate prices in different ways
Then we have the implementation scheme:
// Policy template
class StrategyTemplate {
constructor(){}}// Free admission for senior citizens
class OldPersonStrategy extends StrategyTemplate {
constructor() {}
calculatePrice() {
return 0; }}// Half price for students
class StudentStrategy extends StrategyTemplate {
constructor() {}
calculatePrice() {
return 1; }}// General ticket 2 yuan
class NormalStrategy extends StrategyTemplate {
constructor() {}
calculatePrice() {
return 2; }}// The context passes in the policy and uses it
class BusContext {
constructor(strategy) {
this.strategy = strategy;
}
pay() {
return this.strategy.calculatePrice(); }}/ / use
const oldPerson = new BusContext(new OldPersonStrategy());
oldPerson.pay();
const student = new BusContext(new StudentStrategy());
student.pay();
const person = new BusContext(new NormalStrategy());
person.pay();
Copy the code
In our daily life, we can see the use of many strategic modes everywhere, such as washing machine (quick washing, rinsing, large items, etc.), rice cooker (cooking, porridge, etc.), electric toothbrush (high frequency, medium frequency, low frequency), shopping (full reduction, discount, rebate, etc.).
conclusion
From the above examples, we can conclude that the usage scenarios of the strategy pattern have several characteristics:
- There are multiple solutions to a problem/requirement, and different scenarios need to be judged
- The number of scenarios is small
The advantages are:
- Policies are independent from each other and have good scalability and maintainability
- Unit tests are easy to do
- To reduce the
switch/if else
judge
The disadvantages are:
- It is not suitable for solving too many scenarios
reference
-
[Tai Hua Design Mode – Cheng Jie]:
-
Design patterns in 5 minutes
Personal blog
Fomalhaut blog