有一个农业公司,专门向市场销售各类水果。在这个系统里需要描述下列的水果,葡萄(Grape),草莓(Strawberry),苹果(Apple)。
package com.test;
public interface Fruit {
/**
* 生长
*/
void grow();
/**
* 收获
*/
void harvest();
/**
* 种植
*/
void plant();
}
package com.test;
public class Apple implements Fruit {
//树龄,由于苹果是多年生植物,因此多出一个treeAge
private int treeAge;
@Override
public void grow() {
// TODO Auto-generated method stub
}
@Override
public void harvest() {
// TODO Auto-generated method stub
}
@Override
public void plant() {
// TODO Auto-generated method stub
}
public int getTreeAge() {
return treeAge;
}
public void setTreeAge(int treeAge) {
this.treeAge = treeAge;
}
}
package com.test;
public class Grape implements Fruit {
//葡萄分有籽和无籽
private boolean seedless;
@Override
public void grow() {
}
@Override
public void harvest() {
}
@Override
public void plant() {
}
public boolean isSeedless() {
return seedless;
}
public void setSeedless(boolean seedless) {
this.seedless = seedless;
}
}
package com.test;
public class Strawberry implements Fruit {
@Override
public void grow() {
}
@Override
public void harvest() {
}
@Override
public void plant() {
}
}
package com.test;
public class FruitGardener {
public static Fruit factory(String which) {
if(which.equalsIgnoreCase("apple")) {
return new Apple();
}else if(which.equalsIgnoreCase("strawberry")) {
return new Strawberry();
}else if(which.equalsIgnoreCase("grape")) {
return new Grape();
}
return null;
}
}