标签:sea sep 不同的 入参 bak image 抽象 else img
简单工厂不是设计模式的一种,只是代码规范而且。也就是说构造一个披萨工厂出来,按不同味道生产不同的披萨。
public abstract class AbstractPizza {
public void prepare() {
System.out.println("prepare pizza");
}
public void bake(){
System.out.println("bake pizza");
}
public void cut(){
System.out.println("cut pizza");
}
public void box(){
System.out.println("pizza box");
}
}
public class CheeseAbstractPizza extends AbstractPizza {
@Override
public void prepare() {
System.out.println("prepare CheesePizza");
}
}
public class GreekAbstractPizza extends AbstractPizza {
@Override
public void prepare() {
System.out.println("prepare GreekPizza");
}
}
public class PepperoniAbstractPizza extends AbstractPizza {
@Override
public void prepare() {
System.out.println("prepare PepperoniPizza");
}
}
public class SimpleFactory {
public AbstractPizza createPizza(String type) {
if ("cheesePizza".equals(type)) {
return new CheeseAbstractPizza();
} else if ("greekPizza".equals(type)) {
return new GreekAbstractPizza();
} else if ("pepperoniPizza".equals(type)) {
return new PepperoniAbstractPizza();
}
return null;
}
}
public class PizzaStore {
SimpleFactory simplefactory = null;
PizzaStore(SimpleFactory simplefactory){
this.simplefactory = simplefactory;
}
public void orderPizza(String type) {
AbstractPizza abstractPizza = simplefactory.createPizza(type);
abstractPizza.prepare();
abstractPizza.cut();
abstractPizza.bake();
abstractPizza.box();
}
public static void main(String[] args) {
PizzaStore store = new PizzaStore(new SimpleFactory());
store.orderPizza("cheesePizza");
}
}
prepare CheesePizza
cut pizza
bake pizza
pizza box
标签:sea sep 不同的 入参 bak image 抽象 else img
原文地址:https://www.cnblogs.com/Lollipop_Hsu/p/11974157.html