标签:system xtend ack new package pre ext sys []
package DesignPattern;
public class SimpleFactoryPattern {
public static class Pizza{
public String type="Pizza";
public void prepare(){}
public void bake(){}
public void cut(){}
public void box(){}
}
public static class CheesePizza extends Pizza{
CheesePizza(){
this.type="cheesePizza";
}
}
public static class PeperonPizza extends Pizza{
PeperonPizza(){
this.type="peperonPizza";
}
}
public static class PizzaStore{
SimpleFactory factory;
public PizzaStore(SimpleFactory factory){
this.factory=factory;
}
public Pizza orderPizza(String type){
Pizza pizza=SimpleFactory.createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
public static class SimpleFactory{
public static Pizza createPizza(String type){
Pizza pizza = null;
if (type.equals("cheese")){
pizza= new CheesePizza();
}
else if(type.equals("pepperoni")){
pizza= new PeperonPizza();
}
return pizza;
}
}
//简单工厂更像是一种编码风格,只是简单的将复杂的创建方法独立出来放在简单工厂里
//适用于复杂的创建过程,一致的加工流程
public static void main(String[] args) {
SimpleFactory simpleFactory = new SimpleFactory();
PizzaStore pizzaStore = new PizzaStore(simpleFactory);
Pizza cheesePizza = pizzaStore.orderPizza("cheese");
System.out.println(cheesePizza.type);
}
}
标签:system xtend ack new package pre ext sys []
原文地址:https://www.cnblogs.com/zhouyu0-0/p/10724337.html