案例一:
其中,Employee类就是与上一篇的相同,这里不重复贴了。
还用到了一个Goods类:
1 package com.bjsxt.others.commons; 2 3 public class Goods { 4 private String name; 5 private double price; 6 //折扣 7 private boolean discount; 8 //无参构造器 9 public Goods() { 10 super(); 11 } 12 //有参构造器 13 public Goods(String name, double price, boolean discount) { 14 super(); 15 this.name = name; 16 this.price = price; 17 this.discount = discount; 18 } 19 //setter和getter方法 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public double getPrice() { 27 return price; 28 } 29 public void setPrice(double price) { 30 this.price = price; 31 } 32 public boolean isDiscount() { 33 return discount; 34 } 35 public void setDiscount(boolean discount) { 36 this.discount = discount; 37 } 38 @Override 39 public String toString() { 40 return "(商品:"+this.name+",价格:"+this.price+",是否打折:"+(discount?"是":"否")+")"; 41 } 42 43 }
最后是主函数:
1 package com.bjsxt.others.commons; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import org.apache.commons.collections4.Closure; 8 import org.apache.commons.collections4.CollectionUtils; 9 import org.apache.commons.collections4.Predicate; 10 import org.apache.commons.collections4.functors.ChainedClosure; 11 import org.apache.commons.collections4.functors.IfClosure; 12 import org.apache.commons.collections4.functors.WhileClosure; 13 14 /** 15 * 函数式编程Closure 闭包 封装特定的业务功能 16 * 1、Closure 17 * 2、ifClosure IfClosure.ifClosure(断言,功能1,功能2) 18 * 3、whileClosure WhileClosure.whileClosure(断言,功能,标识) 19 * 4、ChainedClosure.chainedClosure(功能列表) 20 * 21 * CollectionUtils.forAllDo(容器,功能类对象); 22 * 23 */ 24 public class Demo03 { 25 public static void main(String[] args) { 26 basic(); 27 System.out.println("============="); 28 ifClosure(); 29 System.out.println("============="); 30 whileClosure(); 31 System.out.println("============="); 32 chainClosure(); 33 } 34 35 /** 36 * 折上折 先打折商品,进行9折,满百再减20 37 */ 38 public static void chainClosure() { 39 List<Goods> goodList = new ArrayList<Goods>(); 40 goodList.add(new Goods("javase视频",120,true)); 41 goodList.add(new Goods("javaee视频",100,false)); 42 goodList.add(new Goods("高新技术视频",80,false)); 43 44 //满百减20 45 Closure<Goods> substract = new Closure<Goods>() { 46 @Override 47 public void execute(Goods goods) { 48 if(goods.getPrice() >= 100) { 49 goods.setPrice(goods.getPrice()-20); 50 } 51 } 52 }; 53 //打折 54 Closure<Goods> discount = new Closure<Goods>() { 55 @Override 56 public void execute(Goods goods) { 57 if(goods.isDiscount()) { 58 goods.setPrice(goods.getPrice()*0.9); 59 } 60 } 61 }; 62 63 64 //链式操作 65 Closure<Goods> chainClo = ChainedClosure.chainedClosure(discount,substract); 66 67 //关联 68 CollectionUtils.forAllDo(goodList, chainClo); 69 70 //查看操作后的数据 71 for(Goods temp:goodList) { 72 System.out.println(temp); 73 } 74 75 76 } 77 78 /** 79 * 确保所有的员工工资都大于10000,如果已经超过的不再上涨 80 */ 81 public static void whileClosure() { 82 //数据 83 List<Employee> empList = new ArrayList<Employee>(); 84 empList.add(new Employee("bjsxt",20000)); 85 empList.add(new Employee("is",10000)); 86 empList.add(new Employee("good",5000)); 87 88 //业务功能 每次上涨0.2 89 Closure<Employee> cols = new Closure<Employee>() { 90 91 @Override 92 public void execute(Employee emp) { 93 emp.setSalary(emp.getSalary()*1.2); 94 95 } 96 97 }; 98 99 //判断 100 Predicate<Employee> empPre = new Predicate<Employee>() { 101 @Override 102 public boolean evaluate(Employee emp) { 103 return emp.getSalary()<10000; 104 } 105 }; 106 //第三个参数 false 表示while结构 先判断后执行 true do..while 先执行后判断 107 Closure<Employee> whileCols = WhileClosure.whileClosure(empPre, cols, false); 108 109 //工具类 110 CollectionUtils.forAllDo(empList, whileCols); 111 112 //操作后的数据 113 Iterator<Employee> empIt = empList.iterator(); 114 while(empIt.hasNext()) { 115 System.out.println(empIt.next()); 116 } 117 } 118 119 120 /** 121 * 二选一 如果是打折商品,进行9折,否则满百减20 122 */ 123 public static void ifClosure() { 124 List<Goods> goodList = new ArrayList<Goods>(); 125 goodList.add(new Goods("javase视频",120,true)); 126 goodList.add(new Goods("javaee视频",100,false)); 127 goodList.add(new Goods("高新技术视频",80,false)); 128 129 //满百减20 130 Closure<Goods> substract = new Closure<Goods>() { 131 @Override 132 public void execute(Goods goods) { 133 if(goods.getPrice() >= 100) { 134 goods.setPrice(goods.getPrice()-20); 135 } 136 } 137 }; 138 //打折 139 Closure<Goods> discount = new Closure<Goods>() { 140 @Override 141 public void execute(Goods goods) { 142 if(goods.isDiscount()) { 143 goods.setPrice(goods.getPrice()*0.9); 144 } 145 } 146 }; 147 148 //判断 149 Predicate<Goods> pre = new Predicate<Goods>() { 150 @Override 151 public boolean evaluate(Goods goods) { 152 return goods.isDiscount(); 153 } 154 }; 155 156 //二选一 157 Closure<Goods> ifClo = IfClosure.ifClosure(pre,discount,substract); 158 159 //关联 160 CollectionUtils.forAllDo(goodList, ifClo); 161 162 //查看操作后的数据 163 for(Goods temp:goodList) { 164 System.out.println(temp); 165 } 166 167 168 } 169 170 171 /** 172 * 基本操作 173 */ 174 public static void basic() { 175 //数据 176 List<Employee> empList = new ArrayList<Employee>(); 177 empList.add(new Employee("bjsxt",20000)); 178 empList.add(new Employee("is",10000)); 179 empList.add(new Employee("good",5000)); 180 181 //业务功能 182 Closure<Employee> cols = new Closure<Employee>() { 183 184 @Override 185 public void execute(Employee emp) { 186 emp.setSalary(emp.getSalary()*1.2); 187 188 } 189 190 }; 191 192 //工具类 193 CollectionUtils.forAllDo(empList, cols); 194 195 //操作后的数据 196 Iterator<Employee> empIt = empList.iterator(); 197 while(empIt.hasNext()) { 198 System.out.println(empIt.next()); 199 } 200 201 } 202 203 }
运行结果:
(码农:bjsxt,敲砖钱:24000.0) (码农:is,敲砖钱:12000.0) (码农:good,敲砖钱:6000.0) ============= (商品:javase视频,价格:108.0,是否打折:是) (商品:javaee视频,价格:80.0,是否打折:否) (商品:高新技术视频,价格:80.0,是否打折:否) ============= (码农:bjsxt,敲砖钱:20000.0) (码农:is,敲砖钱:10000.0) (码农:good,敲砖钱:10368.0) ============= (商品:javase视频,价格:88.0,是否打折:是) (商品:javaee视频,价格:80.0,是否打折:否) (商品:高新技术视频,价格:80.0,是否打折:否)