factory pattern

let's say you have a pizza shop. you might end up writing some code like this:


Pizza orderPizza() {
Pizza pizza = new Pizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;


When you need more than one type of pizza


Pizza orderPizza(String type) {
Pizza pizza;
if (type.equals("cheese")) {
pizza = new CheesePizza();
} else if (type.equals("greek")) {
pizza = new GreekPizza();
} else if (type.equals("peoperoni")) {
pizza = new PepperoniPizza();
}
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;


Clearly, dealing with which concrete class is instantiated is really messing up our orderPizza() method and preventing it from being closed for modification
[IMG]http://i45.tinypic.com/2ryfr7l.jpg[/IMG]

What should we do then?
Well, what we're going to do is take the creation code and move it out into another object that is only going to be concerned with creating pizzas.

We've got a name for this new object: we call it a factory.
[IMG]http://i50.tinypic.com/35bd05t.jpg[/IMG]

Reworking the pizzastore class

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

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();
} else if (type.equals("veggie")) {
pizza=new VeggiePizza();
}
return pizza;
}
}


class diagram
[IMG]http://i45.tinypic.com/11kifmb.jpg[/IMG]

Franchising the pizza store
As the franchiser, you want to ensure the quality of the franchise operations. Each franchise might want to offer different styles of pizzas(NY, Chicago) depending on where the franchise store is located and the tastes of the local pizza connoisseurs


NyPizzaFactory nyFactory=new NYPizzaFactory();
PizzaStore nyStore=new PizzaStore(nyFactory);
nyStore.order("Veggie");


So you test marketed the SimpleFactory idea, and what you found was that the franchise were using your factory to create pizzas. Rethinking the problem a bit, you see that what you'd really like to do is create a framework that ties the store and the pizza creation together, yet still allows things to remain flexible.

[IMG]http://i46.tinypic.com/qrda29.jpg[/IMG]

allowing the subclasses to decide
[IMG]http://i45.tinypic.com/2ywzthh.jpg[/IMG]

Let's make a PizzaStore


public class NYPizzaStore extends PizzaStore {
Pizza createPizza(String item) {
if (item.equals("cheese")) {
return new NYStyleCheesePizza();
} else if (item.equals("veggie")) {
return new NYStyleVeggiePizza();
} else return null;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值