设计模式 - Abstract Factory模式(abstract factory pattern) 详细说明
Abstract Factory模式(abstract factory pattern) 详细说明
本文地址: http://blog.csdn.net/caroline_wendy/article/details/27091671
參考工厂模式: http://blog.csdn.net/caroline_wendy/article/details/27081511
抽象工厂模式: 提供一个接口, 用于创建相关或依赖对象的家族, 而不须要明白指定详细类.
所有代码: http://download.csdn.net/detail/u012515223/7403553
详细方法:
1. 提供一个抽象工厂(abstract factory)接口(interface)类, 不同的详细工厂(concrete factory)继承此类.
代码:
/**
* @time 2014年5月26日
*/
package factory; /**
* @author C.L.Wang
*
*/
public interface PizzaIngredientFactory {
public Dough createDough();
public Sauce createSauce();
public Cheese createCheese();
public Veggies[] createVeggies();
public Pepperoni createPepperoni();
public Clams createClam();
}
2. 详细工厂(concrete factory), 实现抽象工厂(abstract factory)接口(interface).
代码:
/**
* @time 2014年5月26日
*/
package factory; /**
* @author C.L.Wang
*
*/
public class NYPizzaIngredientFactory implements PizzaIngredientFactory { /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createDough()
*/
@Override
public Dough createDough() {
// TODO Auto-generated method stub
return new ThinCrustDough();
} /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createSauce()
*/
@Override
public Sauce createSauce() {
// TODO Auto-generated method stub
return new MarinaraSauce();
} /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createCheese()
*/
@Override
public Cheese createCheese() {
// TODO Auto-generated method stub
return new ReggianoCheese();
} /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createVeggies()
*/
@Override
public Veggies[] createVeggies() {
// TODO Auto-generated method stub
Veggies veggies[] = {new Garlic(), new Onion(), new Mushroom(), new RedPepper()};
return veggies;
} /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createPepperoni()
*/
@Override
public Pepperoni createPepperoni() {
// TODO Auto-generated method stub
return new SlicedPepperoni();
} /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createClam()
*/
@Override
public Clams createClam() {
// TODO Auto-generated method stub
return new FreshClams();
} } /**
* @time 2014年5月26日
*/
package factory; /**
* @author C.L.Wang
*
*/
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory { /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createDough()
*/
@Override
public Dough createDough() {
// TODO Auto-generated method stub
return new ThickCrustDough();
} /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createSauce()
*/
@Override
public Sauce createSauce() {
// TODO Auto-generated method stub
return new PlumTomatoSauce();
} /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createCheese()
*/
@Override
public Cheese createCheese() {
// TODO Auto-generated method stub
return new MozzarellaCheese();
} /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createVeggies()
*/
@Override
public Veggies[] createVeggies() {
// TODO Auto-generated method stub
Veggies veggies[] = {new BlackOlives(), new Spinach(), new Eggplant()};
return veggies;
} /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createPepperoni()
*/
@Override
public Pepperoni createPepperoni() {
// TODO Auto-generated method stub
return new SlicedPepperoni();
} /* (non-Javadoc)
* @see factory.PizzaIngredientFactory#createClam()
*/
@Override
public Clams createClam() {
// TODO Auto-generated method stub
return new FrozenClams();
} }
3. 产品抽象(abstract)父类, 提供接口, 供详细产品(concrete product)调用.
代码:
/**
* @time 2014年5月26日
*/
package factory; /**
* @author C.L.Wang
*
*/
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;
} public String getName() {
return name;
}
}
4. 详细产品(concrete product)继承产品抽象(abstract)父类, 使用工厂类实现继承接口, 通过不同的工厂生产不同的产品;
代码:
/**
* @time 2014年5月26日
*/
package factory; /**
* @author C.L.Wang
*
*/
public class CheesePizza extends Pizza { PizzaIngredientFactory pizzaIngredientFactory; public CheesePizza(PizzaIngredientFactory pizzaIngredientFactory) {
this.pizzaIngredientFactory = pizzaIngredientFactory;
} /* (non-Javadoc)
* @see factory.Pizza#prepare()
*/
@Override
void prepare() {
// TODO Auto-generated method stub
System.out.println("Preparing " + name);
dough = pizzaIngredientFactory.createDough();
sauce = pizzaIngredientFactory.createSauce();
cheese = pizzaIngredientFactory.createCheese();
System.out.println("Ingredient : " + dough + ", " + sauce + ", " + cheese);
} } /**
* @time 2014年5月26日
*/
package factory; /**
* @author C.L.Wang
*
*/
public class ClamPizza extends Pizza { PizzaIngredientFactory pizzaIngredientFactory; public ClamPizza(PizzaIngredientFactory pizzaIngredientFactory) {
this.pizzaIngredientFactory = pizzaIngredientFactory;
} /* (non-Javadoc)
* @see factory.Pizza#prepare()
*/
@Override
void prepare() {
// TODO Auto-generated method stub
System.out.println("Preparing " + name);
dough = pizzaIngredientFactory.createDough();
sauce = pizzaIngredientFactory.createSauce();
cheese = pizzaIngredientFactory.createCheese();
clam = pizzaIngredientFactory.createClam();
System.out.println("Ingredient : " + dough + ", "
+ sauce + ", " + cheese + ", " + clam);
} }
5. 详细调用函数, 通过传递不同的參数, 调用不同的工厂, 生产出不同的产品.
代码:
/**
* @time 2014年5月26日
*/
package factory; /**
* @author C.L.Wang
*
*/
public class NYPizzaStore extends PizzaStore { /* (non-Javadoc)
* @see factory.PizzaStore#createPizza(java.lang.String)
*/
@Override
Pizza createPizza(String item) {
// TODO Auto-generated method stub
Pizza pizza = null;
PizzaIngredientFactory pizzaIngredientFactory = new NYPizzaIngredientFactory();
if (item.equals("cheese")) {
pizza = new CheesePizza(pizzaIngredientFactory);
pizza.setName("New York Style Cheese Pizza");
} else if (item.equals("clam")) {
pizza = new ClamPizza(pizzaIngredientFactory);
pizza.setName("New York Style Clam Pizza");
} return pizza;
} } /**
* @time 2014年5月26日
*/
package factory; /**
* @author C.L.Wang
*
*/
public class ChicagoPizzaStore extends PizzaStore { /* (non-Javadoc)
* @see factory.PizzaStore#createPizza(java.lang.String)
*/
@Override
Pizza createPizza(String item) {
// TODO Auto-generated method stub
Pizza pizza = null;
PizzaIngredientFactory pizzaIngredientFactory = new ChicagoPizzaIngredientFactory();
if (item.equals("cheese")) {
pizza = new CheesePizza(pizzaIngredientFactory);
pizza.setName("Chicago Style Cheese Pizza");
} else if (item.equals("clam")) {
pizza = new ClamPizza(pizzaIngredientFactory);
pizza.setName("Chicago Style Clam Pizza");
} return pizza;
} }
6. 測试函数
代码:
/**
* @time 2014年5月26日
*/
package factory; /**
* @author C.L.Wang
*
*/
public class PizzaTestDrive { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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"); pizza = nyStore.orderPizza("clam");
System.out.println("Ethan ordered a " + pizza.getName() + "\n"); pizza = chicagoStore.orderPizza("clam");
System.out.println("Joel ordered a " + pizza.getName() + "\n");
} }
7. 输出:
Preparing New York Style Cheese Pizza
Ingredient : Thin Crust Dough, Marinara Sauce, Reggiano Cheese
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Ethan ordered a New York Style Cheese Pizza Preparing Chicago Style Cheese Pizza
Ingredient : ThickCrust style extra thick crust dough, Tomato sauce with plum tomatoes, Shredded Mozzarella
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Joel ordered a Chicago Style Cheese Pizza Preparing New York Style Clam Pizza
Ingredient : Thin Crust Dough, Marinara Sauce, Reggiano Cheese, Fresh Clams from Long Island Sound
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Ethan ordered a New York Style Clam Pizza Preparing Chicago Style Clam Pizza
Ingredient : ThickCrust style extra thick crust dough, Tomato sauce with plum tomatoes,...
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Joel ordered a Chicago Style Clam Pizza
版权声明:本文博主原创文章。博客,未经同意不得转载。
设计模式 - Abstract Factory模式(abstract factory pattern) 详细说明的更多相关文章
- Java设计模式之工厂模式(Factory模式)介绍(转载)
原文见:http://www.jb51.net/article/62068.htm 这篇文章主要介绍了Java设计模式之工厂模式(Factory模式)介绍,本文讲解了为何使用工厂模式.工厂方法.抽象工 ...
- C#设计模式总结 C#设计模式(22)——访问者模式(Vistor Pattern) C#设计模式总结 .NET Core launch.json 简介 利用Bootstrap Paginator插件和knockout.js完成分页功能 图片在线裁剪和图片上传总结 循序渐进学.Net Core Web Api开发系列【2】:利用Swagger调试WebApi
C#设计模式总结 一. 设计原则 使用设计模式的根本原因是适应变化,提高代码复用率,使软件更具有可维护性和可扩展性.并且,在进行设计的时候,也需要遵循以下几个原则:单一职责原则.开放封闭原则.里氏代替 ...
- C#设计模式(6)——原型模式(Prototype Pattern) C# 深浅复制 MemberwiseClone
C#设计模式(6)——原型模式(Prototype Pattern) 一.引言 在软件系统中,当创建一个类的实例的过程很昂贵或很复杂,并且我们需要创建多个这样类的实例时,如果我们用new操作符去创 ...
- 乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)
原文:乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 模板方法模式(Template Method ...
- C#设计模式:代理模式(Proxy Pattern)
一,什么是C#设计模式? 代理模式(Proxy Pattern):为其他对象提供一种代理以控制对这个对象的访问 二,代码如下: using System; using System.Collectio ...
- C#设计模式:组合模式(Composite Pattern)
一,C#设计模式:组合模式(Composite Pattern) using System; using System.Collections.Generic; using System.Linq; ...
- 北风设计模式课程---解释器模式(Interpreter Pattern)
北风设计模式课程---解释器模式(Interpreter Pattern) 一.总结 一句话总结: 不仅要通过视频学,还要看别的博客里面的介绍,搜讲解,搜作用,搜实例 设计模式都是对生活的抽象,比如用 ...
- 设计模式学习--复合模式(Compound Pattern)
设计模式学习--复合模式(Compound Pattern) 概述 ——————————————————————————————————————————————————— 2013年8月4日<H ...
- C#设计模式:备忘录模式(Memento Pattern)
一,C#设计模式:备忘录模式(Memento Pattern) 1.发起人角色(Originator):记录当前时刻的内部状态,负责创建和恢复备忘录数据.负责创建一个备忘录Memento,用以记录当前 ...
随机推荐
- 英文版Ubuntu安装Fcitx输入法
在英文环境(LC_CTYPE=en_US.UTF-8)下安装,可按如下配置: 首先,执行 sudo apt-get install fcitx-pinyin im-switch 然后,执行 im-sw ...
- JavaScript 中的事件类型3(读书笔记思维导图)
Web 浏览器中可能发生的事件有很多类型.如前所述,不同的事件类型具有不同的信息,而“ DOM3级事件”规定了以下几类事件. UI(User Interface,用户界面)事件:当用户与页面上的元素交 ...
- RH033读书笔记(17) - Summary
End of Unit 1 • Questions and Answers • Summary • Open source and the right to modify • The GNU Proj ...
- KFC - About KFC - Quality Assurance
KFC - About KFC - Quality Assurance Restaurant Quality The main attributes for KFC restaurant excell ...
- WPF案例 (四) 模拟Windows7桌面任务栏
原文:WPF案例 (四) 模拟Windows7桌面任务栏 这个程序模彷了Windows7的桌面任务栏,当在桌面上双击某个快捷方式时,将打开一个新的子界面,并且在任务栏里创建一个链接到此界面的任务栏图标 ...
- tudou link
http://www.tudou.com/programs/view/QdOktCIUfQ0/?tid=-1&aid=-120137222&pid=41050010&oid=2 ...
- POJ3050 Hopscotch 【DFS】
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2113 Accepted: 1514 Descrip ...
- poj 3172 Scales 搜索
其实这个题目要是注意到了题目的一点关键性的描述就会变得很简单,题意是给出的砝码是至少是前两个的和的,有了这一点,那么砝码的数量应该就在几十左右,这样的话适当剪枝的搜索是应该可以过的. #include ...
- Apache James使用的方法及相关心得(转)
经过一番的辛苦努力,终于把James 配置搞定啦,好记性不如烂笔头啊,赶紧记下我的成功经过,以备以后查阅! 首先要做的就是配置域名的MX 记录啦: 先添加一条A记录: mail.abc.com 指向 ...
- [置顶] 轻量级语言Lua入门
作为一个脚本爱好者,而且是脚本(Perl)起家的我,一有空就喜欢学习下这些脚本语言.据说魔兽世界.愤怒小鸟都用到了它,所以今天研究下Lua这个叫法有点奇特的脚本 [转载请注明出处:http://blo ...