iOS设计模式(抽象工厂)

抽象工厂(Abstract Factory)

提供一个固定的接口,用于创建一系列有关联或相依存的对象,而不必指定其具体类或其创建的细节。客户端与从工厂得到的具体对象之间没有耦合。

抽象工厂与工厂方法模式的区别

抽象工厂与工厂方法模式在许多方面有很多相似之处,以至于我们常常搞不清楚应该在什么时候用哪一个。两个模式都用于相同的目的:创建对象而不让客户端知晓返回了什么确切的具体对象。

抽象工厂:@、通过对象组合创建抽象产品。@、创建多系列产品。@、必须修改父类的接口才能支持新的产品。

工厂方法:@、通过类继承创建抽象产品。@、创建一种产品。@、子类化创建并重载工厂方法以创建新产品。

抽象工厂模式的优点:

(1)分离接口和实现

客户端使用抽象工厂来创建需要的对象,而客户端根本就不知道具体的实现是谁,客户端只是面向产品的接口编程而已。也就是说,客户端从具体的产品实现中解耦。

(2)使切换产品族变得容易

因为一个具体的工厂实现代表的是一个产品族,比如上面例子的从Intel系列到AMD系列只需要切换一下具体工厂。

抽象工厂模式的缺点:

不太容易扩展新的产品:如果需要给整个产品族添加一个新的产品,那么就需要修改抽象工厂,这样就会导致修改所有的工厂实现类。

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

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

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

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

@interface BlueViewFactory : ColorViewFactory

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

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

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

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

@end


@interface BlueButton : UIButton

@end
@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

@interface BlueSubView : UIView

@end
@implementation BlueSubView

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

@end

@interface RedButton : UIButton

@end
@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
@interface RedSubView : UIView

@end
@implementation RedSubView

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

@end


#######
/*
 抽象工厂
 1. 通过对象组合创建抽象产品
 2. 创建多个系列产品
 3. 必须修改父类的接口才能支持新的产品
 
 工厂方法
 1.通过类继承创建抽象产品
 2.创建一种产品
 3.子类化创建并重写工厂方法来创建新产品
 工厂方法: 多个产品抽象   抽象工厂: 是对工厂抽象
 */
  UIView *red = [RedViewFactory colorView];
  UIButton *btn = [RedViewFactory buttonView]
  [self.view addSubview:btn];
  [self.view addSubview:red];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 抽象工厂与简单工厂最大的区别就是抽象工厂的工厂类也是抽象的。 在软件设计中,如果客户端想手工创建一个类的对象,那么...
    live111阅读 619评论 1 1
  • 设计模式汇总 一、基础知识 1. 设计模式概述 定义:设计模式(Design Pattern)是一套被反复使用、多...
    MinoyJet阅读 3,978评论 1 15
  • 1 场景问题# 1.1 选择组装电脑的配件## 举个生活中常见的例子——组装电脑,我们在组装电脑的时候,通常需要选...
    七寸知架构阅读 4,417评论 6 67
  • 按照惯例,周末上午都要晨跑三五公里。那位戴着眼镜满头白发的老人又在小河边播放萨克斯曲目,依然还是那首《情人的眼泪》...
    青木秀竹阅读 514评论 2 9
  • 今天学了时间成本的计算,突然想到妈妈晚上为我织毛衣的样子,买可能不值钱多少钱,却花了妈妈不知多少个小时,很贵很温暖。
    Vivian_2b6f阅读 155评论 0 1