Style:Mac
Series:Java
Since:2018-09-26
End:2018-09-26
Total Hours:1
Degree Of Diffculty:5
Degree Of Mastery:5
Practical Level:5
Desired Goal:5
Archieve Goal:3
Gerneral Evaluation:3
Writer:kingdelee
Related Links:
http://www.cnblogs.com/kingdelee/
1.把方法当做参数/值进行传递,将函数作为值的思想
第2章 行为参数化
谓词
2.2 行为参数化
你在上一节中已经看到了,你需要一种比添加很多参数更好的方法来应对变化的需求。让 我们后退一步来看看更高层次的抽象。一种可能的解决方案是对你的选择标准建模:你考虑的 是苹果,需要根据 Apple 的某些属性(比如它是绿色的吗?重量超过150克吗?)来返回一个 boolean值。我们把它称为谓词(即一个返回boolean值的函数)。让我们定义一个接口来对选 择标准建模:
public interface ApplePredicate{ boolean test (Apple apple); }
现在你就可以用ApplePredicate的多个实现代表不同的选择标准了,比如(如图2-1所示):
public class AppleHeavyWeightPredicate implements ApplePredicate{ public boolean test(Apple apple){ return apple.getWeight() > 150; } } public class AppleGreenColorPredicate implements ApplePredicate{ public boolean test(Apple apple){ return "green".equals(apple.getColor()); } }
3.
中间操作和终端操作