工厂模式

2018-01-10  本文已影响6人  yangzai
简单工厂模式
//披萨抽象类
public abstract class Pizza {
    protected String name;
        //只有原材料不同
    public abstract void prepare();
    public void bake(){
        System.out.println(name+" baking;");
    }
    public void cut(){
        System.out.println(name+" cutting;");
    }
    public void box(){
        System.out.println(name+" boxing;");
    }
    public void setname(String name){
        this.name=name;
    }
}
public class GreekPizza extends Pizza {

    @Override
    public void prepare() {
        // TODO Auto-generated method stub
        super.setname("GreekPizza");
        
        System.out.println(name+" preparing;");
    }

}

public class CheesePizza extends Pizza {
    @Override
    public void prepare() {
        super.setname("CheesePizza");
        System.out.println(name+" preparing;");
    }
}
public class PepperPizza extends Pizza {
    @Override
    public void prepare() {
        super.setname("PepperPizza");   
        System.out.println(name+" preparing;");
    }
}

public class SimplePizzaFactory {
    public Pizza CreatePizza(String ordertype) {
        Pizza pizza = null;
        if (ordertype.equals("cheese")) {
            pizza = new CheesePizza();
        } else if (ordertype.equals("greek")) {
            pizza = new GreekPizza();
        } else if (ordertype.equals("pepper")) {
            pizza = new PepperPizza();
        }
        return pizza;
    }
}

简单工厂,仅仅是把对象的创建封装到工厂中,不对外暴露细节。

public class OrderPizza {
    SimplePizzaFactory mSimplePizzaFactory;
        //注入工厂
    public OrderPizza(SimplePizzaFactory mSimplePizzaFactory) {
        setFactory(mSimplePizzaFactory);
    }
    public void setFactory(SimplePizzaFactory mSimplePizzaFactory) {
        Pizza pizza = null;
        String ordertype;
        this.mSimplePizzaFactory = mSimplePizzaFactory;
        do {
            ordertype = gettype();
            pizza = mSimplePizzaFactory.CreatePizza(ordertype);
            if (pizza != null) {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }
        } while (true);
    }
        //获取用户的需求
    private String gettype() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza type:");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
//测试类
public class PizzaStroe {
    public static void main(String[] args) {
        SimplePizzaFactory mSimplePizzaFactory;
        OrderPizza mOrderPizza;
        mOrderPizza=new OrderPizza(new SimplePizzaFactory());
    }
}
工厂方法
public class LDCheesePizza extends Pizza {
    @Override
    public void prepare() {
        super.setname("LDCheesePizza");
        System.out.println(name+" preparing;");
    }
}
public class LDPepperPizza extends Pizza {
    @Override
    public void prepare() {
        super.setname("LDPepperPizza");
        System.out.println(name+" preparing;");
    }
}
public class NYPepperPizza extends Pizza {
    @Override
    public void prepare() {
        super.setname("NYPepperPizza");
        System.out.println(name+" preparing;");
    }
}
public class NYCheesePizza extends Pizza {
    @Override
    public void prepare() {
        super.setname("NYCheesePizza");
        System.out.println(name+" preparing;");
    }
}

public abstract class OrderPizza {
    public OrderPizza() {
        Pizza pizza = null;
        String ordertype;
        do {
            ordertype = gettype();
                        //这里抽象出创建产品的方法,由子类实现
            pizza = createPizza(ordertype);
                        .....
        } while (true);
    }
    abstract Pizza createPizza(String ordertype);
       ....
}
//抽象方法的实现类
public class LDOrderPizza extends OrderPizza {
    @Override
    Pizza createPizza(String ordertype) {
        Pizza pizza = null;
        if (ordertype.equals("cheese")) {
            pizza = new LDCheesePizza();
        } else if (ordertype.equals("pepper")) {
            pizza = new LDPepperPizza();
        }
        return pizza;
    }
}

public class NYOrderPizza extends OrderPizza {
    @Override
    Pizza createPizza(String ordertype) {
        Pizza pizza = null;
        if (ordertype.equals("cheese")) {
            pizza = new NYCheesePizza();
        } else if (ordertype.equals("pepper")) {
            pizza = new NYPepperPizza();
        }
        return pizza;
    }
}
//测试
public class PizzaStroe {
    public static void main(String[] args) {
        OrderPizza mOrderPizza;
               //创建不同地域的工厂就生产不同地域的披萨
        mOrderPizza=new NYOrderPizza(); 
    }
}

这样我们就能够根据不同的地域生产不同的披萨了。

抽象工厂
//抽象工厂
public interface AbsFactory {
    public Pizza CreatePizza(String ordertype) ;
}
public class LDFactory implements AbsFactory {
    @Override
    public Pizza CreatePizza(String ordertype) {
        Pizza pizza = null;
        if (ordertype.equals("cheese")) {
            pizza = new LDCheesePizza();
        } else if (ordertype.equals("pepper")) {
            pizza = new LDPepperPizza();
        }
        return pizza;
    }
}
public class NYFactory implements AbsFactory {
    @Override
    public Pizza CreatePizza(String ordertype) {
        Pizza pizza = null;
        if (ordertype.equals("cheese")) {
            pizza = new NYCheesePizza();
        } else if (ordertype.equals("pepper")) {
            pizza = new NYPepperPizza();
        }
        return pizza;
    }
}
//使用抽象工厂
public class OrderPizza {
    AbsFactory mFactory;
       //注入具体工厂
    public OrderPizza(AbsFactory mFactory) {
        setFactory(mFactory);
    }
    public void setFactory(AbsFactory mFactory) {
        Pizza pizza = null;
        String ordertype;
        this.mFactory = mFactory;
        do {
            ordertype = gettype();
                       //通过工厂生产披萨
            pizza = mFactory.CreatePizza(ordertype);
                      ....
        } while (true);
    }
   .....
}
//测试
public class PizzaStroe {
    public static void main(String[] args) {
        OrderPizza mOrderPizza;
        mOrderPizza=new OrderPizza(new LDFactory());
    }
}

不同地域的披萨组成了多个产品族,我们可以通过不同的工厂生产各个族的产品

上一篇下一篇

猜你喜欢

热点阅读