iOS架构师_工厂模式

简单工厂:

例子:
iOS架构师_工厂模式_第1张图片

模式图
iOS架构师_工厂模式_第2张图片

代码示例:
iOS架构师_工厂模式_第3张图片

创建水果工厂类FruitsFactory

再创建水果类Fruits,以及Fruits的子类Apple,Orange,Banana

FruitsFactory.h

#import 
#import "Fruits.h"
#import "Apple.h"
#import "Orange.h"
#import "Banana.h"

// 由于外面要传一个类型进来,所以要加一个枚举才行
typedef NS_ENUM(NSInteger) {
    kApple,
    kOrange,
    kBanana
} FruitsType;

@interface FruitsFactory : NSObject

// 创造水果的工厂
+ (Fruits *)fruitsFactory:(FruitsType)type;
@end

FruitsFactory.m

#import "FruitsFactory.h"

@implementation FruitsFactory
+ (Fruits *)fruitsFactory:(FruitsType)type {
    // 创建空的对象.在工厂方法里面进行水果的制造
    Fruits *fruits = nil;

    switch (type) {
        case kApple:
            fruits = [[Apple alloc] init];
            break;

        case kOrange:
            fruits = [[Orange alloc] init];
            break;
        case kBanana:
            fruits = [[Banana alloc] init];
        default:
            break;
    }
    return fruits;
}
@end

Fruits.h

#import 

@interface Fruits : NSObject
- (void)sweet; /**< 甜 */
- (void)poorTaste; /**< 不好吃 */
@end

Fruits.m

#import "Fruits.h"

@implementation Fruits

- (void)sweet {}

- (void)poorTaste {}

@end

Apple.h

#import "Fruits.h"
//遵循了开闭原则
@interface Apple : Fruits
- (void)freshApple; /**< 新鲜的苹果 */   // 开闭原则
@end

Apple.m

#import "Apple.h"

@implementation Apple
// 甜
- (void)sweet {
    NSLog(@"Apple 非常甜");
}

// 不好吃
- (void)poorTaste {
    NSLog(@"Apple 不好吃");
}

// 新鲜的苹果
- (void)freshApple {
    NSLog(@"Apple 新鲜的苹果");
}
@end

Orange.h

#import "Fruits.h"

@interface Orange : Fruits
- (void)acidOrange; /**< 酸橘子 */
@end

Orange.m

#import "Orange.h"

@implementation Orange
// 甜
- (void)sweet {
    NSLog(@"Orange 非常甜");
}

// 不好吃
- (void)poorTaste {
    NSLog(@"Orange 不好吃");
}

/**< 酸橘子 */
- (void)acidOrange {
    NSLog(@"Orange 有点酸");
}

@end

Banana.h

#import "Fruits.h"

@interface Banana : Fruits

@end

Banana.m

#import "Banana.h"
//遵循了里氏替换原则
@implementation Banana
// 甜
- (void)sweet {
    NSLog(@"Banana 非常甜");
}

// 不好吃
- (void)poorTaste {
    NSLog(@"Banana 不好吃");
}
@end

ViewController.m调用水果工厂类,通过工厂类创建我们需要的对象

#import "ViewController.h"
#import "FruitsFactory.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 在水果工厂里面创建出苹果
    Fruits *fruits = [FruitsFactory fruitsFactory:kApple];
    [fruits sweet];

    // 在水果工厂里面创建出苹果, 调用私有的方法
    Apple *apple = (Apple *)[FruitsFactory fruitsFactory:kApple];
    [apple freshApple];

    // 在水果工厂里面创建出橘子, 调用私有的方法
    Orange *orange = (Orange *)[FruitsFactory fruitsFactory:kOrange];
    [orange acidOrange];
}

@end

工厂方法

结构图

iOS架构师_工厂模式_第4张图片

代码示例:
iOS架构师_工厂模式_第5张图片

创建ColorViewGenerator,并且创建继承自ColorViewGenerator的子类RedViewGenerator,BlueViewGenerator

创建ColorView类,并且创建继承自ColorView的子类:RedView,BlueView

ColorViewGenerator.h

#import 
#import "ColorView.h"

@interface ColorViewGenerator : NSObject
- (ColorView *)colorViewWithFrame:(CGRect)frame;
@end

ColorViewGenerator.m

#import "ColorViewGenerator.h"

@implementation ColorViewGenerator
- (ColorView *)colorViewWithFrame:(CGRect)frame {
    return [[ColorView alloc] initWithFrame:frame];
}

@end

Generator 相当于一个工厂

RedViewGenerator.h

#import "ColorViewGenerator.h"
#import "RedView.h"

@interface RedViewGenerator : ColorViewGenerator

@end

RedViewGenerator.m

#import "RedViewGenerator.h"

@implementation RedViewGenerator
- (ColorView *)colorViewWithFrame:(CGRect)frame {
    return [[RedView alloc] initWithFrame:frame];
}
@end

BlueViewGenerator.h

#import "ColorViewGenerator.h"
#import "BlueView.h"

@interface BlueViewGenerator : ColorViewGenerator

@end

BlueViewGenerator.m

#import "BlueViewGenerator.h"

@implementation BlueViewGenerator
- (ColorView *)colorViewWithFrame:(CGRect)frame {
    return [[BlueView alloc] initWithFrame:frame];
}
@end

ColorView.m

#import "ColorView.h"

@implementation ColorView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor grayColor]];
    }
    return self;
}

@end

RedView.m

#import "RedView.h"

@implementation RedView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];

        UIImage *backgroundImage = [UIImage imageNamed:@"tupian"];
        UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
        [self addSubview:backgroundView];
    }
    return self;
}

@end

BlueView.m

#import "BlueView.h"

@implementation BlueView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blueColor];

        UIImage *backgroundImage = [UIImage imageNamed:@"tupian2"];
        UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
        [self addSubview:backgroundView];
    }
    return self;
}


@end

ViewController.m调用

#import "ViewController.h"
#import "ColorViewGenerator.h"
#import "RedViewGenerator.h"
#import "BlueViewGenerator.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 1.创建一个工厂类
    ColorViewGenerator *colorGen = [[RedViewGenerator alloc] init];
    CGRect rect = CGRectMake(0, 0, 300, 600);
    ColorView *red = [colorGen colorViewWithFrame:rect];

    // 添加
    [self.view addSubview:red];

    ColorViewGenerator *colorGen2 = [[BlueViewGenerator alloc] init];
    CGRect rect2 = CGRectMake(300, 600, 50, 50);
    ColorView * blue = [colorGen2 colorViewWithFrame:rect2];
    [self.view addSubview:blue];
}

@end

iOS架构师_工厂模式_第6张图片

工厂方法:对多个产品抽象

抽象工厂

抽象工厂方法:对工厂抽象

抽象工厂UML图

iOS架构师_工厂模式_第7张图片

代码示例:

iOS架构师_工厂模式_第8张图片

新建继承自NSObject的类ColorViewFactory
ColorViewFactory.h

#import 

@interface ColorViewFactory : NSObject
// 生产红色的View
+ (UIView *)colorView;

// 生产蓝色的UIButton
+ (UIButton *)buttonView;
@end

ColorViewFactory.m

#import "ColorViewFactory.h"

@implementation ColorViewFactory
+ (UIView *)colorView {
    return  nil;
}

// 生产蓝色的UIButton
+ (UIButton *)buttonView {
    return nil;
}
@end

创建继承自ColorViewFactory的RedViewFactory和BlueViewFactory

RedViewFactory.m

#import "RedViewFactory.h"
#import "RedButton.h"
#import "RedSubView.h"

@implementation RedViewFactory
+ (UIView *)colorView {
    return [[RedSubView alloc] init];
}

+ (UIButton *)buttonView {
     return [RedButton buttonWithType:UIButtonTypeCustom];
}

@end

子类实现父类的抽象方法


BlueViewFactory.m

#import "BlueViewFactory.h"
#import "BlueButton.h"
#import "BlueSubView.h"

@implementation BlueViewFactory
+ (UIView *)colorView {
    return [[BlueSubView alloc] init];
}

+ (UIButton *)buttonView {
    return [BlueButton buttonWithType:UIButtonTypeCustom];
}
@end

创建继承自UIView的RedSubView和BlueSubView

RedSubView.h
RedSubView.m

#import "RedSubView.h"

@implementation RedSubView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, 100, 100);
        self.backgroundColor = [UIColor redColor];
    }
    return self;
}

@end

BlueSubView.h
BlueSubView.m

#import "BlueSubView.h"

@implementation BlueSubView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, 100, 100);
        self.backgroundColor = [UIColor blueColor];
    }
    return self;
}

@end

创建继承自UIButton的RedButton和BlueButton

RedButton.h
RedButton.m

#import "RedButton.h"

@implementation RedButton

+ (instancetype)buttonWithType:(UIButtonType)buttonType {
    [super buttonWithType:buttonType];

    RedButton *btn = [[RedButton alloc] initWithFrame:CGRectMake(0, 100, 300, 30)];
    [btn setTitle:@"红色" forState:UIControlStateNormal];
    btn.titleLabel.backgroundColor = [UIColor redColor];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;

    return btn;
}

@end

BlueButton.h
BlueButton.m

#import "BlueButton.h"

@implementation BlueButton

+ (instancetype)buttonWithType:(UIButtonType)buttonType {
    [super buttonWithType:buttonType];

    BlueButton *btn = [[BlueButton alloc] initWithFrame:CGRectMake(0, 100, 300, 30)];
    [btn setTitle:@"蓝色" forState:UIControlStateNormal];
    btn.titleLabel.backgroundColor = [UIColor redColor];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;

    return btn;
}

@end

ViewController.m

#import "ViewController.h"
#import "RedViewFactory.h"
#import "BlueViewFactory.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *red = [RedViewFactory colorView];
    UIButton *btn = [RedViewFactory buttonView];
    [self.view addSubview:btn];
    [self.view addSubview:red];
}

@end

总结:

抽象工厂
1. 通过对象组合创建抽象产品
2. 创建多个系列产品
3. 必须修改父类的接口才能支持新的产品

工厂方法
1.通过类继承创建抽象产品
2.创建一种产品
3.子类化创建并重写工厂方法来创建新产品
工厂方法: 多个产品抽象 抽象工厂: 是对工厂抽象

你可能感兴趣的:(iOS进阶,iOS架构设计)