commons的Closure——高琪JAVA300講筆記之commons

案例一:java

其中,Employee類就是與上一篇的相同,這裏不重複貼了。apache

還用到了一個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  * 一、Closure
 17  * 二、ifClosure  IfClosure.ifClosure(斷言,功能1,功能2)
 18  * 三、whileClosure  WhileClosure.whileClosure(斷言,功能,標識)
 19  * 四、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 }

運行結果:ide

(碼農: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,是否打折:否)
相關文章
相關標籤/搜索