HeadFirst设计模式之工厂模式
一、
1."工厂模式"不是种真正的设计模式,而是一种编程术语
2.The Factory Method Pattern defi nes an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
3.The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
二、简单工厂
1.根据情况,new 出来的对象是不同类型的子类,所以会变化是初始化部分,所以根据OO原则,把初始化操作抽取出来封装
2.把产生的不同类型对象的if elseif else new xxx(),抽离出来有什么好处?答:(1)可以为不同的客户端提供相同的初始化机制(2)
3.
4.
package headfirst.designpatterns.factory.pizzas; import java.util.ArrayList; abstract public class Pizza {
String name;
String dough;
String sauce;
ArrayList<String> toppings = 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() {
// code to display pizza name and ingredients
StringBuffer display = new StringBuffer();
display.append("---- " + name + " ----\n");
display.append(dough + "\n");
display.append(sauce + "\n");
for (String topping : toppings) {
display.append(topping + "\n");
}
return display.toString();
}
}
5.
package headfirst.designpatterns.factory.pizzas; public class CheesePizza extends Pizza {
public CheesePizza() {
name = "Cheese Pizza";
dough = "Regular Crust";
sauce = "Marinara Pizza Sauce";
toppings.add("Fresh Mozzarella");
toppings.add("Parmesan");
}
}
6.
package headfirst.designpatterns.factory.pizzas; 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;
} }
7.
package headfirst.designpatterns.factory.pizzas; 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;
}
}
三、工厂方法模式
1.上面简单工厂的创建pizza是绑定在createPizza()里的,不灵活,可以把createPizza()改为抽象方法,由不同如ChicagoPizzaStore、NYPizzaStore去实现,则可以解耦,实现运行时绑定
The following guidelines can help you avoid OO designs that violate the Dependency Inversion Principle:
No variable should hold a reference to a concrete class.
No class should derive from a concrete class.
No method should override an implemented method of any of its base classes.--》If you override an implemented method,then your base class wasn’t really an abstraction to start with. Those methods implemented in the base class are
meant to be shared by all your subclasses.
2.
3.
4.
5.抽象工厂其实就是在两个维度上抽象(1)在产品上(2)在creator上,注意在creator中有抽象了一个factoryMehtod(),如下图
6.
package headfirst.designpatterns.factory.pizzafm; public abstract class PizzaStore { abstract Pizza createPizza(String item); public Pizza orderPizza(String type) {
Pizza pizza = createPizza(type);
System.out.println("--- Making a " + pizza.getName() + " ---");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
7.
package headfirst.designpatterns.factory.pizzafm; public class ChicagoPizzaStore extends PizzaStore { Pizza createPizza(String item) {
if (item.equals("cheese")) {
return new ChicagoStyleCheesePizza();
} else if (item.equals("veggie")) {
return new ChicagoStyleVeggiePizza();
} else if (item.equals("clam")) {
return new ChicagoStyleClamPizza();
} else if (item.equals("pepperoni")) {
return new ChicagoStylePepperoniPizza();
} else return null;
}
}
8.
package headfirst.designpatterns.factory.pizzafm; 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;
}
}
9.
package headfirst.designpatterns.factory.pizzafm; 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");
}
}
10.
package headfirst.designpatterns.factory.pizzafm; 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");
}
}
四、抽象工厂
1.
2.
3.
package headfirst.designpatterns.factory.pizzaaf; public abstract class PizzaStore { protected abstract Pizza createPizza(String item); public Pizza orderPizza(String type) {
Pizza pizza = createPizza(type);
System.out.println("--- Making a " + pizza.getName() + " ---");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
4.
package headfirst.designpatterns.factory.pizzaaf; public class ChicagoPizzaIngredientFactory
implements PizzaIngredientFactory
{ public Dough createDough() {
return new ThickCrustDough();
} public Sauce createSauce() {
return new PlumTomatoSauce();
} public Cheese createCheese() {
return new MozzarellaCheese();
} public Veggies[] createVeggies() {
Veggies veggies[] = { new BlackOlives(),
new Spinach(),
new Eggplant() };
return veggies;
} public Pepperoni createPepperoni() {
return new SlicedPepperoni();
} public Clams createClam() {
return new FrozenClams();
}
}
5.
package headfirst.designpatterns.factory.pizzaaf; public abstract class Pizza {
String name; Dough dough;
Sauce 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;
} String getName() {
return name;
} public String toString() {
StringBuffer result = new StringBuffer();
result.append("---- " + name + " ----\n");
if (dough != null) {
result.append(dough);
result.append("\n");
}
if (sauce != null) {
result.append(sauce);
result.append("\n");
}
if (cheese != null) {
result.append(cheese);
result.append("\n");
}
if (veggies != null) {
for (int i = 0; i < veggies.length; i++) {
result.append(veggies[i]);
if (i < veggies.length-1) {
result.append(", ");
}
}
result.append("\n");
}
if (clam != null) {
result.append(clam);
result.append("\n");
}
if (pepperoni != null) {
result.append(pepperoni);
result.append("\n");
}
return result.toString();
}
}
6.
package headfirst.designpatterns.factory.pizzaaf; public interface Dough {
public String toString();
}
7.
package headfirst.designpatterns.factory.pizzaaf; public class ThickCrustDough implements Dough {
public String toString() {
return "ThickCrust style extra thick crust dough";
}
}
8.
package headfirst.designpatterns.factory.pizzaaf; 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 + "\n"); pizza = chicagoStore.orderPizza("cheese");
System.out.println("Joel ordered a " + pizza + "\n"); pizza = nyStore.orderPizza("clam");
System.out.println("Ethan ordered a " + pizza + "\n"); pizza = chicagoStore.orderPizza("clam");
System.out.println("Joel ordered a " + pizza + "\n"); pizza = nyStore.orderPizza("pepperoni");
System.out.println("Ethan ordered a " + pizza + "\n"); pizza = chicagoStore.orderPizza("pepperoni");
System.out.println("Joel ordered a " + pizza + "\n"); pizza = nyStore.orderPizza("veggie");
System.out.println("Ethan ordered a " + pizza + "\n"); pizza = chicagoStore.orderPizza("veggie");
System.out.println("Joel ordered a " + pizza + "\n");
}
}
9.
五、工厂方法与抽象工厂的对比
1.
2.
3.
HeadFirst设计模式之工厂模式的更多相关文章
- HeadFirst 设计模式 04 工厂模式
除了 new 操作符之外, 还有更多创造对象的方法. 工厂处理创建对象的细节. 这么做的目的是为了抽象, 例如把创建比萨的代码包装进一个类, 当以后实现改变时, 只需修改这个类即可. 利用静态方法定义 ...
- 设计模式——抽象工厂模式及java实现
设计模式--抽象工厂模式及java实现 设计模式在大型软件工程中很重要,软件工程中采用了优秀的设计模式有利于代码维护,方便日后更改和添加功能. 设计模式有很多,而且也随着时间在不断增多,其中最著名的是 ...
- 5. 星际争霸之php设计模式--抽象工厂模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- 3. 星际争霸之php设计模式--简单工厂模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- iOS 设计模式之工厂模式
iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...
- 设计模式之工厂模式(Factory)
设计模式的工厂模式一共有三种:简单工厂模式,工厂模式,抽象工厂模式 简单工厂模式原理:只有一个工厂类,通过传参的形式确定所创建的产品对象种类 代码如下: #include <stdio.h> ...
- php设计模式:工厂模式
php设计模式:工厂模式 意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类. 工厂模式实现: 工厂模式中任何创建对象的工厂类都要实现这个接口,实现接口的方法体中都要实现接口中的方法,它声明 ...
- 浅析JAVA设计模式之工厂模式(一)
1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...
- java 设计模式之工厂模式与反射的结合
工厂模式: /** * @author Rollen-Holt 设计模式之 工厂模式 */ interface fruit{ public abstract void eat(); } ...
随机推荐
- JavaScript高级程序开发3笔记
Js对象 注意:js基本数据类型不是对象,但是"abc".match()这种,可以调用对象的方法,是因为调用方法是临时产生了一个wrapper的包装对象,this指向它: Js ...
- echarts标准饼图解读(一)——提示框(tooltip)配置
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Apache使用mysql认证用户
使用MySQL进行认证 第1步:下载MySQL认证模块,并更名为mod_auth_mysql.so文件,并保存在apache的modules目录下 第2步:apache要加载此功能模块 LoadMod ...
- @Autowired与@Resource用法
官方文档中有这样一段话. If you intend to express annotation-driven injection by name, do not primarily use @Aut ...
- myeclipse 的 working set
想必大家的myeclipse会有很多工程看的和不方便,那么怎么让它看的简洁一点呢,使用working set 会让你的目录看起来没有那么的多. 首先是怎么创建 working set ,在新建时选择 ...
- OpenCV3读取、写入和保存图像
需要说明的是在OpenCV3中已经将imread()和imwrite()函数转移到imgcodecs模块中,因此读写图像时,需要包含imgcodecs.hpp头文件,但是highgui.hpp头文件中 ...
- 英文版firefox显示中文字体丑的问题
在Options里面选择Content,在Fonts&Colors区域的Default font中,选择Times New Roman 如下图1: 在旁边的Advanced中选择,Fonts ...
- asp.net导出Excel 按照预定格式,以及解决导出乱码
protected void ToExcel() { //新建一个Gridview,原因:避免当前窗口GridView外层没有直接跟form标签,从而避免“gridview1未包含在run='serv ...
- Excel数据复制到Winform控件ListView
先给窗体添加一个右键菜单contextMenuStrip 加一个下拉项[粘贴] 粘贴事件: private void tsmiPaste_Click(object sender, EventArgs ...
- C#使用Zxing2.0生成二维码 带简单中心LOGO
参考:http://www.open-open.com/lib/view/open1379214678162.html 代码:http://files.cnblogs.com/halo/%E4%BA% ...