[Commons]——函数式编程Closure

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());
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值