public class test3 {
public static void main(String[] args) {
//basic();
//ifClosure();
//whileClosure();
chainClosure();
}
public static void basic(){//基本功能,函数式编程Closure,闭包封闭特定的业务功能
List<Empolyee> list=new ArrayList<Empolyee>();
list.add(new Empolyee("张三", 10000));
list.add(new Empolyee("李四", 15000));
Closure<Empolyee> closure=new Closure<Empolyee>() {//业务功能
@Override
public void execute(Empolyee empolyee) {
empolyee.setSalary(empolyee.getSalary()*1.2);
}
};
CollectionUtils.forAllDo(list, closure);//工具类,CollectionUtils.forAllDo(容器, 功能类对象)
Iterator<Empolyee> iterator=list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
/**
* 二选一
* 如果是打折商品,进行9折,否则满百减20
*/
public static void ifClosure(){//ifclosure.ifclosure(断言,功能1,功能2)
List<Goods> goodsList=new ArrayList<Goods>();
goodsList.add(new Goods("javase", 150, true));
goodsList.add(new Goods("javaee", 110, false));
goodsList.add(new Goods("javaWeb", 101, false));
Closure<Goods> substract=new Closure<Goods>() {//满百减20,业务功能
@Override
public void execute(Goods goods) {
if (goods.getPrice()>100) {
goods.setPrice(goods.getPrice()-20);
}
}
};
Closure<Goods> discount=new Closure<Goods>() {//打折,业务功能
@Override
public void execute(Goods goods) {
if (goods.isDiscount()) {
goods.setPrice(goods.getPrice()*0.9);
}
}
};
Predicate<Goods> predicate=new Predicate<Goods>() {//判断
@Override
public boolean evaluate(Goods goods) {
return goods.isDiscount();
}
};
Closure<Goods> closure=new IfClosure<>(predicate, discount, substract);//二选一
CollectionUtils.forAllDo(goodsList, closure);
Iterator<Goods> iterator=goodsList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
/**
* 确保所有的员工都大于100000,如果已经超过不再上涨
*/
public static void whileClosure(){//whileclosure.whileclosure(断言,功能,标识);
List<Empolyee> empList=new ArrayList<Empolyee>();//数据
empList.add(new Empolyee("bjsxt",10000));
empList.add(new Empolyee("wext",1000));
empList.add(new Empolyee("isxt",5000));
Closure<Empolyee> closure=new Closure<Empolyee>() {//业务功能
@Override
public void execute(Empolyee empolyee) {
empolyee.setSalary(empolyee.getSalary()*1.2);;
}
};
Predicate<Empolyee> predicate=new Predicate<Empolyee>() {//判断
@Override
public boolean evaluate(Empolyee empolyee) {
return empolyee.getSalary()<10000;
}
};
//false表示while结构,先判断后执行,true, do。。。while,先执行后判断
Closure<Empolyee> whileClosure=WhileClosure.whileClosure(predicate, closure, false);
CollectionUtils.forAllDo(empList, whileClosure);//工具
for (Empolyee emp : empList) {
System.out.println(emp);
}
}
/**
* 二选一
*满百再减20后,如果是打折商品满100,再9折
*/
public static void chainClosure(){
List<Goods> goodsList=new ArrayList<Goods>();
goodsList.add(new Goods("javase", 150, true));
goodsList.add(new Goods("javaee", 200, false));
goodsList.add(new Goods("javaWeb", 180, false));
Closure<Goods> substract=new Closure<Goods>() {//业务功能1
@Override
public void execute(Goods goods) {
if (goods.getPrice()>100) {
goods.setPrice(goods.getPrice()-20);
}
}
};
Closure<Goods> discount=new Closure<Goods>() {//业务功能2
@Override
public void execute(Goods goods) {
if (goods.isDiscount()) {
goods.setPrice(goods.getPrice()*0.9);
}
}
};
//ChainedClosure.chainedClosure(功能列表);
Closure<Goods> closure=ChainedClosure.chainedClosure(discount,substract);//综合业务逻辑
CollectionUtils.forAllDo(goodsList, closure);//工具
Iterator<Goods> iterator=goodsList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
[Commons]——函数式编程Closure
最新推荐文章于 2024-03-21 15:54:09 发布