抽象工厂模式(Abstract Factory Pattern)

我认为抽象工厂模式就是为了解决工厂模式中违反闭包原则所建立的。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
优点:解决了闭包原则,通过统一的工厂方法作为入口,不同的实现类有统一的接口。
缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。
样例如下:

代码如下:

/* 实现类接口*/
public interface Produce {
public void produce();
}

/* 实现类Car1*/
public class Car1 implements Produce {
@Override
public void produce() {
// TODO Auto-generated method stub
System.out.println("生产Car1");
}
}

/* 实现类Car2*/
public class Car2 implements Produce {
@Override
public void produce() {
// TODO Auto-generated method stub
System.out.println("生产car2");
}
}

/* 工厂接口*/
public interface CarFactory {
public Produce produceCar();
}
/* 工厂实现类1*/
public class Car1Factory implements CarFactory {
@Override
public Produce produceCar() {
// TODO Auto-generated method stub
return new Car1();
}
}
/* 工厂实现类2*/
public class Car2Factory implements CarFactory {
@Override
public Produce produceCar() {
// TODO Auto-generated method stub
return new Car2();
}
}
/* demo*/
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {
CarFactory car1Factory = new Car1Factory();
Produce produce1 = car1Factory.produceCar();
produce1.produce();
CarFactory car2Factory = new Car2Factory();
Produce produce2 = car2Factory.produceCar();
produce2.produce();
}
}



  • 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、付费专栏及课程。

余额充值