SimipleFactory 简单工厂模式

Fruti.java

package com.tcs.SimipleFactory;
/**
* The interface of Fruit difine the interface which all fruit must implement its method;
*/
public interface Fruit {
void grow();
void harvest();
void plant();
}

Apple.java
package com.tcs.SimipleFactory;
/**
* Apple implements Fruit and also it has its own attribute and method, such as treeAge and method log();
*/

public class Apple implements Fruit {
//Apple 出了继承Fruit之外,还有自己的一个属性 treeAge。

private int treeAge;



public void grow() {
log("Apple is growing");

}

public void harvest() {
log("Apple has been harvested");

}

public void plant() {
log("Apple has been planted");

}

public static void log(String s){
System.out.println(s);
}

/**
* @return the treeAge
*/
public final int getTreeAge() {
return treeAge;
}

/**
* @param treeAge the treeAge to set
*/
public final void setTreeAge(int treeAge) {
this.treeAge = treeAge;
}
}


Grape.java
package com.tcs.SimipleFactory;
/**
* Grape implements Fruit and also it has its own attribute and method, such as seedLess and method log();
*/

public class Grape implements Fruit {
private boolean seedLess;
public void grow() {
log("Grape is growing");

}

public void harvest() {
log("Grape has been harvested");

}

public void plant() {
log("Grape has been planted");

}

public static void log(String s){
System.out.println(s);
}

/**
* @return the seedLess
*/
public final boolean isSeedLess() {
return seedLess;
}

/**
* @param seedLess the seedLess to set
*/
public final void setSeedLess(boolean seedLess) {
this.seedLess = seedLess;
}



}

StrawBerry.java
package com.tcs.SimipleFactory;
/*
*StrawBerry  implements Fruit
*
*/
public class StrawBerry implements Fruit {

public void grow() {
log("StrawBerry is growing");

}

public void harvest() {
log("StrawBerry has been harvested");

}

public void plant() {
log("StrawBerry has been planted");

}

public static void log(String s){
System.out.println(s);
}
}

FruitGardener.java
package com.tcs.SimipleFactory;
/*
*
*Gardener will create some fruit by his client's request,Also if the request is a bad one.He will throws it  a BadFruitException which he create it own
*/
public class FruitGardener {
public static Fruit factory(String which) throws BadFruitException {
if (which.equalsIgnoreCase("apple")) {
return new Apple();
} else if (which.equalsIgnoreCase("strawberry")) {
return new StrawBerry();
} else if (which.equalsIgnoreCase("grape")) {
return new Grape();
} else {
throw new BadFruitException("Bad Fruit Request");
}
}
}

BadFruitException.java
package com.tcs.SimipleFactory;
/*
*This exception was create by Gardener and handing Exception
*
*/
public class BadFruitException extends Exception {
public BadFruitException(String msg) {
super(msg);
}
}


TestMain.java
package com.tcs.SimipleFactory;
/*
*when test this at client,Customer just call FruitGardener's  method factory.Then there will be much fruit to plant grow and harvest.Also hand some error request
*
*
*/
public class TestMain {

/**
* @param args
*/
public static void main(String[] args) {
try {
FruitGardener.factory("apple").grow();//call method which you want to call
FruitGardener.factory("strawberry").harvest();//call method which you want to call
FruitGardener.factory("grape").plant();//call method which you want to call
FruitGardener.factory("bad request"); //test when bad fruit
} catch (BadFruitException e) {
e.printStackTrace();
}

}

}

你可能感兴趣的:(apple)