您的位置:首页 > 移动开发 > Objective-C

iOS的Objective-C的工厂设计模式详解

2016-03-24 18:10 621 查看
工厂设计模式,一共有三种:

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.当新增加一个产品时,需要修改工厂类,

当有很多产品时,那代码量就特别多,

不易于维护

2.当工厂类出现问题,所有产品停止工作

工厂方法工厂方法,定义一个产品协议,多个遵守该协议的类,

一个工厂协议,多个遵守该协议的实现类,

不同的工厂类实现不同的业务,当某一个

工厂类出现问题,其他的工厂类不受影响

当新增加产品时,不修改其他工厂的代码,

而是新添加一个工厂实现

当产品有几个大分类,这几个大分类下又

有许多小分类产品,如果小分类产品一旦

很多,就会造成类爆炸

抽象工厂抽象工厂,定义几个大分类产品协议,

有多个遵守该协议的实现类,

每个小分类产品遵守自己的大分类协议,

定义一个工厂协议,里面顶一个一些产品

方法,创建大分类的工厂实现类,遵守工厂

协议,实现工厂协议里的方法,在协议里的方法

处理业务逻辑,这个业务逻辑就在产品类里实现

而工厂协议方法里,只负责调用产品实现类已经

实现好的方法

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: