判断语句过多,逻辑相同,重复太多 可以使用策略模式
abstract class Animal{
abstract String eat(String food);
}
class center{
private Animal animal;
center(Animal animal){
this.animal=animal;
}
public String result(String food){
return animal.eat(food);
}
}
class cat extends Animal{
@Override
String eat(String food) {
return "cat eat "+food;
}
}
class dog extends Animal{
@Override
String eat(String food) {
return "dog eat "+food;
}
}
//测试
@Test
void contextLoads() {
center center = new center(new cat());
System.out.println( center.result("鱼"));
}