设计模式 - 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,用以记录当前 ...
随机推荐
- bootstrap css选择不同的宽度
刚开始使用bootstrap css开源项目.遇到一个问题,默认的input 宽度太大,需要找小一点的. 其实只需要在input tag中选用预定义的较小的宽度即可.比如: <input typ ...
- NYOJ 914 Yougth的最大化
Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 Yougth如今有n个物品的重量和价值各自是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价 ...
- Storyboard 经常用法总结-精华版
1.prepareForSegue: Now we know what the destinationViewController is we can set its data properties. ...
- 数组去重Array
var aee3=[31,42,13,19,5,11,8,13,40,39,1,8,44,15,3]; Array.prototype.unqu2=function(){ this.sort(); v ...
- 开源工具DbUtils的使用(数据库的增删改查)
开源工具DbUtils的使用(数据库的增删改查) 一.DbUtils简介: DBUtils是apache下的一个小巧的JDBC轻量级封装的工具包,其最核心的特性是结果集的封装,可以直接将查询出来的结果 ...
- poj 1220 NUMBER BASE CONVERSION(短除法进制转换)
题目连接:1220 NUMBER BASE CONVERSION 题目大意:给出两个进制oldBase 和newBase, 以及以oldBase进制存在的数.要求将这个oldBase进制的数转换成ne ...
- Linux从用户层到内核层系列 - GNU系列之glibc介绍
题记:本系列文章的目的是抛开书本从源代码和使用的角度分析Linux内核和相关源代码,byhankswang和你一起玩转linux开发 轻松搞定TCP/IP协议栈,原创文章欢迎交流, byhankswa ...
- poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS (母函数)
/* 给出一个数n,把它拆分成若干个数的和,要求最大的数在中间并向两边非递增.问拆法有多少种. 母函数.枚举中间的那一个数.由于左右对称.所以仅仅须要求左边部分的方案就可以. 注意,左右两部分的取数必 ...
- If you pay peanuts,you get monkeys
英文原文:Before you send an email to contact a web developer, please read this… 做为一名开发者,我收到很多关于开发新 web 应 ...
- 安装dotnet core
CentOS 7.1下安装dotnet core .NET CORE的官方(http://dotnet.github.io/getting-started/)只提供了Windows, Ubuntu14 ...