iOS 工厂模式(简单工厂)

  • 工厂模式

    • 简单工厂模式
      具体工厂,简化生产流程,隔离生产细节,产品有一些共同功能。

    • 抽象工厂模式
      工厂类是抽象的,客户端需要通过抽象工厂指定具体工厂,具体工厂生产具体产品。

  • 应用,使用场景
    工厂生产手机
    品牌工厂生产手机

设备工厂

//
//  DeviceFactory.h
//  LearnSimpleFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import 
#import "BaseDevice.h"
#import "iPhoneDevice.h"
#import "AndroidDevice.h"

typedef enum : NSInteger {
    kiPhone = 0x11,
    kAndroid,
} EDeviceType;

@interface DeviceFactory : NSObject

///根据用户提交的指令创建出具体的手机
+ (BaseDevice *)deviceFactoryWithDeviceType:(EDeviceType)type;

@end
//
//  DeviceFactory.m
//  LearnSimpleFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "DeviceFactory.h"

@implementation DeviceFactory

+ (BaseDevice *)deviceFactoryWithDeviceType:(EDeviceType)type {
    BaseDevice *device = nil;
    if (type == kiPhone) {
        device = [[iPhoneDevice alloc] init];
    }
    else if (type == kAndroid) {
        device = [[AndroidDevice alloc] init];
    }
    return device;
}

@end

设备基类

//
//  BaseDevice.h
//  LearnSimpleFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import 
#import "PhoneProtocol.h"

@interface BaseDevice : NSObject

@end
//
//  BaseDevice.m
//  LearnSimpleFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "BaseDevice.h"

@implementation BaseDevice

- (void)phoneCall {
    
}

- (void)sendMessage {
    
}

@end

手机质量控制协议

//
//  PhoneProtocol.h
//  LearnSimpleFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import 

@protocol PhoneProtocol 

@required

/**
 *  打电话
 */
- (void)phoneCall;

/**
 *  发短信
 */
- (void)sendMessage;

@end

苹果手机

//
//  iPhoneDevice.h
//  LearnSimpleFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "BaseDevice.h"

@interface iPhoneDevice : BaseDevice

/**
 *  指纹识别
 */
- (void)fingerprintIndentification;

@end
//
//  iPhoneDevice.m
//  LearnSimpleFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "iPhoneDevice.h"

@implementation iPhoneDevice

- (void)phoneCall {
    NSLog(@"iPhone phoneCall");
}

- (void)sendMessage {
    NSLog(@"iPhone sendMessage");
}

- (void)fingerprintIndentification {
    NSLog(@"iPhone fingerprintIndentification");
}

@end

安卓手机

//
//  AndroidDevice.h
//  LearnSimpleFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "BaseDevice.h"

@interface AndroidDevice : BaseDevice

/**
 *  定制主题
 */
- (void)customTheme;

@end
//
//  AndroidDevice.m
//  LearnSimpleFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "AndroidDevice.h"

@implementation AndroidDevice

- (void)phoneCall {
    NSLog(@"Android phoneCall");
}

- (void)sendMessage {
    NSLog(@"Android sendMessage");
}

- (void)customTheme {
    NSLog(@"Android customTheme");
}

@end

使用

//
//  ViewController.m
//  LearnSimpleFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "ViewController.h"
#import "DeviceFactory.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    ///工厂中创建出具体产品
    iPhoneDevice *iPhone = (iPhoneDevice *)[DeviceFactory deviceFactoryWithDeviceType:kiPhone];
    ///使用产品的功能
    [iPhone fingerprintIndentification];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(iOS 工厂模式(简单工厂))