当前位置 : 主页 > 编程语言 > java >

Java设计模式:简单工厂模式(Simple Factory Pattern)

来源:互联网 收集:自由互联 发布时间:2022-09-02
简单工厂模式: 可以使用静态方法,因为这样它可以不需要创建对象,但缺点是:不能通过继承来改变创建方法的行为。 简单工厂其实不是一个设计模式,反而更像一种编程习惯。 事


简单工厂模式:

可以使用静态方法,因为这样它可以不需要创建对象,但缺点是:不能通过继承来改变创建方法的行为。

简单工厂其实不是一个设计模式,反而更像一种编程习惯。

事例:

一个披萨店,会生产各种类型的披萨,如蔬菜类、蛤蜊类、奶酪类、意大利辣肠类等。

步骤:

1)选择一种类型的披萨

2)准备、烧烤、切片、包装 

UML类图

Java设计模式:简单工厂模式(Simple Factory Pattern)_Java简单工厂模式

遵循的设计原则:

对修改关闭,对扩展开放

Pizza.java 披萨抽象超类

package com.jing.factory.simple;

import java.util.ArrayList;

public abstract class Pizza {

String name; // 名字
String dough;// 面包
String sauce;// 酱料

// 佐料
ArrayList<String> topping = new ArrayList<String>();

public String getName() {
return name;
}

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

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

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

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

public String toString() {
StringBuffer display = new StringBuffer();
display.append("---- " + name + " ---------\n");
display.append(dough + "\n");
display.append(sauce + "\n");

for (int i = 0; i < topping.size(); ++i) {
display.append((String) topping.get(i) + "\n");
}

return display.toString();
}

}

CheesePizza.java 奶酷披萨

package com.jing.factory.simple;

public class CheesePizza extends Pizza {

public CheesePizza() {
name = "Cheese Pizza"; // 奶酪 披萨
dough = "Regular Crust"; // 普通的披萨饼皮
sauce = "Marinara Pizza Sauce"; // 蕃茄酱,沙司

topping.add("Fresh Mozzarella");// 新鲜的马苏里拉奶酪
topping.add("Parmesan");// 芝士
}
}

ClamPizza.java 蛤蜊披萨


package com.jing.factory.simple;

public class ClamPizza extends Pizza {

public ClamPizza(){
name = "Clam Pizza";//蛤蜊
dough = "Thin crust";// 薄的面皮
sauce = "White garlic sauce";//白的蒜酱

topping.add("Clams");
topping.add("Grated Parmesan cheese");// 巴马干酪
}
}

VeggiePizza.java 蔬菜披萨

package com.jing.factory.simple;

public class VeggiePizza extends Pizza {

public VeggiePizza(){
name = "Veggie Pizza"; // 蔬菜
dough = "Crust";
sauce = "Marinara sauce";// 蕃茄酱

topping.add("Shredded mozzaredlla");// 意大利干酪
topping.add("Grated parmesan");// 芝士
topping.add("Diced onion");// 切丁洋葱
topping.add("Sliced mushrooms");//切片蘑菇
topping.add("Sliced red pepper");//切片红花椒
topping.add("Sliced black olives");//橄榄

}

}

PepperoniPizza.java 意大利辣肠披萨

package com.jing.factory.simple;

public class PepperoniPizza extends Pizza {

public PepperoniPizza(){
name = "Pepperoni Pizza";//意大利辣肠
dough = "Curst";// 面皮
sauce = "Marinara sauce";// 蕃茄酱

topping.add("Sliced Pepperoni");// 切片辣肠
topping.add("Sliced Onion"); // 切片洋葱
topping.add("Grated parmesan cheese");//芝士奶酪
}

}

SimplePizzaFactory.java 简单披萨工厂,用于生产各种披萨,它是我们唯一用来披萨类的地方

package com.jing.factory.simple;

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

return pizza;
}
}

PizzaStore.java 披萨店,拥有一个生产工厂,及确定种类后的生产流程

package com.jing.factory.simple;

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

Test.java 测试类

package com.jing.factory.simple;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

// 生成一个披萨的生产工厂
SimplePizzaFactory factory = new SimplePizzaFactory();

// 生成一个披萨店,它保存生产工厂
PizzaStore store = new PizzaStore(factory);

// 生产一个奶酪披萨
Pizza pizza = store.orderPizza("cheese");
System.out.println("we ordered a " + pizza.getName() + "\n");

// 生产一个蔬菜披萨
pizza = store.orderPizza("veggie");
System.out.println("we ordered a " + pizza.getName() + "\n");
}
}

输出:

Preparing Cheese Pizza
Baking Cheese Pizza
Cutting Cheese Pizza
Boxing Cheese Pizza
we ordered a Cheese Pizza

Preparing Veggie Pizza
Baking Veggie Pizza
Cutting Veggie Pizza
Boxing Veggie Pizza
we ordered a Veggie Pizza


网友评论