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) 详细说明的更多相关文章

  1. Java设计模式之工厂模式(Factory模式)介绍(转载)

    原文见:http://www.jb51.net/article/62068.htm 这篇文章主要介绍了Java设计模式之工厂模式(Factory模式)介绍,本文讲解了为何使用工厂模式.工厂方法.抽象工 ...

  2. C#设计模式总结 C#设计模式(22)——访问者模式(Vistor Pattern) C#设计模式总结 .NET Core launch.json 简介 利用Bootstrap Paginator插件和knockout.js完成分页功能 图片在线裁剪和图片上传总结 循序渐进学.Net Core Web Api开发系列【2】:利用Swagger调试WebApi

    C#设计模式总结 一. 设计原则 使用设计模式的根本原因是适应变化,提高代码复用率,使软件更具有可维护性和可扩展性.并且,在进行设计的时候,也需要遵循以下几个原则:单一职责原则.开放封闭原则.里氏代替 ...

  3. C#设计模式(6)——原型模式(Prototype Pattern) C# 深浅复制 MemberwiseClone

    C#设计模式(6)——原型模式(Prototype Pattern)   一.引言 在软件系统中,当创建一个类的实例的过程很昂贵或很复杂,并且我们需要创建多个这样类的实例时,如果我们用new操作符去创 ...

  4. 乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)

    原文:乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 模板方法模式(Template Method ...

  5. C#设计模式:代理模式(Proxy Pattern)

    一,什么是C#设计模式? 代理模式(Proxy Pattern):为其他对象提供一种代理以控制对这个对象的访问 二,代码如下: using System; using System.Collectio ...

  6. C#设计模式:组合模式(Composite Pattern)

    一,C#设计模式:组合模式(Composite Pattern) using System; using System.Collections.Generic; using System.Linq; ...

  7. 北风设计模式课程---解释器模式(Interpreter Pattern)

    北风设计模式课程---解释器模式(Interpreter Pattern) 一.总结 一句话总结: 不仅要通过视频学,还要看别的博客里面的介绍,搜讲解,搜作用,搜实例 设计模式都是对生活的抽象,比如用 ...

  8. 设计模式学习--复合模式(Compound Pattern)

    设计模式学习--复合模式(Compound Pattern) 概述 ——————————————————————————————————————————————————— 2013年8月4日<H ...

  9. C#设计模式:备忘录模式(Memento Pattern)

    一,C#设计模式:备忘录模式(Memento Pattern) 1.发起人角色(Originator):记录当前时刻的内部状态,负责创建和恢复备忘录数据.负责创建一个备忘录Memento,用以记录当前 ...

随机推荐

  1. vdsm的SSL证书验证过程

    1. Copy the VDSM certificate of the RHEV-H(Red Hat Enterprise Virtualization Hypervisor ) host to th ...

  2. 基于libevent, libuv和android Looper不断演进socket编程 - 走向架构师之路 - 博客频道 - CSDN.NET

    基于libevent, libuv和android Looper不断演进socket编程 - 走向架构师之路 - 博客频道 - CSDN.NET 基于libevent, libuv和android L ...

  3. python安装依赖

    yum install zlib zlib-devel openssl openssl-devel bzip2 bzip2-devel ncurses ncurses-devel readline r ...

  4. achieve aop through xml

    The main way to achive AOP is deploying a xml file. Now a xml file is presented to be a explanation ...

  5. 开发腾讯移动游戏平台SDK Android版Ane扩展 总结

    本文记录了在开发 腾讯移动游戏平台SDK(MSDK) Android版Ane扩展 过程中所遇到的问题和相关解决方式 问题一:编译报错:Unable to resolve target 'android ...

  6. Nginx 负载均衡-加权轮询策略剖析

    本文介绍的是客户端请求在多个后端服务器之间的均衡,注意与客户端请求在多个nginx进程之间的均衡相区别(Nginx根据每个工作进程的当前压力调整它们获取监听套接口的几率,那些当前比较空闲的工作进程有更 ...

  7. sql server2012附加的数据库问题

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjM2NzUxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  8. PostgreSQL服务端监听设置及client连接方法

    背景介绍: PostgreSQL服务端执行在RedHat Linux上,IP为:192.168.230.128 client安装在Windows XP上, IP为:192.168.230.1 配置方法 ...

  9. TextView中如何支持html标签,放置图片和动作标签

    TextView文本框和输入框几乎是一个正常的带界面的可交互的Android应用的基本组成 TextView主要作用是显示文本内容,其实还可以显示图片,当然有必要的话还可以为文本内容添加动作相应用户的 ...

  10. 《软件project》课程报告 —国土资源执法监察管理信息系统建模

    ***********************************************声明*************************************************** ...