设计模式 - 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,用以记录当前 ...
随机推荐
- C语言编写的bmp读写程序
C语言编写的bmp读写程序 建议先把bmp的数据存储格式了解下 <span style="font-size:16px;">#include "Windows ...
- Uva - 11419 - SAM I AM
题意:一个矩形——R*C的网格,在某些位置上有石头,在网格外开一炮可以打掉该行或者该列的石头,求打掉这些石头最少需要多少门大炮,位置分别设在哪行哪列(0<R<1001, 0 < C ...
- 从零开始学C++之动态创建对象
回顾前面的文章,实现了一个简单工厂模式来创建不同类对象,但由于c++没有类似new "Circle"之类的语法,导致CreateShape 函 数中需要不断地ifelse地去判断, ...
- Java的byte数组
今天我们讨论一下Java的byte数组.byte数组就是字节数组,每一位存一个字节.看下面程序,猜猜输出多少? package com.wjy.bytes; import java.io.File; ...
- 【OpenCV新手教程之十三】OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/26157633 作者:毛星云(浅墨) ...
- 蓝桥杯软件学院webserver,android
- 我和小美的撸码日记(3)之中的一个句话搞定MVC表单页数据绑定与提交
另外献上在<线体验Demo地址>希望大家也能从中得到一些启示. 地址:http://121.40.148.178:8080/ . username:guest,password:12345 ...
- 日版 Galaxy Note sc05d 涮机
问题描写叙述:手机已坏.进不了系统 以下提供线涮刷机教程: 1.首先拔掉你的sd卡和sim卡.双清你的手机.可能有些版本号的机油没有双清模式,那就进到设置里边恢复出厂设置,然后关掉你的手机再开机.等到 ...
- Android于fragment_main.xml文件问题组件收购
package com.dhy.phonedial; import android.app.Activity; import android.app.Fragment; import android. ...
- cocos2d-x2.x环境搭建配置
[安装工具] VS2012 Cocos2D-X 2.2.3 Python 2.7.8 一.运行cocos2dx中的hello world! 1.在Cocos2D-X 2.2.3目录下,点击cocos2 ...