美文网首页iOS Collectionios专题[0006]iOS设计模式
iOS设计模式(1)简单工厂模式

iOS设计模式(1)简单工厂模式

作者: leehoo | 来源:发表于2016-09-07 15:29 被阅读4395次
设计模式系列文章

《iOS设计模式(2)工厂模式》
《iOS设计模式(3)适配器模式》
《iOS设计模式(4)抽象工厂模式》
《iOS设计模式(5)策略模式》
《iOS设计模式(6)模板模式》
《iOS设计模式(7)建造者模式》

1.概念描述

“简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。”——百度百科

2.场景举例

举个例子,小猿最近买了一辆车,新车到手要做的第一件事就是去车管所上牌照。车管所大厅里,人满为患,大家先是根据自己的车辆提交相关的资料,然后车管所依次安排大家进行相关的摇号,拿到摇的号后交费就可以去外面的窗口等待拿车牌了。

车牌号都是一批一批的出的,大家的牌子都不一样,有的蓝牌,有的黄牌,大街上还见过军牌和警牌的。那这个砸牌子的地方其实就是一个“牌照工厂”。

这个模式本身很简单而且使用在业务较简单的情况下。一般用于小项目或者具体产品很少扩展的情况(这样工厂类才不用经常更改)。
它由三种角色组成:
工厂类角色:这是本模式的核心,含有一定的商业逻辑和判断逻辑,根据逻辑不同,产生具体的工厂产品。如例子中的LHCarLicenseFactory类。
抽象产品角色:它一般是具体产品继承的父类或者实现的接口。由接口或者抽象类来实现。如例中的LHCarLicense基类。
具体产品角色:工厂类所创建的对象就是此角色的实例。在oc中由一个具体类实现,如例子中的LHBlueCarLicense、LHYellowCarLicense类。引用

3代码实现

实现车牌基类


#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@protocol LHCarLicenseProtocol <NSObject>

// 打印牌照
- (NSString *)printLicenseNumber;

@end

@interface LHCarLicense : NSObject<LHCarLicenseProtocol>

@property(nonatomic, copy)NSString              *city; // 城市
@property(nonatomic, copy, readonly)NSString    *licenseNumber; // 车牌号

@end

#import "LHCarLicense.h"

@implementation LHCarLicense

#pragma mark -
#pragma mark Public Method

- (NSString *)printLicenseNumber{
    _licenseNumber = [self getLicenseNumber];
    return _licenseNumber;
}

#pragma mark -
#pragma mark Private Method

// 获取牌照
- (NSString *)getLicenseNumber
{
    NSString *firstChar = [self getRandomChar];
    NSString *lastNumber = @"";
    for (NSInteger index = 0; index < 5; index++) {
        NSInteger random = [self getRandomNumber:0 to:1];
        NSString *newNumber = random == 0 ? [self getRandomChar] : [NSString stringWithFormat:@"%ld",(long)[self getRandomNumber:0 to:9]];
        lastNumber = [NSString stringWithFormat:@"%@%@",lastNumber,newNumber];
    }
    return [NSString stringWithFormat:@"%@%@·%@",_city,firstChar,lastNumber];
}

// 获取牌照号
- (NSString *)getRandomChar
{
    int NUMBER_OF_CHARS = 1;
    char data[NUMBER_OF_CHARS];
    data[0] = (char)('A' + (arc4random_uniform(26)));
    return [[NSString alloc] initWithBytes:data length:NUMBER_OF_CHARS encoding:NSUTF8StringEncoding];
}

//获取一个随机整数,范围在[from,to),包括from,不包括to
-(NSInteger)getRandomNumber:(NSInteger)from to:(NSInteger)to
{
    return (NSInteger)(from + (arc4random() % (to - from + 1)));
}

然后派生出两个子类,蓝色车牌类和黄色车牌类

#import "LHCarLicense.h"

@interface LHBlueCarLicense : LHCarLicense

@end

#import "LHBlueCarLicense.h"

@implementation LHBlueCarLicense

// 打印牌照号
- (NSString *)printLicenseNumber{
    [super printLicenseNumber];
    return [NSString stringWithFormat:@"蓝色牌照: %@",self.licenseNumber];
}

@end
#import "LHCarLicense.h"

@interface LHYellowCarLicense : LHCarLicense

@end

#import "LHYellowCarLicense.h"

@implementation LHYellowCarLicense

// 打印牌照号
- (NSString *)printLicenseNumber{
    [super printLicenseNumber];
    return [NSString stringWithFormat:@"黄色牌照: %@",self.licenseNumber];
}

@end

关键的是创建工厂类

#import <Foundation/Foundation.h>

@class LHCarLicense;

typedef enum : NSUInteger {
    ELicenseType_Blue,
    ELicenseType_Yellow
} ELicenseType;

@interface LHCarLicenseFactory : NSObject

/**
 *  获取牌照工厂
 *
 *  @param type 牌照类型
 *
 *  @return 返回牌照对象
 */
+ (LHCarLicense *)createCarLicenseWithType:(ELicenseType)type;

@end

#import "LHCarLicenseFactory.h"
#import "LHCarLicense.h"
#import "LHBlueCarLicense.h"
#import "LHYellowCarLicense.h"

@implementation LHCarLicenseFactory

+ (LHCarLicense *)createCarLicenseWithType:(ELicenseType)type{
    
    LHCarLicense *_license;
    
    switch (type) {
        case ELicenseType_Blue:
            _license = [[LHBlueCarLicense alloc] init];
            break;
            
        case ELicenseType_Yellow:
            _license = [[LHYellowCarLicense alloc] init];
            break;
    }
    
    return _license;
}

@end

客户端调用

#import "ViewController.h"
#import "LHCarLicenseFactory.h"
#import "LHCarLicense.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)createLicense:(ELicenseType)type
{
    LHCarLicense *_license = [LHCarLicenseFactory createCarLicenseWithType:type];
    _license.city = _txtCity.text ? _txtCity.text : @"京";
    _lbLicenseNumber.text = [_license printLicenseNumber];
}

#pragma mark -
#pragma mark Button Event

// 生成蓝色牌照
- (IBAction)btnBlueEvent:(UIButton *)sender {
    
    [self createLicense:ELicenseType_Blue];
}

// 生成黄色牌照
- (IBAction)btnYellowEvent:(UIButton *)sender {
    
    [self createLicense:ELicenseType_Yellow];
}
@end

这样工作人员就可以批量生成不同类型的车牌号了,输出结果如下:

WechatIMG2.jpeg
优缺点

  • 优点:客户端调用简单明了,不需要关注太多的逻辑。
  • 缺点:也正是所谓的优点,导致了工厂类中揉杂了太多的业务逻辑,产品类本身是符合开闭原则的,对扩展开放对修改关闭,但是工厂类却违反了开闭原则,因为每增加一个产品,工厂类都需要进行逻辑修改和判断,导致耦合度太高

以上是个人的理解,有不同意见欢迎指正留言。

源码下载


本文参考
简单工厂、工厂方法、抽象工厂、策略模式、策略与工厂的区别

相关文章

网友评论

  • 莪de微笑掩飾著莪盷脆:谢谢分享,LHCarLicense的.h中不用在委托代理中添加方法- (NSString *)printLicenseNumber,直接在@interface LHCarLicense : NSObject 下面添加该方法。
    洛河水手:父类声明的公共方法派生类都可以继承,不知道作者为什么用要用协议定义成协议方法?
  • 开发仔小广:和类簇一样啊,oc里的NSNumber就是这吧
    c山龙隐秀:oc的nsnumber严格来说是抽象工厂 返回的是具体的工厂类
  • Rooses兵:感谢分享
  • Thebloodelves:那这个砸牌子的地方其实就是一个“牌照工厂”。 哈哈
    dominghao:有点意思航.
  • ttys苹果核:”然后派生出两个自(子)类,蓝色车牌类和黄色车牌类“ 有个错别字
    leehoo:@lq9869 好的,谢谢提醒,马上更正:+1:

本文标题:iOS设计模式(1)简单工厂模式

本文链接:https://www.haomeiwen.com/subject/yspjettx.html