欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

设计模式-策略模式

程序员文章站 2022-06-26 08:39:50
参考:《大话设计模式》 策略模式(Strategy): 它定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户环境角色(Context):持有一个Strategy对象的引用抽象策略角色(Strategy):一般为抽象类和接口,给出所有的具体策略具体策略角 ......

概念:https://www.runoob.com/design-pattern/strategy-pattern.html

需求场景:查询出来很多员工信息,

  1.   过滤工资大于5000的……
  2.  过滤年龄大于50的……

常规思路1:

        List<Employee> employees = Arrays.asList(new Employee("张三", 18, new BigDecimal("9999")),
                new Employee("李四", 20, new BigDecimal("8888")),
                new Employee("翠花", 50, new BigDecimal("7777"))
        );

        //工资大于5000的
        List<Employee> salarys = Lists.newArrayList();
        for (Employee item : employees) {
            if (item.getSalary().compareTo(new BigDecimal("5000")) <=0) {
                continue;
            }
            salarys.add(item);
        }

        //年龄大于50岁
        List<Employee> ages = Lists.newArrayList();
        for (Employee age : employees) {
            if (age.getAge().compareTo(50) <= 0) {
                continue;
            }
            ages.add(age);
        }

常规思路2:

        List<Employee> employees = Arrays.asList(new Employee("张三", 18, new BigDecimal("9999")),
                new Employee("李四", 20, new BigDecimal("8888")),
                new Employee("翠花", 50, new BigDecimal("7777"))
        );

        //java8 流的应用
        //工资大于5000的
        List<Employee> collect = employees.stream().filter(e -> e.getSalary().compareTo(new BigDecimal("5000")) > 0)
                .collect(Collectors.toList());
        System.out.println(collect);

        //年龄大于50岁
        List<Employee> collect1 = employees.stream().filter(e -> e.getAge() > 50)
                .collect(Collectors.toList());
        System.out.println(collect1);

设计模式改造:其实这种场景比较简单,过滤的算法不够复杂,设计模式的优势体现的不是很明显,但这种意识和高度必须得时刻提醒着自己

//策略类
public interface MyPredicate<T> {

    public boolean test(T t);
}

//年龄策略的实现类
public class FilterEmployeeByAge implements  MyPredicate<Employee>{
    @Override
    public boolean test(Employee employee) {
        return employee.getAge() > 50;
    }
}

//工资策略的实现类
public class FilterEmployeeBySalary implements MyPredicate<Employee> {
    @Override
    public boolean test(Employee o) {
        return o.getSalary().compareTo(new BigDecimal("5000")) > 0;
    }
}




//测试调用的过程
public class Test {

    private MyPredicate myPredicate;

    public Test() {
    }

    public void setMyPredicate(MyPredicate myPredicate) {
        this.myPredicate = myPredicate;
    }
    public static void main(String[] args) {

        List<Employee> employees = Arrays.asList(

                new Employee("张三", 18, new BigDecimal("9999")),
                new Employee("李四", 20, new BigDecimal("8888")),
                new Employee("翠花", 50, new BigDecimal("7777"))

        );

        Test test = new Test();
        test.setMyPredicate(new FilterEmployeeBySalary());

        test.filterEmployees(employees);
        System.out.println(">>>>>>>>薪资" + test.filterEmployees(employees));

        test.setMyPredicate(new FilterEmployeeByAge());
        test.filterEmployees(employees);
        System.out.println(">>>>>>>>年龄" + test.filterEmployees(employees));
    }



    private List<Employee> filterEmployees(List<Employee> employees){

        List<Employee> employs = Lists.newArrayList();
        for (Employee item : employees) {
            if (myPredicate.test(item)){
                employs.add(item);
            }
        }
        return employs;
    }
}

 

本文地址:https://blog.csdn.net/cxdn_czj/article/details/112009358

相关标签: JAVA 设计模式