标签:
一个工厂方法,依据传入的参数,生成对应的具体产品类;
不属于23种GOF设计模式;
工厂方法一般设成静态方法,返回值一般是抽象类或接口,具体的产品类一般继承或实现抽象类、接口;
优点:产品使用者不需要关心产品类的创建过程,与具体产品的实现类达到解耦的效果;
缺点:违背"开放--封闭"原则(OCP),因为新增一个产品类的时候,需要修改原先的工厂方法;
适用场合:当工厂类负责创建的对象比较少的时候;
【抽象产品类】首先,定义一个抽象父亲类,拥有一个name属性和打印name的方法,其次还有若干doSomething方法:
package com.pichen.dp.creationalpattern.simplefactory; public abstract class Father { private String name; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } public abstract void printName(); public abstract void doSomething01(); public abstract void doSomething02(); public abstract void doSomething03(); }
【具体产品类】定义具体的中国父亲类和美国父亲类,继承抽象父亲类,并实现printName等方法:
package com.pichen.dp.creationalpattern.simplefactory; public class ChineseFather extends Father{ public ChineseFather(String name) { this.setName(name); } @Override public void printName() { System.out.println(this.getClass().getName() + ":" + this.getName()); } @Override public void doSomething01() { // TODO Auto-generated method stub } @Override public void doSomething02() { // TODO Auto-generated method stub } @Override public void doSomething03() { // TODO Auto-generated method stub } }
package com.pichen.dp.creationalpattern.simplefactory; public class AmericanFather extends Father{ public AmericanFather(String name) { this.setName(name); } @Override public void printName() { System.out.println(this.getClass().getName() + ":" + this.getName()); } @Override public void doSomething01() { // TODO Auto-generated method stub } @Override public void doSomething02() { // TODO Auto-generated method stub } @Override public void doSomething03() { // TODO Auto-generated method stub } }
【简单工厂类】定义一个简单共产类,并定义一个静态生产方法,根据传进来的type参数生成对应的具体父亲类:
package com.pichen.dp.creationalpattern.simplefactory; import com.pichen.dp.common.exception.DPException; public class SimpleFactory { public static Father produce(String type, String name) throws DPException{ if("cn".equals(type)){ return new ChineseFather(name); }else if("us".equals(type)){ return new AmericanFather(name); }else{ throw new DPException("invalid param~"); } } }
【产品实用类】写个main函数,开始测试使用产品,具体看代码:
package com.pichen.dp.creationalpattern.simplefactory; import com.pichen.dp.common.exception.DPException; public class Main { public static Father makeMoney(String type, String name) { Father father = null; /* 调用共产方法,创建具体产品类 */ try { father = SimpleFactory.produce(type, name); } catch (DPException e) { System.out.println("exception happend when produce " + type + " obj~"); e.printStackTrace(); } /* 具体赚钱的步骤 */ if(father != null){ father.doSomething01(); father.doSomething02(); father.doSomething03(); } return father; } public static void main(String[] args) { Father cnFather = Main.makeMoney("cn", "cn father"); if (cnFather != null) cnFather.printName(); Father usFather = Main.makeMoney("us", "us father "); if (usFather != null) usFather.printName(); Father apple = Main.makeMoney("fruit","apple"); if (apple != null) apple.printName(); } }
【其它】异常类DPException:
package com.pichen.dp.common.exception; public class DPException extends Exception{ /** * */ private static final long serialVersionUID = 8110345414312602844L; public DPException(String msg){ super(msg); System.out.println("DPException~"); } }
标签:
原文地址:http://www.cnblogs.com/chenpi/p/5139050.html