首页 > 代码库 > 工厂模式

工厂模式

工厂模式包括二种:工厂方法和抽象工厂。二者都是让子类决定该创建的对象是什么,

来达到将对象创建的过程封装的目的。但二者略有不同,工厂方法是通过继承来实现,

而抽象工厂是通过组合来实现的。

首先不考虑调料的不同的情况下,随时会有Pizza联盟店加入进来。Pizza联盟店地址不

同,当地的风俗和口味也会不一样。因此Pizza联盟店会提供相应口味的Pizza。工厂方

法模式即在抽象类PizzaStore中提供创建产品的抽象方法,然后让各个地方的Pizza店

继承抽象类PizzaStore,实现抽象方法,自己选择创建什么类型的产品。

实现图如下:

Pizza类:

public abstract class Pizza {
String name;
String dough;
String sauce;
ArrayList toppings=new ArrayList();

public void prepare()
{
System.out.println("preparing "+name);
System.out.println("Tossing dough..."+dough);
System.out.println("Adding sauce..."+sauce);
System.out.println("Adding toppings: ");
for(int i=0;i<toppings.size();i++)
{
System.out.println(" "+toppings.get(i));
}
}

public void bake()
{
System.out.println("Bake for 25 minutes at 350");
}
public void cut()
{
System.out.println("Cutting the pizza into diagonal slices");
}
public void box()
{
System.out.println("Place pizza in official PizzaStore box");
}
public String getName()
{
return name;
}

}

ChicagoStyleCheesePizza类(Pizza具体类):

public class ChicagoStyleCheesePizza extends Pizza{
public ChicagoStyleCheesePizza()
{
name="Chicago Style Deep Dish Cheese Pizza";
dough="Extra Thick Crust Dough";
sauce="Plum Tomato Sauce";

toppings.add("Shredded Mozzrella Cheese");
}

public void cut()
{
System.out.println("Cutting the pizza into square slices");
}

}

NYStyleCheesePizza类(Pizza具体类):

public class NYStyleCheesePizza extends Pizza{
public NYStyleCheesePizza()
{
name="NY Style Sauce and Cheese Pizza";
dough="Thin Crust Dough";
sauce="Marinara Sauce";

toppings.add("Grated Reggiano Cheese");
}
}

PizzaStore类(Pizza联盟店基类,所有具体联盟店均继承该基类)

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

ChicagoPizzaStore类(Pizza联盟店):

public class ChicagoPizzaStore extends PizzaStore{

@Override
public Pizza createPizza(String type) {
// TODO Auto-generated method stub
if(type.equals("cheese"))
{
return new ChicagoStyleCheesePizza();
}
else if(type.equals("clam"))
{
return new ChicagoStyleClamPizza();
}
else if(type.equals("pepperoni"))
{
return new ChicagoStylePepperoniPizza();
}
else if(type.equals("veggie"))
{
return new ChicagoStyleVeggiePizza();
}
else
{
return null;
}
}

}

NYPizzaStore类(Pizza联盟店):

public class NYPizzaStore extends PizzaStore{

@Override
public Pizza createPizza(String type) {
// TODO Auto-generated method stub
if(type.equals("cheese"))
{
return new NYStyleCheesePizza();
}
else if(type.equals("clam"))
{
return new NYStyleClamPizza();
}
else if(type.equals("pepperoni"))
{
return new NYStylePepperoniPizza();
}
else if(type.equals("veggie"))
{
return new NYStyleVeggiePizza();
}
else
{
return null;
}
}

}

测试类:

public class PizzaTestDrive {
public static void main(String [] args)
{
PizzaStore nyStore=new NYPizzaStore();
PizzaStore chicagoStore=new ChicagoPizzaStore();
Pizza pizza=nyStore.createPizza("cheese");
System.out.println("Ethan ordered a "+pizza.getName()+" \n");
pizza=chicagoStore.createPizza("cheese");
System.out.println("Joel ordered a "+pizza.getName()+" \n");
}
}