闭包之Closure

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.ChainedClosure;
import org.apache.commons.collections4.functors.IfClosure;
import org.apache.commons.collections4.functors.WhileClosure;

/**
 * 函数式编程之Closure(闭包 封装特定的业务功能)
 * 1.基本操作
 *   Closure
 * 2.IfClosure  
 *   IfClosure.ifClosure(断言,功能1,功能2)-->判断条件是否满足,满足执行功能1,否则执行功能2
 * 3.WhileClosure 
 *   WhileClosure.whileClosure(断言,功能,标识) 
 * 4.ChainedClosure.chainedClosure(功能列表);
 *   CollectionUtils.forAllDo(容器,功能类对象);
 * @author Administrator
 *
 */
public class Demo03 {

    public static void main(String[] args) {
        test1();
        test2();
        test3();
        test4();
    }

    public static  void test4(){
        System.out.println("==========测试ChainedClosure==========");
        List<Goods> list =new ArrayList<Goods>();
        list.add(new Goods("javase视频",120,true));
        list.add(new Goods("javaee视频",100,false));
        list.add(new Goods("高新技术视频",80,false));

         //满百减20
        Closure<Goods> subtract=new Closure<Goods>(){
            public void execute(Goods goods) {
                if(goods.getPrice()>=100){
                    goods.setPrice(goods.getPrice()-20);
                }
            }};
        //打折
        Closure<Goods> discount=new Closure<Goods>(){
            public void execute(Goods goods) {
                if(goods.isDiscount()){
                    goods.setPrice(goods.getPrice()*0.9);
                }
            }}; 
        //依次执行功能列表,哪个放在前面就先执行哪一个   
        Closure<Goods> clo = ChainedClosure.chainedClosure(subtract,discount);
        CollectionUtils.forAllDo(list, clo);
        Iterator<Goods> it = list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

    public static void test3(){
        System.out.println("==========测试WhileClosure==========");
        //数据
        List<Employee> list =new ArrayList<Employee>();
        list.add(new Employee("bjsxt",20000));
        list.add(new Employee("is",10000));
        list.add(new Employee("good",5000));

        Closure<Employee> closure = new Closure<Employee>() {
            @Override
            public void execute(Employee input) {
                input.setSalary(input.getSalary()*1.2);
            }
        };

        //判断
        Predicate<Employee> pre=new Predicate<Employee>(){
            @Override
            public boolean evaluate(Employee emp) {
                return emp.getSalary()<10000;
            }           
        };  
        //false 表示 while结构 先判断后执行   true do..while 先执行后判断
        Closure<Employee> clo = WhileClosure.whileClosure(pre, closure, false);
        CollectionUtils.forAllDo(list, clo);
        Iterator<Employee> it = list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

    public static void test2(){
        System.out.println("==========测试IfClosure==========");
        List<Goods> list =new ArrayList<Goods>();
        list.add(new Goods("javase视频",120,true));
        list.add(new Goods("javaee视频",100,false));
        list.add(new Goods("高新技术视频",80,false));

         //满百减20
        Closure<Goods> subtract=new Closure<Goods>(){
            public void execute(Goods goods) {
                if(goods.getPrice()>=100){
                    goods.setPrice(goods.getPrice()-20);
                }
            }};
        //打折
        Closure<Goods> discount=new Closure<Goods>(){
            public void execute(Goods goods) {
                if(goods.isDiscount()){
                    goods.setPrice(goods.getPrice()*0.9);
                }
            }}; 

        //判断
        Predicate<Goods> pre=new Predicate<Goods>(){
            public boolean evaluate(Goods goods) {
                return goods.isDiscount();
            }}; 

        Closure<Goods> ifcol = IfClosure.ifClosure(pre, discount, subtract);
        CollectionUtils.forAllDo(list, ifcol);
        Iterator<Goods> it = list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

    public static void test1(){
        System.out.println("==========测试Closure==========");
        //数据
        List<Employee> list =new ArrayList<Employee>();
        list.add(new Employee("bjsxt",20000));
        list.add(new Employee("is",10000));
        list.add(new Employee("good",5000));

        Closure<Employee> closure = new Closure<Employee>() {
            @Override
            public void execute(Employee input) {
                input.setSalary(input.getSalary()*1.2);
            }
        };
        CollectionUtils.forAllDo(list, closure);
        Iterator<Employee> it = list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

}

输出结果:
        ==========测试Closure==========
        (码农:bjsxt,工资:24000.0)
        (码农:is,工资:12000.0)
        (码农:good,工资:6000.0)
        ==========测试IfClosure==========
        (商品:javase视频,价格:108.0,是否打折:是)
        (商品:javaee视频,价格:80.0,是否打折:否)
        (商品:高新技术视频,价格:80.0,是否打折:否)
        ==========测试WhileClosure==========
        (码农:bjsxt,工资:20000.0)
        (码农:is,工资:10000.0)
        (码农:good,工资:10368.0)
        ==========测试ChainedClosure==========
        (商品:javase视频,价格:90.0,是否打折:是)
        (商品:javaee视频,价格:80.0,是否打折:否)
        (商品:高新技术视频,价格:80.0,是否打折:否)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值