设计模式-工厂模式

来源:互联网 发布:百旺金赋税控软件 编辑:程序博客网 时间:2024/05/22 15:20

1.简单工厂


开披萨店:

public abstract class PizzaStore

{

public Pizza orderPizza(String type)

{

Pizza pizza;

pizza = createPizza(type);


pizza.prepare();

pizza.bake();

pizza.cut();

pizza.box();

return pizza;

}

abstract Pizza createPizza(String type);

}


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 if(item.equals("clam"))

{

return new NYStyleClamPizza();

}

else if(item.equals("pepperoni"))

{

return new NYStylePepperoniPizza();

}

else return null;

}

}


披萨:

public abstract class Pizza

{

String name;

String dough;

String sauce;

ArrayList toppings = new ArrayList ();

void prepare()

{

System.out.println("Preparing " + name);

System.out.println("Tossing dough... ");

System.out.println("Adding sauce... ");

System.out.println("Adding toppings: " );

for(int i = 0;i < toppings.size();i++)

{

System.out.println("        " + toppings.get(i));

}

}

void bake()

{

System.out.println("Bake for 25 minutes at 350");

}

void cut()

{

System.out.println("Cutting the pizza into diagonal slices");

}

void box()

{

System.out.println("Place pizza in official PizzaStore box");

}

public String getName()

{

return name;

}

}

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");

}

}

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 Mozzarella Cheese");

}

void cut()

{

System.out.println("Cutting the pizza into square slices");

}

}

吃披萨,测试:

public class PizzaTestDrive

{

public static void main(String[] args)

{

PizzaStore nyStore = new NYPizzaStore();

PizzaStore chicagoStore = new ChicagoPizzaStore();

Pizza pizza = nyStore.orderPizza("cheese");

System.out.println("Ethan ordered a " + pizza.getName() + "\n");

pizza = chicagoStore.orderPizza("cheese");

System.out.println("Joel ordered a " + pizza.getName() + "\n");

}

}


2.抽象工厂


建造原料工厂:

public interface PizzaIngredientFactory

{

public Dough createDough();

public Sauce createSauce();

public Cheese createCheese();

public Veggies[] createVeggies();

public Pepperoni createPepperoni();

public Clams createClam();

}

public class NYPizzaIngredientFactory implements PizzaIngredientFactory

{

public Dough createDough()

{

return new ThinCrustDough();

}

public Sauce createSauce()

{

return new MsrinareSauce();

}

public Cheese createCheese()

{

return new ReggianoCheese();

}

public Veggies[] createVeggies()

{

Veggies veggies[] = { new Garlic(),new Onion(),new Mushroom(),new RedPepper()};

return veggies;

}

public Pepperoni createPepperoni()

{

return new SlicedPepperoni();

}

public Clams createClam()

{

return new FreshClams();

}

}

重做披萨:

public abstract class Pizza

{

String name;

String dough;

String sauce;

Veggies veggies[];

Cheese cheese;

Pepperoni pepperoni;

Clams clam;


abstract void prepare();


void bake()

{

System.out.println("Bake for 25 minutes at 350");

}

void cut()

{

System.out.println("Cutting the pizza into diagonal slices");

}

void box()

{

System.out.println("Place pizza in official PizzaStore box");

}

void setName(String name)

{

this.name = name;

}

public String getName()

{

return name;

}

public String toString()

{

//这里是打印披萨的代码

}

}

public class CheesePizza extends Pizza

{

PizzaIngredientFactory ingredientFactory;

public CheesePizza (PizzaIngredientFactory ingredientFactory)

{

this.ingredientFactory = ingredientFactory;

}

void prepare()

{

System.out.println("Preparing " + name);

dough = ingredientFactory.createDough();

sauce = ingredientFactory.createSauce();

cheese = ingredientFactory.createCheese();

}

}

public class ClamPizza extends Pizza

{

PizzaIngredientFactory ingredientFactory;

public CheesePizza (PizzaIngredientFactory ingredientFactory)

{

this.ingredientFactory = ingredientFactory;

}

void prepare()

{

System.out.println("Preparing " + name);

dough = ingredientFactory.createDough();

sauce = ingredientFactory.createSauce();

cheese = ingredientFactory.createCheese();

clam = ingredientFactory.createClam();

}

}

重做披萨店:

public class NYPizzaStore extends PizzaStore

{

protected Pizza createPizza(String item)

{

Pizza pizza = null;

PizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory();

if(item.equals("cheese"))

{

pizza = new CheesePizza(ingredientFactory );

pizza.setName("New York Style Cheese Pizza");

}

else if(item.equals("veggie"))

{

pizza = new VeggiePizza(ingredientFactory );

pizza.setName("New York Style VeggiePizza");

}

else if(item.equals("clam"))

{

pizza = new ClamPizza(ingredientFactory );

pizza.setName("New York Style Clam Pizza");

}

else if(item.equals("pepperoni"))

{

pizza = new PepperoniPizza(ingredientFactory );

pizza.setName("New York Style Pepperoni Pizza");

}

else return pizza;

}

}

3.比较简单工厂和抽象工厂



0 0