工厂模式看了几天了,以前曾经有看过,不过没去细看,只是简单的返回一些对象,没考虑到底使用的是哪种对象,这几天翻了一
些资料,给搞晕了,好在今天翻到一个高手的BLOG,渐渐的清楚了,以下是引用的一些重点段落
1.1 简单工厂(Simple Factory)模式
package factory.briefnessfactory; public interface Fruit { /** * 种植 */ void plant(); /** * 生长 */ void grow(); /** * 收获 */ void harvest(); }
package factory.briefnessfactory; public class Apple implements Fruit { private int treeAge; /** * 种植 */ public void plant() { System.out.println("Apple has been planted."); } /** * 生长 */ public void grow() { System.out.println("Apple is growing..."); } /** * 收获 */ public void harvest() { System.out.println("Apple has been harvested."); } /** * @return 返回树龄 */ public int getTreeAge() { return treeAge; } /** * 设置树龄 */ public void setTreeAge(int treeAge) { this.treeAge = treeAge; } }
package factory.briefnessfactory; public class Strawberry implements Fruit { /** * 生长 */ public void grow() { System.out.println("Strawberry is growing..."); } /** * 收获 */ public void harvest() { System.out.println("Strawberry has been harvested."); } /** * 种植 */ public void plant() { System.out.println("Strawberry has been planted."); } /** * 辅助方法 */ public static void log(String msg) { System.out.println(msg); } }
package factory.briefnessfactory; public class Grape implements Fruit { private boolean seedless; //是否有籽 /** * 种植 */ public void plant() { System.out.println("Grape has been planted."); } /** * 生长 */ public void grow() { System.out.println("Grape is growing..."); } /** * 收获 */ public void harvest() { System.out.println("Grape has been harvested."); } /** * @return 是否有籽 */ public boolean getSeedless() { return seedless; } /** * 有无籽的赋值方法 */ public void setSeedless(boolean seedless) { this.seedless = seedless; } /** * 辅助方法 */ public static void log(String msg) { System.out.println(msg); } }
package factory.briefnessfactory; public class FruitGardener { /** * 静态工厂方法 * @param which :具体的产品名称 * @return 一个水果对象 * @throws BadFruitException */ 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"); } } }
1.1.2.6 工厂异常定义类:BadFruitException.java
package factory.briefnessfactory; public class BadFruitException extends Exception { public BadFruitException(String msg) { super(msg); //调用父类的构造方法 } }
1.1.2.7 一般工厂模式的测试类
package factory.briefnessfactory; public class TestApp { /** * 测试方法 */ private void test(String fruitName) { try { Fruit f = FruitGardener.factory(fruitName); System.out.println("恭喜!生产了一个水果对象:" + fruitName); } catch (BadFruitException e) { System.out.println("对不起!工厂目前不能生产你所要的产品:" + fruitName); System.out.println(e.getMessage()); //输出异常信息 e.printStackTrace(); //输出异常堆栈信息 } } /** * 应用入口方法 */ public static void main(String args[]) { TestApp t = new TestApp(); t.test("apple"); t.test("grape"); t.test("strawberry"); t.test("car"); //此处会抛异常,水果工厂能生产car(轿车)吗!哈哈哈哈... } }
1.1.2.8 测试运行结果
恭喜!生产了一个水果对象:apple 恭喜!生产了一个水果对象:grape 恭喜!生产了一个水果对象:strawberry 对不起!工厂目前不能生产你所要的产品:car Bad fruit request factory.briefnessfactory.BadFruitException: Bad fruit request at factory.briefnessfactory.FruitGardener.factory(FruitGardener.java:18) at factory.briefnessfactory.TestApp.test(TestApp.java:9) at factory.briefnessfactory.TestApp.main(TestApp.java:25)
1.1.4 简单工厂模式的实现
本来想自己写,不过实在想不出有比这篇更具有说服力的了