最近在学习java中了解了行为参数化,在这里谈谈自己的理解,如果有认识不正确的地方还请指出。
行为参数化有很多好处,比如能够减少代码量、使程序结构更加明朗等。
比如:
有一个苹果类Apple,里面包括苹果的颜色及重量:
class Apple{
int id;
private String color;
private int weight;
Apple(int id,String color,int weight){
this.id=id;
this.color=color;
this.weight=weight;
}
public String getColor() {
return this.color;
}
public int getWeight() {
return this.weight;
}
}
有一个向量inventory,里面存放许多苹果:
Vector <Apple> inventory=new Vector<>();
inventory.add(new Apple(0,"Green",12));
inventory.add(new Apple(1,"Red",16));
inventory.add(new Apple(2,"Green",17));
现在要从inventory中选出所有绿色的苹果,方法为:
public static Vector<Apple> getGreenApple(Vector<Apple> inventory) {
Vector <Apple> greenApple=new Vector<>();
for(Apple apple:inventory) {
if(apple.getColor().equals("Green"))
greenApple.add(apple);
}
return greenApple;
}
调用时代码为:
Vector<Apple> greenApple=getGreenApple(inventory);
这时候如果需要选出所有红色的苹果,则又需要重新写一个方法:
public static Vector<Apple> getRedApple(Vector<Apple> inventory) {
Vector <Apple> redApple=new Vector<>();
for(Apple apple:inventory) {
if(apple.getColor().equals("Red"))
redApple.add(apple);
}
return redApple;
}
你会发现这个方法和上面得到绿色苹果的方法有很多相似,不同的只是一个要红苹果,一个要绿苹果(事实上,这个方法是我复制之前方法然后修改得到的)。程序中的代码长且有很多重复,不能突出重点。另外,日后你可能会有得到重量大于15或小于12等等很多需求,若是每个需求都写一个方法,程序就会非常臃肿。
这时候,使用行为参数化便能够使代码变得简洁,所谓行为参数化,即将行为(比方说得到绿苹果)作为方法的参数:
public static Vector<Apple> getSelectApple(Vector<Apple> inventory,Predicate<Apple> p){
Vector <Apple> selectApple=new Vector<>();
for(Apple apple:inventory) {
if(p.test(apple))
selectApple.add(apple);
}
return selectApple;
}
其中Predicate是java.util.function中定义的接口,内部只有一个test方法。
相比于前面的getGreenApple和getRedApple方法,getSelectApple方法多了一个参数,此时如果要得到绿苹果,只需要实现Predicate接口,并重写其中的test方法,然后将其作为对象应用于getSelectApple方法即可:
Predicate <Apple>p=new Predicate<Apple>(){
public boolean test(Apple apple) {
return apple.getColor().equals("Green");
}
};
Vector<Apple> greenApple=getSelectApple(inventory,p);
代码量有一定的减少,它成功的完成了行为参数化,日后有任何需求时只需要实现Predicate接口并重写test方法,而方法getSelectApple方法并不需要修改,使程序结构更加清楚。当有大量的需求时能够有效减少代码量(毕竟只需要实现Predicate接口而不需要重新写很多方法)。
到此为止,我们算是完成了任务,但是,我们还需要一遍一遍的实现Predicate接口,这其中也有很多重复的代码:
Predicate <Apple>p=new Predicate<Apple>(){
public boolean test(Apple apple) {
}
};
如果能够减少这部分的代码,便能够进一步使程序简洁。
首先,我们可以使用匿名接口,这就省去了Predicate p申明:
Vector<Apple> greenApple=getSelectApple(inventory,
new Predicate<Apple>(){
public boolean test(Apple apple) {
return apple.getColor().equals("Green");
}
});
但是代码还是不够简洁,更进一步,可以使用Lambda表达式:
Vector<Apple> greenApple=getSelectApple(inventory,apple->apple.getColor().equals("Green"));
至此,完成了从
public static Vector<Apple> getGreenApple(Vector<Apple> inventory) {
Vector <Apple> greenApple=new Vector<>();
for(Apple apple:inventory) {
if(apple.getColor().equals("Green"))
greenApple.add(apple);
}
return greenApple;
}
Vector<Apple> greenApple=getGreenApple(inventory);
到
public static Vector<Apple> getSelectApple(Vector<Apple> inventory,Predicate<Apple> p){
Vector <Apple> selectApple=new Vector<>();
for(Apple apple:inventory) {
if(p.test(apple))
selectApple.add(apple);
}
return selectApple;
}
Vector<Apple> greenApple=getSelectApple(inventory,apple->apple.getColor().equals("Green"));
的简化。
好吧,看起来并没有减少很多。事实上,当需求增加时,便能够看到明显的简化,随便一想便能想到,如果要增加10个需求,比如说得到红苹果等,使用第一种写法代码量将扩大10倍(重新写10遍方法,约90行代码),但是用后面的写法只增加10行代码。另外,后面的写法使程序逻辑更加清晰,将test方法与getSelectApple方法分开,test负责选择合适的苹果,getSelectApple负责将合适的苹果放入一个向量并返回。
最后做一个总结,行为参数化即增加一个参数,这个参数很可能是一个接口,通过对接口中的方法重写来完成行为。借助Lambda表达式,行为参数化能够使程序代码最少、结构清晰,面对日后需求的增加或者改动能够有很大的拓展空间。