Design pattern (strategy pattern)

Strategy mode

In Strategy Pattern, the behavior of a class or its algorithm can be changed at runtime. This type of design pattern is a behavioral pattern.
In the strategy mode, we create objects representing various strategies and a context object whose behavior changes as the strategy object changes. The strategy object changes the execution algorithm of the context object.

Introduction

Intent: Define a series of algorithms, encapsulate them one by one, and make them interchangeable.
The main solution: in the case of multiple similar algorithms, the use of if...else is complicated and difficult to maintain.
When to use: A system has many, many types, and what distinguishes them is their direct behavior.
How to solve: Encapsulate these algorithms into classes one by one and replace them arbitrarily.
Key code: implement the same interface.
Advantages: 1. The algorithm can be switched freely. 2. Avoid using multiple conditional judgments. 3. Good scalability.
Disadvantages: 1. Strategy categories will increase. 2. All strategy categories need to be exposed to the outside world.
Usage scenarios: 1. If there are many classes in a system, and the difference between them is only in their behavior, then using the strategy mode can dynamically make an object choose one behavior among many behaviors. 2. A system needs to dynamically select one of several algorithms. 3. If an object has many behaviors, these behaviors can only be achieved by using multiple conditional selection statements if they do not use appropriate patterns.
Note: If there are more than four strategies in a system, you need to consider using a mixed mode to solve the problem of strategy expansion.

Code

//员工实体类
@Data
@AllArgsConstructor
public class Emp {
    
    

    private String name;
    private int age;
    private double salary;

}
//策略接口
public interface MyPredicate<T> {
    
    
    public boolean test(T t);
}
/**
* @description: 策略实现类,根据年龄过滤
* @author TAO
* @date 2021/3/27 20:29
*/
public class FilterEmpByAge implements MyPredicate<Emp>{
    
    
    @Override
    public boolean test(Emp emp) {
    
    
        return emp.getAge()>=35;
    }
}
/**
* @description: 策略实现类,根据工资过滤
* @author TAO
* @date 2021/3/28 14:12
*/
public class FilterEmpBySalary  implements MyPredicate<Emp> {
    
    
    @Override
    public boolean test(Emp emp) {
    
    
        return emp.getSalary() >= 5000;
    }

}

	//测试代码
	@Test
    public void test(){
    
    
        List<Emp> list=filterEmp(empList, new FilterEmpByAge());
        for (Emp emp:list){
    
    
            System.out.println(emp);
        }
        System.out.println("-----------------------------");

        List<Emp> list2=filterEmp(empList, new FilterEmpBySalary());
        for (Emp emp:list2){
    
    
            System.out.println(emp);
        }
    }

    public List<Emp> filterEmp(List<Emp> list,MyPredicate<Emp> mp){
    
    
        List<Emp> emps = new ArrayList<>();
        for (Emp emp:list){
    
    
            if (mp.test((emp))){
    
    
                emps.add(emp);
            }
        }
        return emps;
    }

Guess you like

Origin blog.csdn.net/CSDN877425287/article/details/115280858