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(); } ...
随机推荐
- css笔记——css 实现自定义按钮
css实现自定义按钮的样式实际上很早就有了,只是会用的人不是很多,里面涉及到了最基础的css写法,在火狐中按钮还是会显示出来,这时需要将i标签的背景设置为白色,同时z-index设置比input高一些 ...
- JAVA:避免重复的创建对象
在实际开发中,重复使用同一个对象要比每次需要的时候就创建一个对象要好的多: 作为一个比较极端的反面例子,看下面这个语句: String s = new String("haha") ...
- 再说 extern "C"
早知道 C++ 源文件中要调用C语言函数需要在函数申明时 指定extern "C": 要不然可以编译通过,但连接时提示找不到什么什么符号,原因是C和C++生成的函数名不一样,ext ...
- vim 跳转命令
基本跳转: hjkl:左下上右 HML:当前屏幕顶.中.底部 web:下一单词词首.下一单词词尾.前一单词词首 gg:文件首 G:文件末尾 ngg/nG:第n行 ta:移动到所在行之后第一个字符a ...
- 一些值得思考的"小题"一
如下是我们查找数组中某个元素的一种通常做法 const int *Find(const int *array, int length, int x) { const int *p = array; ; ...
- [DevExpress]GridControl 列头绘制Checkbox
关键代码: /// <summary> /// 为列头绘制CheckBox /// </summary> /// <param name="view" ...
- (转)[Erlang 0080] RabbitMQ :VHost,Exchanges, Queues,Bindings and Channels
和RabbitMQ这个项目的缘分好奇怪,很长一段时间内是只关注源代码,真的是Erlang开源项目中的典范;现在要在项目中应用RabbitMQ,从新的视角切入,全新的感觉.仿佛旧情人换了新衣,虽是熟稔却 ...
- 可变参数列表---以dbg()为例
在UART驱动的drivers/serial/samsung.h中遇到如下定义: #ifdef CONFIG_SERIAL_SAMSUNG_DEBUG extern void printascii(c ...
- Ping的过程详解
先看拓朴图: 我在这里讲拼的两情况,一种是同一网段内,一种是跨网段的ping …. 首先,如果主机A,要去拼主机B,那么主机A,就要封装二层报文,他会先查自己的MAC地址表,如果没有B的MAC地址,就 ...
- 手把手教你写LKM rookit! 之 第一个lkm程序及模块隐藏(一)
唉,一开始在纠结起个什么名字,感觉名字常常的很装逼,于是起了个这<手把手教你写LKM rookit> 我觉得: 你们觉得:...... 开始之前,我们先来理解一句话:一切的操作都是系统调用 ...