Abstract factory pattern-抽象工厂模式

参考文献:
http://en.wikipedia.org/wiki/Abstract_factory_pattern

抽象工厂模式(英语:Abstract Factory)是一种常见的设计模式。此模式为一个产品家族提供了统一的创建接口。当需要这个产品家族的某一系列的时候,可以从抽象工厂中选出相对系的系列来创建一个具体的工厂类别。

[b]例子[/b]
假设我们有两种产品接口 Button 和 Border ,每一种产品都支持多种系列,比如 Mac 系列和 Windows 系列。这样每个系列的产品分别是 MacButton, WinButton, MacBorder, WinBorder 。为了可以在运行时刻创建一个系列的产品族,我们可以为每个系列的产品族建立一个工厂 MacFactory 和 WinFactory 。每个工厂都有两个方法 CreateButton 和 CreateBorder 并返回对应的产品,可以将这两个方法抽象成一个接口 AbstractFactory 。这样在运行时刻我们可以选择创建需要的产品系列。
[img]http://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Abstract_factory.svg/517px-Abstract_factory.svg.png[/img]

package com.sinosoft.train.gof.cp;

interface GUIFactory {
public Button createButton();
}


class WinFactory implements GUIFactory {
public Button createButton() {
return new WinButton();
}
}


class OSXFactory implements GUIFactory {
public Button createButton() {
return new OSXButton();
}
}

interface Button {
public void paint();
}


class WinButton implements Button {
public void paint() {
System.out.println("I'm a WinButton");
}
}


class OSXButton implements Button {
public void paint() {
System.out.println("I'm an OSXButton");
}
}


class Application {
public Application(GUIFactory factory){
Button button = factory.createButton();
button.paint();
}
}

public class AbstractFactory {
public static void main(String[] args) {
new Application(createOsSpecificFactory());
}

public static GUIFactory createOsSpecificFactory() {
int sys = 0;
if (sys == 1) {
return new WinFactory();
} else {
return new OSXFactory();
}
}
}


[b]适用性[/b]
在以下情况可以使用抽象工厂模式
一个系统要独立于它的产品的创建、组合和表示时。
一个系统要由多个产品系列中的一个来配置时。
当你要强调一系列相关的产品对象的设计以便进行联合使用时。
当你提供一个产品类库,而只想显示它们的接口而不是实现时

[b]优点[/b]
具体产品从客户代码中被分离出来
容易改变产品的系列
将一个系列的产品族统一到一起创建

[b]缺点[/b]
在产品族中扩展新的产品是很困难的,它需要修改抽象工厂的接口
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Abstract Factory Pattern是一种设计模式,它是创建对象的工厂模式的变体,允许对象在运行时被替换。 Abstract Factory模式提供了一种方法,可以创建一组相关或相互依赖的对象,而不需要明确指定其具体类。 下面是一个C语言的代码示例,该代码实现了一个抽象工厂模式,该模式创建一组车辆: ```c #include <stdio.h> typedef struct IVehicle IVehicle; struct IVehicle { void (*Drive)(IVehicle *); }; typedef struct Car Car; struct Car { IVehicle base; int wheelCount; }; void Car_Drive(IVehicle *vehicle) { Car *car = (Car *)vehicle; printf("Driving a car with %d wheels\n", car->wheelCount); } typedef struct Bike Bike; struct Bike { IVehicle base; int pedalCount; }; void Bike_Drive(IVehicle *vehicle) { Bike *bike = (Bike *)vehicle; printf("Riding a bike with %d pedals\n", bike->pedalCount); } typedef struct IVehicleFactory IVehicleFactory; struct IVehicleFactory { IVehicle *(*CreateVehicle)(IVehicleFactory *); }; typedef struct CarFactory CarFactory; struct CarFactory { IVehicleFactory base; }; IVehicle *CarFactory_CreateVehicle(IVehicleFactory *factory) { Car *car = (Car *)malloc(sizeof(Car)); car->base.Drive = &Car_Drive; car->wheelCount = 4; return (IVehicle *)car; } typedef struct BikeFactory BikeFactory; struct BikeFactory { IVehicleFactory base; }; IVehicle *BikeFactory_CreateVehicle(IVehicleFactory *factory) { Bike *bike = (Bike *)malloc(sizeof(Bike)); bike->base.Drive = &Bike_Drive; bike->pedalCount = 2; return (IVehicle *)bike; } int main(int argc, char *argv[]) { CarFactory carFactory = { { &CarFactory_CreateVehicle } }; IVehicle *vehicle = carFactory.base.CreateVehicle((IVehicleFactory *)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值