定义

抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式,属于对象创建型模式。

模式结构

抽象工厂模式包含如下角色:

  • AbstractFactory:抽象工厂
  • ConcreteFactory:具体工厂创建具有特定实现的产品对象
  • AbstractProduct:抽象产品
  • Product:具体产品

抽象工厂模式 - 图1

工厂模式与抽象工厂的区别

抽象工厂 工厂模式
通过对象组合创建抽象产品 通过类继承创建抽象产品
创建多种产品 创建一种产品
必须修改抽象工厂接口才能支持新产品,并且会对所有子类工厂造成影响 子类化工厂并重载工厂方法即可创建新产品

适用场景

在以下情况下可以使用抽象工厂模式:

  • 一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有类型的工厂模式都是重要的。
  • 系统中有多于一个的产品族,而每次只使用其中某一产品族。
  • 属于同一个产品族的产品将在一起使用,这一约束必须在系统的设计中体现出来。
  • 系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于具体实现。

    代码实现

    两个车牌基类:蓝色车牌和黄色车牌
  1. #import "LHCarLicenseProtocol.h"
  2. @interface LHBlueCarLicense : NSObject<LHCarLicenseProtocol>
  3. @property(nonatomic, copy)NSString *city; // 城市
  4. @end
  5. #import "LHBlueCarLicense.h"
  6. @implementation LHBlueCarLicense
  7. // 打印车牌号
  8. - (NSString *)printLicenseNumber{
  9. return nil;
  10. }
  11. @end
  1. #import "LHCarLicenseProtocol.h"
  2. @interface LHYellowCarLicense : NSObject<LHCarLicenseProtocol>
  3. @property(nonatomic, copy)NSString *city; // 城市
  4. @end
  5. #import "LHYellowCarLicense.h"
  6. @implementation LHYellowCarLicense
  7. // 打印牌照号
  8. - (NSString *)printLicenseNumber{
  9. return nil;
  10. }
  11. @end

下面是北京蓝色车牌和黄色车牌类实现(河北车牌代码一样,这里就不再贴代码了)

  1. #import "LHBlueCarLicense.h"
  2. @interface LHBeijingBlueCarLicense : LHBlueCarLicense
  3. @property(nonatomic, copy, readonly)NSString *licenseNumber; // 车牌号
  4. @end
  5. #import "LHBeijingBlueCarLicense.h"
  6. #import "LHCommonClass.h"
  7. @implementation LHBeijingBlueCarLicense
  8. // 打印牌照号
  9. - (NSString *)printLicenseNumber{
  10. self.city = @"京";
  11. _licenseNumber = [LHCommonClass getLicenseNumberWithCity:self.city];
  12. return [NSString stringWithFormat:@"北京蓝色车牌号:%@",_licenseNumber];
  13. }
  14. @end
  1. #import "LHYellowCarLicense.h"
  2. @interface LHBeijingYellowCarLicense : LHYellowCarLicense
  3. @property(nonatomic, copy, readonly)NSString *licenseNumber; // 车牌号
  4. @end
  5. #import "LHBeijingYellowCarLicense.h"
  6. #import "LHCommonClass.h"
  7. @implementation LHBeijingYellowCarLicense
  8. // 打印牌照号
  9. - (NSString *)printLicenseNumber{
  10. self.city = @"京";
  11. _licenseNumber = [LHCommonClass getLicenseNumberWithCity:self.city];
  12. return [NSString stringWithFormat:@"北京黄色车牌号:%@",_licenseNumber];
  13. }
  14. @end

产品族类定义完成后,下面我们看工厂类的实现。之前我们的工厂类定义的是蓝色车牌工厂类和黄色车牌工厂类,在抽象工厂模式中我们要定义的是北京车牌工厂类和河北车牌工厂类。我们先看工厂基类的定义

  1. #import <Foundation/Foundation.h>
  2. @class LHBlueCarLicense;
  3. @class LHYellowCarLicense;
  4. @interface LHCarLicenseFactory : NSObject
  5. /**
  6. * 获取蓝色牌照工厂
  7. *
  8. * @return 返回牌照对象
  9. */
  10. + (LHBlueCarLicense *)createBlueCarLicense;
  11. /**
  12. * 获取黄色牌照工厂
  13. *
  14. * @return 返回牌照对象
  15. */
  16. + (LHYellowCarLicense *)createYellowCarLicense;
  17. @end
  18. #import "LHCarLicenseFactory.h"
  19. #import "LHBlueCarLicense.h"
  20. #import "LHYellowCarLicense.h"
  21. @implementation LHCarLicenseFactory
  22. // 获取蓝色牌照工厂
  23. + (LHBlueCarLicense *)createBlueCarLicense
  24. {
  25. return nil;
  26. }
  27. // 获取黄色牌照工厂
  28. + (LHYellowCarLicense *)createYellowCarLicense
  29. {
  30. return nil;
  31. }
  32. @end

下面看北京工厂类的实现(河北工厂类代码实现一样,不在这里贴了)

  1. #import "LHCarLicenseFactory.h"
  2. @interface LHBeijingCarLicenseFactory : LHCarLicenseFactory
  3. @end
  4. #import "LHBeijingCarLicenseFactory.h"
  5. #import "LHBlueCarLicense.h"
  6. #import "LHYellowCarLicense.h"
  7. #import "LHBeijingBlueCarLicense.h"
  8. #import "LHBeijingYellowCarLicense.h"
  9. @implementation LHBeijingCarLicenseFactory
  10. // 北京蓝色牌照
  11. + (LHBlueCarLicense *)createBlueCarLicense
  12. {
  13. LHBlueCarLicense *_license = [[LHBeijingBlueCarLicense alloc] init];
  14. return _license;
  15. }
  16. // 北京黄色牌照
  17. + (LHYellowCarLicense *)createYellowCarLicense
  18. {
  19. LHYellowCarLicense *_license = [[LHBeijingYellowCarLicense alloc] init];
  20. return _license;
  21. }
  22. @end

那么客户端的调用就简单多了

  1. // 北京蓝色牌照点击事件
  2. - (IBAction)btnBeijingBlueEvent:(UIButton *)sender {
  3. LHBlueCarLicense *_license = [LHBeijingCarLicenseFactory createBlueCarLicense];
  4. _lbText.text = [NSString stringWithFormat:@"%@\n%@",_lbText.text,_license.printLicenseNumber];
  5. }
  6. // 北京黄色牌照点击事件
  7. - (IBAction)btnBeijingYellowEvent:(UIButton *)sender {
  8. LHYellowCarLicense *_license = [LHBeijingCarLicenseFactory createYellowCarLicense];
  9. _lbText.text = [NSString stringWithFormat:@"%@\n%@",_lbText.text,_license.printLicenseNumber];
  10. }

优点

  • 抽象工厂模式隔离了具体类的生成,使得客户并不需要知道什么被创建。由于这种隔离,更换一个具体工厂就变得相对容易。所有的具体工厂都实现了抽象工厂中定义的那些公共接口,因此只需改变具体工厂的实例,就可以在某种程度上改变整个软件系统的行为。另外,应用抽象工厂模式可以实现高内聚低耦合的设计目的,因此抽象工厂模式得到了广泛的应用。
  • 当一个产品族中的多个对象被设计成一起工作时,它能够保证客户端始终只使用同一个产品族中的对象。这对一些需要根据当前环境来决定其行为的软件系统来说,是一种非常实用的设计模式。
  • 增加新的具体工厂和产品族很方便,无须修改已有系统,符合“开闭原则”。

    缺点

  • 在添加新的产品对象时,难以扩展抽象工厂来生产新种类的产品,这是因为在抽象工厂角色中规定了所有可能被创建的产品集合,要支持新种类的产品就意味着要对该接口进行扩展,而这将涉及到对抽象工厂角色及其所有子类的修改,显然会带来较大的不便。

  • 开闭原则的倾斜性(增加新的工厂和产品族容易,增加新的产品等级结构麻烦)。