Lambda --01由来配套小示例

一个简单的介绍lambda的小示例:

import java.util.*;

//数据库表Dish表中的每行记录,都是一道菜
class Dish { 
    private final String name;         //菜名
    private final boolean vegetarian;  //是否素菜
    private final int calories;        //菜还有的热量
    private final Type type;           //菜的类型
 
    public Dish(String name, boolean vegetarian, int calories, Type type) { 
        this.name = name; 
        this.vegetarian = vegetarian; 
        this.calories = calories; 
        this.type = type; 
    } 

    public String getName() { return name; } 
    public boolean isVegetarian() { return vegetarian; } 
    public int getCalories() { return calories; } 
    public Type getType() { return type; } 

    public String toString() { return name; }

    public enum Type { MEAT, FISH, OTHER } 
} 

//策略模式的主体结构代码组成:
interface MyPredicate{
    boolean test(Dish d);
}



//策略模式,不同的策略实现方案

//素菜策略
class VegetarianImpl implements MyPredicate{
    public boolean test(Dish dish){
        return dish.isVegetarian() ;
    }
}

//热量400策略
class Calories400Impl implements MyPredicate{
    public boolean test(Dish dish){
        return dish.getCalories() > 400 ;
    }
}

//FISH类别策略
class TypeFISHImpl implements MyPredicate{
    public boolean test(Dish dish){
        return dish.getType().equals(Dish.Type.FISH) ;
    }
}


public class TestLambda01
{

    public static List<Dish> filterDishes(List<Dish> dishes, MyPredicate p) { 
	List<Dish> result = new ArrayList<Dish>(); 
	for(Dish dish: dishes)

	    //if( dish.isVegetarian() ) 
	    //if( dish.getCalories() > 400 )
	    //if( dish.getType().equals(Dish.Type.FISH) )

	    if( p.test(dish) )    //主体结构代码,高度抽象出来了不同的区分诉求为一个接口
		result.add(dish); 
	    return result; 
	}

    public static void main(String[] args){

	//Dish表的所有记录,构成了一份食谱,也即菜的集合
	List<Dish> dishes = Arrays.asList( 
	    new Dish("pork", false, 800, Dish.Type.MEAT), 
	    new Dish("beef", false, 700, Dish.Type.MEAT), 
	    new Dish("chicken", false, 400, Dish.Type.MEAT), 
	    new Dish("french fries", true, 530, Dish.Type.OTHER), 
	    new Dish("rice", true, 350, Dish.Type.OTHER), 
	    new Dish("season fruit", true, 120, Dish.Type.OTHER), 
	    new Dish("pizza", true, 550, Dish.Type.OTHER), 
	    new Dish("prawns", false, 300, Dish.Type.FISH), 
	    new Dish("salmon", false, 450, Dish.Type.FISH) 
	);

        //匿名内部类的实现手法		
        for(Dish d : 
                filterDishes( dishes,
		    new MyPredicate(){
			public boolean test(Dish dish){
			    return dish.getType().equals(Dish.Type.FISH) ;
			}
		    }
	        )
	){
	    System.out.println("Dish.Type.FISH contains: "+d);
	}

        //lambda表达式传递 行为《值》的实现手法
	for(Dish d :
	        filterDishes(dishes, (dish) -> {return dish.getCalories() > 400;} )
	){
	    System.out.println("Calories400 contains: "+d);
	}

        //传统按部就班的实现手法
	for(Dish d :
	        filterDishes(dishes, new VegetarianImpl())
	){
	    System.out.println("IsVegetarian contains: "+d);
	}
    }
}

程序输出:

Dish.Type.FISH contains: prawns
Dish.Type.FISH contains: salmon
Calories400 contains: pork
Calories400 contains: beef
Calories400 contains: french fries
Calories400 contains: pizza
Calories400 contains: salmon
IsVegetarian contains: french fries
IsVegetarian contains: rice
IsVegetarian contains: season fruit
IsVegetarian contains: pizza

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值