let's say you have a pizza shop. you might end up writing some code like this:
Pizza orderPizza() {
Pizza pizza = new Pizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
When you need more than one type of pizza
Pizza orderPizza(String type) {
Pizza pizza;
if (type.equals("cheese")) {
pizza = new CheesePizza();
} else if (type.equals("greek")) {
pizza = new GreekPizza();
} else if (type.equals("peoperoni")) {
pizza = new PepperoniPizza();
}
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
Clearly, dealing with which concrete class is instantiated is really messing up our orderPizza() method and preventing it from being closed for modification
What should we do then?
Well, what we're going to do is take the creation code and move it out into another object that is only going to be concerned with creating pizzas.
We've got a name for this new object: we call it a factory.
Reworking the pizzastore class
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;
}
}
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("veggie")) {
pizza=new VeggiePizza();
}
return pizza;
}
}
class diagram
Franchising the pizza store
As the franchiser, you want to ensure the quality of the franchise operations. Each franchise might want to offer different styles of pizzas(NY, Chicago) depending on where the franchise store is located and the tastes of the local pizza connoisseurs
NyPizzaFactory nyFactory=new NYPizzaFactory();
PizzaStore nyStore=new PizzaStore(nyFactory);
nyStore.order("Veggie");
So you test marketed the SimpleFactory idea, and what you found was that the franchise were using your factory to create pizzas. Rethinking the problem a bit, you see that what you'd really like to do is create a framework that ties the store and the pizza creation together, yet still allows things to remain flexible.
allowing the subclasses to decide
Let's make a PizzaStore
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 return null;
}
}
分享到:
相关推荐
3、抽象工厂模式(Abstract Factory Pattern) 用意:一个工厂生产一系列产品
Head.First设计模式中的The Factory Pattern(工厂模式)ppt
Interfaces + Factory pattern = Decoupled architecture
简单工厂(Simple Factory)模式又称为静态工厂方法(Static Factory Method)模式,属于类的创建型模式,通常它根据自变量的不同返回不同的类的实例。...我已经用C#的源码编程了:请下载Simple Factory Pattern.rar
前端大厂最新面试题-Factory Pattern.docx
抽象工厂模式(Abstract Factory Pattern) 引入: 在前面介绍的两个创建型模式里面,我们解决的都是有关"new"的问题,用它们来避免显式指定类创建对象。我写的也非常简单易懂,相信看过的朋友们都应该对简单工厂...
Head First 设计模式 (四) 工厂模式(factory pattern) C++实现
java 创建型模式--工厂模式(Factory Pattern)示范代码demo,关于工厂模式可以查看我的博客 http://blog.csdn.net/u012930316/article/details/54599580
工厂设计模式,非常详细的设计模式文档,欢迎大家下载
一个C++工厂模式的例子,构造函数私有……
NULL 博文链接:https://lym6520.iteye.com/blog/701742
NULL 博文链接:https://jacky-dai.iteye.com/blog/2294531
今天小编就为大家分享一篇关于PHP设计模式之工厂模式(Factory Pattern)的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
计算器 使用Command Factory Pattern和依赖项注入的Java控制台应用程序
Factory Pattern
5、工厂方法模式FACTORY METHOD PATTERN 6、抽象工厂模式ABSTRACT FACTORY PATTERN 7、门面模式FACADE PATTERN 8、适配器模式ADAPTER PATTERN 9、模板方法模式TEMPLATE METHOD PATTERN 10、建造者模式BUILDER ...
备忘录模式(Memento Pattern) 策略模式(Strategy Pattern) 抽象工厂模式(Abstract Factory Pattern) 代理模式(Proxy Pattern) 单例模式(Singleton Pattern) 迭代器模式(Iterator Pattern) 访问者模式(Visitor ...
Factory Method Pattern 工厂三兄弟之工厂方法模式(一) 工厂三兄弟之工厂方法模式(二) 工厂三兄弟之工厂方法模式(三) 工厂三兄弟之工厂方法模式(四) 抽象工厂模式-Abstract Factory Pattern 工厂三兄弟之...
工厂模式(Factory Pattern) 抽象工厂模式(Abstract Factory Pattern) 单例模式(Singleton Pattern) 建造者模式(Builder Pattern) 原型模式(Prototype Pattern) 2. 结构型模式 适配器模式(Adapter ...
C#设计模式(6)-Abstract Factory Pattern C#设计模式(5)-Factory Method Pattern C#设计模式(4)-Simple Factory Pattern C#设计模式(3) - 设计原则(2) C#设计模式(2) - 设计原则(1) C#设计模式(1) - ...