工厂设计模式,一共有三种:
1. 简单工厂(Sample Factory)
2. 工厂方法 (Factory Method)
3. 抽象工厂 (Abstract Factory)
先来两张图看下
1. 简单工厂 (Sample Factory)
定义一个协议,创建几个遵守该协议的实现类,实现类实现自己的功能,和一个工厂类,这个工厂类用来实现业务逻辑,外部调用工厂时,完全不知工厂内部的实现类的实现
代码:
文件:ZRProduct.h协议文件
#import <Foundation/Foundation.h>
@protocol ZRProduct <NSObject>
@end
文件:ZROperator.h 遵守ZRProduct协议,实现相应的方法的声明
#import <Foundation/Foundation.h>
#import "ZRProduct.h"
@interface ZROperator : NSObject<ZRProduct>
@end
@interface WashingMachine : NSObject<ZRProduct>
@end
@interface Refrigerator : NSObject<ZRProduct>
@end
@interface AirCondition : NSObject<ZRProduct>
@end
文件:ZROperator.m 遵守ZRProduct协议,实现相应业务的方法实现
#import "ZROperator.h"
@implementation ZROperator
@end
@implementation WashingMachine
- (instancetype)init
{
if(self = [super init]){
NSLog(@"洗衣机被制造了");
}
return self;
}
@end
@implementation Refrigerator
- (instancetype)init
{
if(self = [super init]){
NSLog(@"冰箱被制造了");
}
return self;
}
@end
@implementation AirCondition
- (instancetype)init
{
if(self = [super init]){
NSLog(@"空调被制造了");
}
return self;
}
@end
文件:SampleFactory.h 工厂类,用来对外调用,调用者不需要知道内部是怎么实现的
#import <Foundation/Foundation.h>
#import "ZRProduct.h"
@interface SampleFactory : NSObject
+ (id<ZRProduct>)factory:(Class)className;
@end
文件:SampleFactory.m 工厂类的实现,需要传一个参数,类名
#import "SampleFactory.h"
#import "ZROperator.h"
@implementation SampleFactory
+ (id<ZRProduct>)factory:(Class)className
{
if([className class] == [WashingMachine class]){
return [[WashingMachine alloc] init];
} else if([className class] == [Refrigerator class]){
return [[Refrigerator alloc] init];
} else if([className class] == [AirCondition class]){
return [[AirCondition alloc] init];
} else {
return nil;
}
}
@end
调用及结果:
2.工厂方法 (Factory Method)
定义一个工厂协议,一个产品协议,多个遵守该协议的实现类,和多个工厂实现类,要求遵守工厂协议,产品协议定义一些共有方法,有产品实现类来实现,工厂协议定义产品协议,所以工厂实现类,实现每个产品实现类的业务
代码:
文件:ZRFactory.h 工厂协议
#import <Foundation/Foundation.h>
#import "ZRProduct.h"
@protocol ZRFactory <NSObject>
- (id<ZRProduct>)create;
@end
文件:ZRProduct.h 产品协议
#import <Foundation/Foundation.h>
@protocol ZRProduct <NSObject>
@end
文件:ZROperator.h 产品协议实现类头文件
#import <Foundation/Foundation.h>
#import "ZRProduct.h"
@interface ZROperator : NSObject<ZRProduct>
@end
@interface WashingMachine : NSObject<ZRProduct>
@end
@interface Refrigerator : NSObject<ZRProduct>
@end
@interface AirCondition : NSObject<ZRProduct>
@end
文件:ZROperator.m 产品实现类实现文件
#import "ZROperator.h"
@implementation ZROperator
@end
@implementation WashingMachine
- (instancetype)init
{
if(self = [super init]){
NSLog(@"洗衣机被制造了");
}
return self;
}
@end
@implementation Refrigerator
- (instancetype)init
{
if(self = [super init]){
NSLog(@"冰箱被制造了");
}
return self;
}
@end
@implementation AirCondition
- (instancetype)init
{
if(self = [super init]){
NSLog(@"空调被制造了");
}
return self;
}
@end
文件:SampleFactory.h 实现工厂类头文件
#import <Foundation/Foundation.h>
#import "ZRFactory.h"
@interface SampleFactory : NSObject
@end
@interface SampleFactoryWashinMachine : NSObject<ZRFactory>
@end
@interface SampleFactoryRefrigerator : NSObject<ZRFactory>
@end
@interface SampleFactoryAirConditioner : NSObject<ZRFactory>
@end
文件:SampleFactory.m 实现工厂类实现文件,会实现每个工厂方法,当然OC有可选,有必选的
#import "SampleFactory.h"
#import "ZROperator.h"
@implementation SampleFactory
@end
@implementation SampleFactoryWashinMachine
//实现工厂协议的方法,就是调用产品实现类的逻辑
- (id<ZRProduct>)create
{
return [[WashingMachine alloc] init];
}
@end
@implementation SampleFactoryRefrigerator
- (id<ZRProduct>)create
{
return [[Refrigerator alloc] init];
}
@end
@implementation SampleFactoryAirConditioner
- (id<ZRProduct>)create
{
return [[AirCondition alloc] init];
}
@end
调用及结果
3.抽象工厂 (Abstract Factory)
定义多个产品协议,创建多个产品实现类遵守产品协议,定义一个工厂协议,包含多个产品协议的定义方法,创建多个工厂实现类遵守工厂协议,实现工厂协议指定的方法
代码:
文件:ZRProduct.h 产品协议类,多个
#import <Foundation/Foundation.h>
@protocol ZRProduct <NSObject>
@end
@protocol WashingMachineProtocol <NSObject>
@end
@protocol RefrigeratorProtocol <NSObject>
@end
@protocol AirConditionerProtocol <NSObject>
@end
文件:ZROperator.h 产品实现类头文件
#import <Foundation/Foundation.h>
#import "ZRProduct.h"
@interface ZROperator : NSObject<ZRProduct>
@end
@interface WashingMachine : NSObject<WashingMachineProtocol>
@end
@interface Refrigerator : NSObject<RefrigeratorProtocol>
@end
@interface AirCondition : NSObject<AirConditionerProtocol>
@end
文件:ZROperator.m 产品实现类实现文件
#import "ZROperator.h"
@implementation ZROperator
@end
@implementation WashingMachine
- (instancetype)init
{
if(self = [super init]){
NSLog(@"洗衣机被制造了");
}
return self;
}
@end
@implementation Refrigerator
- (instancetype)init
{
if(self = [super init]){
NSLog(@"冰箱被制造了");
}
return self;
}
@end
@implementation AirCondition
- (instancetype)init
{
if(self = [super init]){
NSLog(@"空调被制造了");
}
return self;
}
@end
文件:ZRFactory.h 工厂协议文件
#import <Foundation/Foundation.h>
#import "ZRProduct.h"
@protocol ZRFactory <NSObject>
- (id<WashingMachineProtocol>)washCreate;
- (id<AirConditionerProtocol>)airCreate;
- (id<RefrigeratorProtocol>)refrigCreate;
@end
文件:SampleFactory.h 工厂实现类头文件
#import <Foundation/Foundation.h>
#import "ZRFactory.h"
@interface SampleFactory : NSObject
@end
@interface SampleFactoryWashinMachine : NSObject<ZRFactory>
@end
@interface SampleFactoryRefrigerator : NSObject<ZRFactory>
@end
@interface SampleFactoryAirConditioner : NSObject<ZRFactory>
@end
文件:SampleFactory.m 工厂实现类源文件
#import "SampleFactory.h"
#import "ZROperator.h"
@implementation SampleFactory
@end
@implementation SampleFactoryWashinMachine
//实现抽象工厂的方法,就是间接的调用了产品实现类的方法
- (id<WashingMachineProtocol>)washCreate
{
return [[WashingMachine alloc] init];
}
- (id<RefrigeratorProtocol>)refrigCreate
{
return [[Refrigerator alloc] init];
}
- (id<AirConditionerProtocol>)airCreate
{
return [[AirCondition alloc] init];
}
@end
@implementation SampleFactoryRefrigerator
- (id<WashingMachineProtocol>)washCreate
{
return [[WashingMachine alloc] init];
}
- (id<RefrigeratorProtocol>)refrigCreate
{
return [[Refrigerator alloc] init];
}
- (id<AirConditionerProtocol>)airCreate
{
return [[AirCondition alloc] init];
}
@end
@implementation SampleFactoryAirConditioner
- (id<WashingMachineProtocol>)washCreate
{
return [[WashingMachine alloc] init];
}
- (id<RefrigeratorProtocol>)refrigCreate
{
return [[Refrigerator alloc] init];
}
- (id<AirConditionerProtocol>)airCreate
{
return [[AirCondition alloc] init];
}
@end
运行及结果:
如果有说的不对之处,请大师指点!!!~~~谢谢!
综上所述:工厂的三种模式
| 优点 | 缺点 |
简单工厂 | 简单工厂的核心类就是SampleFactory, | 1.当新增加一个产品时,需要修改工厂类, |
工厂方法 | 工厂方法,定义一个产品协议,多个遵守该协议的类, | 当产品有几个大分类,这几个大分类下又 |
抽象工厂 | 抽象工厂,定义几个大分类产品协议, | |