工厂模式-A

package D_Factory.a;

/**
* 只适用一个地区的披萨店,当在别地建立披萨店时,由于地区口味差异,一个工厂无法满足。 解决方案,根据地区建立多个工厂
*
*/
public class SimplePizzaFactory {
public Pizza createPizza(String type) {
Pizza pizza = null;
if (type.equals(“cheese”)) {
pizza = new CheesePizza();
} else if (type.equals(“pepperoni”)) {
pizza = new PepperoniPizza();
}
return pizza;
}
}
package D_Factory.a;

public class PizzaStore {
SimplePizzaFactory factory;

public PizzaStore(SimplePizzaFactory factory) {
    this.factory = factory;
}

public Pizza orderPizza(String type) {
    Pizza pizza;
    pizza = factory.createPizza(type);
    pizza.prepare();
    pizza.bake();
    pizza.cut();
    pizza.box();
    return pizza;
}

}
package D_Factory.a;

import D_Factory.d.Dough;

public abstract class Pizza {
public String name;
public Dough dough;
public String sauce;
public String cheese;
public String clam;

public void prepare() {
    System.out.println("prepare");
}

public void bake() {
    System.out.println("bake");
}

public void cut() {
    System.out.println("cut");
}

public void box() {
    System.out.println("box");
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

}
package D_Factory.a;

public class PepperoniPizza extends Pizza {

}
package D_Factory.a;

public class CheesePizza extends Pizza {

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值