首页 > 代码库 > 设计模式---简单工厂模式(学习笔记)

设计模式---简单工厂模式(学习笔记)

简单工厂事实上不是一个设计模式,比較像一种编程习惯!

首先看一下代码,从代码中理解这个编程习惯。

举个做披萨的样例:

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

假设一个模块仅须要一个工厂类,没有必要把它生产出来,使用静态方法就能够啦,把上述的抽象工厂类直接替换为详细工厂类,并使用静态方法就能够,这样就变为简单工厂模式,或者是静态工厂模式

写的不好,希望多提意见,共同进步学习!谢谢