定义
抽象工厂模式(Abstract Factory Pattern
):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式,属于对象创建型模式。
模式结构
抽象工厂模式包含如下角色:
- AbstractFactory:抽象工厂
- ConcreteFactory:具体工厂创建具有特定实现的产品对象
- AbstractProduct:抽象产品
- Product:具体产品
工厂模式与抽象工厂的区别
抽象工厂 | 工厂模式 |
---|---|
通过对象组合创建抽象产品 | 通过类继承创建抽象产品 |
创建多种产品 | 创建一种产品 |
必须修改抽象工厂接口才能支持新产品,并且会对所有子类工厂造成影响 | 子类化工厂并重载工厂方法即可创建新产品 |
适用场景
在以下情况下可以使用抽象工厂模式:
- 一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有类型的工厂模式都是重要的。
- 系统中有多于一个的产品族,而每次只使用其中某一产品族。
- 属于同一个产品族的产品将在一起使用,这一约束必须在系统的设计中体现出来。
- 系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于具体实现。
代码实现
两个车牌基类:蓝色车牌和黄色车牌
#import "LHCarLicenseProtocol.h"
@interface LHBlueCarLicense : NSObject<LHCarLicenseProtocol>
@property(nonatomic, copy)NSString *city; // 城市
@end
#import "LHBlueCarLicense.h"
@implementation LHBlueCarLicense
// 打印车牌号
- (NSString *)printLicenseNumber{
return nil;
}
@end
#import "LHCarLicenseProtocol.h"
@interface LHYellowCarLicense : NSObject<LHCarLicenseProtocol>
@property(nonatomic, copy)NSString *city; // 城市
@end
#import "LHYellowCarLicense.h"
@implementation LHYellowCarLicense
// 打印牌照号
- (NSString *)printLicenseNumber{
return nil;
}
@end
下面是北京蓝色车牌和黄色车牌类实现(河北车牌代码一样,这里就不再贴代码了)
#import "LHBlueCarLicense.h"
@interface LHBeijingBlueCarLicense : LHBlueCarLicense
@property(nonatomic, copy, readonly)NSString *licenseNumber; // 车牌号
@end
#import "LHBeijingBlueCarLicense.h"
#import "LHCommonClass.h"
@implementation LHBeijingBlueCarLicense
// 打印牌照号
- (NSString *)printLicenseNumber{
self.city = @"京";
_licenseNumber = [LHCommonClass getLicenseNumberWithCity:self.city];
return [NSString stringWithFormat:@"北京蓝色车牌号:%@",_licenseNumber];
}
@end
#import "LHYellowCarLicense.h"
@interface LHBeijingYellowCarLicense : LHYellowCarLicense
@property(nonatomic, copy, readonly)NSString *licenseNumber; // 车牌号
@end
#import "LHBeijingYellowCarLicense.h"
#import "LHCommonClass.h"
@implementation LHBeijingYellowCarLicense
// 打印牌照号
- (NSString *)printLicenseNumber{
self.city = @"京";
_licenseNumber = [LHCommonClass getLicenseNumberWithCity:self.city];
return [NSString stringWithFormat:@"北京黄色车牌号:%@",_licenseNumber];
}
@end
产品族类定义完成后,下面我们看工厂类的实现。之前我们的工厂类定义的是蓝色车牌工厂类和黄色车牌工厂类,在抽象工厂模式中我们要定义的是北京车牌工厂类和河北车牌工厂类。我们先看工厂基类的定义
#import <Foundation/Foundation.h>
@class LHBlueCarLicense;
@class LHYellowCarLicense;
@interface LHCarLicenseFactory : NSObject
/**
* 获取蓝色牌照工厂
*
* @return 返回牌照对象
*/
+ (LHBlueCarLicense *)createBlueCarLicense;
/**
* 获取黄色牌照工厂
*
* @return 返回牌照对象
*/
+ (LHYellowCarLicense *)createYellowCarLicense;
@end
#import "LHCarLicenseFactory.h"
#import "LHBlueCarLicense.h"
#import "LHYellowCarLicense.h"
@implementation LHCarLicenseFactory
// 获取蓝色牌照工厂
+ (LHBlueCarLicense *)createBlueCarLicense
{
return nil;
}
// 获取黄色牌照工厂
+ (LHYellowCarLicense *)createYellowCarLicense
{
return nil;
}
@end
下面看北京工厂类的实现(河北工厂类代码实现一样,不在这里贴了)
#import "LHCarLicenseFactory.h"
@interface LHBeijingCarLicenseFactory : LHCarLicenseFactory
@end
#import "LHBeijingCarLicenseFactory.h"
#import "LHBlueCarLicense.h"
#import "LHYellowCarLicense.h"
#import "LHBeijingBlueCarLicense.h"
#import "LHBeijingYellowCarLicense.h"
@implementation LHBeijingCarLicenseFactory
// 北京蓝色牌照
+ (LHBlueCarLicense *)createBlueCarLicense
{
LHBlueCarLicense *_license = [[LHBeijingBlueCarLicense alloc] init];
return _license;
}
// 北京黄色牌照
+ (LHYellowCarLicense *)createYellowCarLicense
{
LHYellowCarLicense *_license = [[LHBeijingYellowCarLicense alloc] init];
return _license;
}
@end
那么客户端的调用就简单多了
// 北京蓝色牌照点击事件
- (IBAction)btnBeijingBlueEvent:(UIButton *)sender {
LHBlueCarLicense *_license = [LHBeijingCarLicenseFactory createBlueCarLicense];
_lbText.text = [NSString stringWithFormat:@"%@\n%@",_lbText.text,_license.printLicenseNumber];
}
// 北京黄色牌照点击事件
- (IBAction)btnBeijingYellowEvent:(UIButton *)sender {
LHYellowCarLicense *_license = [LHBeijingCarLicenseFactory createYellowCarLicense];
_lbText.text = [NSString stringWithFormat:@"%@\n%@",_lbText.text,_license.printLicenseNumber];
}
优点
- 抽象工厂模式隔离了具体类的生成,使得客户并不需要知道什么被创建。由于这种隔离,更换一个具体工厂就变得相对容易。所有的具体工厂都实现了抽象工厂中定义的那些公共接口,因此只需改变具体工厂的实例,就可以在某种程度上改变整个软件系统的行为。另外,应用抽象工厂模式可以实现高内聚低耦合的设计目的,因此抽象工厂模式得到了广泛的应用。
- 当一个产品族中的多个对象被设计成一起工作时,它能够保证客户端始终只使用同一个产品族中的对象。这对一些需要根据当前环境来决定其行为的软件系统来说,是一种非常实用的设计模式。
增加新的具体工厂和产品族很方便,无须修改已有系统,符合“开闭原则”。
缺点
在添加新的产品对象时,难以扩展抽象工厂来生产新种类的产品,这是因为在抽象工厂角色中规定了所有可能被创建的产品集合,要支持新种类的产品就意味着要对该接口进行扩展,而这将涉及到对抽象工厂角色及其所有子类的修改,显然会带来较大的不便。
- 开闭原则的倾斜性(增加新的工厂和产品族容易,增加新的产品等级结构麻烦)。