iOS 工厂模式

一、GOF是这样描述工厂模式的:

“Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.”

在基类中定义创建对象的一个接口,让子类决定实例化哪个类。工厂方法让一个类的实例化延迟到子类中进行。

“专门定义一个类来负责创建其他类的实例,被创建的实例常常具有共同的父类。”

实际上就是由一个工厂类,根据传入的参数,动态的决定创建出哪一个产品类的实例。

工厂方法要解决的问题是对象的创建时机,它提供了一种扩展的策略,很好地符合了开放封闭原则。工厂方法也叫做虚构造器(Virtual Constructor).

二、什么时候使用工厂方法?


三、iOS中工厂方法的简单实现

  举个例子,有一家生产衣服的工厂,它生产2种型号的衣服,一个为DOTA类,一个为LOL类,经销商需要进什么类型的货一定要显示的告诉工厂,生产指定的类型,这样很麻烦,后来它有钱了,开了两家工厂,一家单独生产DOTA类,一家单独生产LOL类,这样,经销商进货直接去找对应的工厂就行.  

#import <Foundation/Foundation.h>

@interface HMTClothes : NSObject

//  展示衣服类型
- (void)showClothesType;												       @end

#import "HMTClothes.h"

@implementation HMTClothes
- (void)showClothesType{
}
@end

#import "HMTClothes.h"

@interface HMTClothes_Dota : HMTClothes

- (void)showClothesType;

@end

#import "HMTClothes_Dota.h"

@implementation HMTClothes_Dota

- (void)showClothesType{
  NSLog(@"这件衣服的类型是Dota");
}
@end

#import "HMTClothes.h"

@interface HMTClothes_LOL : HMTClothes

- (void)showClothesType;

@end

#import "HMTClothes_LOL.h"

@implementation HMTClothes_LOL

- (void)showClothesType{
  NSLog(@"这件衣服的类型是LOL");
}

@end


#import <Foundation/Foundation.h>

@class HMTClothes;
@interface HMTClothesFactory : NSObject

- (HMTClothes *)makeClothes;

@end

#import "HMTClothesFactory.h"

@implementation HMTClothesFactory

- (HMTClothes *)makeClothes{
  return nil;
}
@end

#import "HMTClothesFactory.h"

@interface HMTClothesDotaFactory : HMTClothesFactory

- (HMTClothes *)makeClothes;

@end

#import "HMTClothesDotaFactory.h"
#import "HMTClothes_Dota.h"

@implementation HMTClothesDotaFactory

- (HMTClothes *)makeClothes{
  return [[HMTClothes_Dota alloc] init];
}
@end

#import "HMTClothesFactory.h"

@interface HMTClothesLOLFactory : HMTClothesFactory

- (HMTClothes *)makeClothes;

@end

#import "HMTClothesLOLFactory.h"
#import "HMTClothes_LOL.h"
@implementation HMTClothesLOLFactory

- (HMTClothes *)makeClothes{
  return [[HMTClothes_LOL alloc] init];
}
@end


- (void)viewDidLoad{
  [super viewDidLoad];
  
  /**
   *  实例变量的类型,是由进行了alloc的Class决定的,一般也就是等号的右边
   *  就好比,NSMutableArray * mutableArray = 一个类型为NSArray的数组;它的实质还是一个NSArray类型,并
   *  不能调用NSMutableArray的方法
   */
  HMTClothesFactory *clothesFactory = [[HMTClothesDotaFactory alloc] init];
  HMTClothes *clothes = [clothesFactory makeClothes];
  [clothes showClothesType];  //  -->输出的是"这件衣服的类型是Dota"
  //  如果你想制造LOL衣服,直接把HMTClothesDotaFactory->HMTClothesLOLFactory即可
 }

通过以上的结构图和代码可知,简单工厂模式主要有三种角色,分别是工厂角色、抽象产品角色和具体产品角色。

  1. 工厂类角色:简单工厂模式的核心,负责根据传入的参数来实例化具体的产品实例。
  2. 抽象产品角色:通常是工厂产生具体类的父类(或者是具体类实现的接口)。
  3. 具体产品角色:简单工厂模式所创建的任何对象都是这个角色的实例。

优缺点                        

  从上面的介绍可以看出,简单工厂模式的优点是客户端可以直接消费产品,而不必关心具体产品的实现,消除了客户端直接创建产品对象的责任,实现了对责任的分割。

  缺点是工厂类集中了所有产品的创建逻辑,一旦不能正常工作,整个系统都会受到影响,而且当产品类别多结构复杂的时候,把所有创建工作放进一个工厂来,会使后期程序的扩展较为困难。

  通过优缺点的分析,我们可以在如下场景下使用简单工厂模式:

  1. 工厂类负责创建的对象比较少时;
  2. 客户端只知道传入工厂类的参数,对于如何创建对象的逻辑不必关心时。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值