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();
}

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值