简单工厂其实不是一个设计模式,比较像一种编程习惯!
首先看一下代码,从代码中理解这个编程习惯。
举个做披萨的例子:
Pizza类代码:
public interface Pizza { void prepare(); void box(); void bake(); void cut(); }具体的披萨CheesePizza:
public class CheesePizza implements Pizza { private String tag="CheesePizza:"; @Override public void prepare() { System.out.println(tag+"pripare()"); } @Override public void box() { System.out.println(tag+"box()"); } @Override public void bake() { System.out.println(tag+"bake()"); } @Override public void cut() { System.out.println(tag+"cut()"); } }具体的披萨ClamPizza:
public class ClamPizza implements Pizza { private String tag="ClamPizza:"; @Override public void prepare() { System.out.println(tag+"pripare()"); } @Override public void box() { System.out.println(tag+"box()"); } @Override public void bake() { System.out.println(tag+"bake()"); } @Override public void cut() { System.out.println(tag+"cut()"); } }
public class SimplePizzaFactory { public Pizza createPizza(String tpye){ Pizza pizza = null; if (tpye.equals("cheese")){ pizza=new CheesePizza(); }else if (tpye.equals("clam")){ pizza=new ClamPizza(); } return pizza; } }披萨订单类PizzaStore:
public class PizzaStore { SimplePizzaFactory simplePizzaFactory; protected PizzaStore(SimplePizzaFactory simplePizzaFactory) { this.simplePizzaFactory = simplePizzaFactory; } protected final Pizza orderPizza(String type){ // Pizza pizza=pizzafactory.createP(type); Pizza pizza=simplePizzaFactory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } }
public static void main(String[] args){ PizzaStore chijiaPizzaStore=new PizzaStore(new SimplePizzaFactory()); chijiaPizzaStore.orderPizza("cheese"); chijiaPizzaStore.orderPizza("clam"); }
CheesePizza:pripare() CheesePizza:bake() CheesePizza:cut() CheesePizza:box() ClamPizza:pripare() ClamPizza:bake() ClamPizza:cut() ClamPizza:box()从代码中可以看出,创建对象之前,其实是把对象的共同操作方法抽象出来,为以后对象的扩展做准备,这个接口可以是接口类或者是抽象类,由具体的子类实现或者继承。但是对于简单工厂里的方法,只确定创建哪个具体的对象。
来看一个比较通用的工厂模式框架代码:
抽象产品类:
public abstract class Product { public void method1(){ }; public abstract void method2(); }具体产品类
public class ConcreteProduct1 implements Product{ @Override public void method2() { } }public class ConcreteProduct2 implements Product{ @Override public void method2() { } }
public abstract class Creator { /** * * @param c * @param <T> 参数类型可以自行设置,通常为String,emum,Class等,也可以为空 * @return */ public abstract <T extends Product> T createProducts(Class<T> c); }具体工厂类:
public class ConcreteFactory extends Creator { @Override public <T extends Product> T createProducts(Class<T> c) { Product product=null; try { product= (Product) Class.forName(c.getName()).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return (T) product; } }
Creator creator=new ConcreteFactory(); Product product= creator.createProducts(COncreteProduct2.class);
如果一个模块仅需要一个工厂类,没有必要把它生产出来,使用静态方法就可以啦,把上述的抽象工厂类直接替换为具体工厂类,并使用静态方法就可以,这样就变为简单工厂模式,或者是静态工厂模式。
写的不好,希望多提意见,共同进步学习!谢谢
设计模式---简单工厂模式(学习笔记),布布扣,bubuko.com
原文:http://blog.csdn.net/zouchengxufei/article/details/24295165