美文网首页
抽象工厂

抽象工厂

作者: XZhongWen | 来源:发表于2019-03-01 15:36 被阅读0次

iOS设计模式 - 抽象工厂

原理图

AbstractFactory.png

说明

  • 抽象工厂相当于在简单工厂基础上将工厂进行了抽象
  • 抽象工厂提供了创建一系列相关或互相依赖对象的接口, 为无需指定它们具体的类
  • 如果多个类有相同的行为,但实际实现不同,则可能需要某种抽象类型作为其父类被继承,抽象类型定义了所有相关具体类将共有的共同行为

代码实现

描述

  • 角色 抽象工厂, Apple工厂, Android工厂, 手机, 手表
  • 行为 工厂生产手机和手表; Apple工厂生产Apple手机和Apple手表; Android工厂生产Android手机和Android手表

客户通过切换不同的工厂实例来获取相应的产品; 即通过相同的接口, 切换不同的工厂实例获取相应的产品

Client

//
//  ViewController.m
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import "ViewController.h"
#import "AbstractFactory.h"
#import "AppleFactory.h"
#import "AndroidFactory.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 生产iPhone,iWatch
    AbstractFactory *factory = [[AppleFactory alloc] init];
    [factory createPhone];
    [factory createWatch];
    
    // 生产AndroidPhone, AndroidPhone
    factory = [[AndroidFactory alloc] init];
    [factory createPhone];
    [factory createWatch];
}
//
//  AbstractFactory.h
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "PhoneProtocol.h"
#import "WatchProtocol.h"

@interface AbstractFactory : NSObject

/**
 生产手机
 */
- (id<PhoneProtocol>)createPhone;

/**
 生产手表
 */
- (id<WatchProtocol>)createWatch;

@end
//
//  AppleFactory.h
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AbstractFactory.h"

@interface AppleFactory : AbstractFactory

@end

//
//  AppleFactory.m
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import "AppleFactory.h"
#import "iPhone.h"
#import "iWatch.h"

@implementation AppleFactory

- (id<PhoneProtocol>)createPhone {
    return [[iPhone alloc] init];
}

- (id<WatchProtocol>)createWatch {
    return [[iWatch alloc] init];
}

@end

//
//  AndroidFactory.h
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AbstractFactory.h"

@interface AndroidFactory : AbstractFactory

@end

//
//  AndroidFactory.m
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import "AndroidFactory.h"
#import "AndroidPhone.h"
#import "AndroidWatch.h"

@implementation AndroidFactory

- (id<PhoneProtocol>)createPhone {
    return [[AndroidPhone alloc] init];
}

- (id<WatchProtocol>)createWatch {
    return [[AndroidWatch alloc] init];
}

@end
//
//  PhoneProtocol.h
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol PhoneProtocol <NSObject>

/**
 打电话

 @param phoneNum 电话号码
 */
- (void)call:(NSString *)phoneNum;

@end

//
//  WatchProtocol.h
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol WatchProtocol <NSObject>

/**
 提醒时间
 */
- (void)reminderTime;

@end
//
//  iPhone.h
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "PhoneProtocol.h"

@interface iPhone : NSObject <PhoneProtocol>

@end

//
//  iPhone.m
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import "iPhone.h"

@implementation iPhone

- (void)call:(NSString *)phoneNum {
    NSLog(@"iPhone call");
}

@end
//
//  iWatch.h
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "WatchProtocol.h"

@interface iWatch : NSObject <WatchProtocol>

@end

//
//  iWatch.m
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import "iWatch.h"

@implementation iWatch

- (void)reminderTime {
    NSLog(@"iWatch reminder");
}

@end
//
//  AndroidPhone.h
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "PhoneProtocol.h"

@interface AndroidPhone : NSObject <PhoneProtocol>

@end

//
//  AndroidPhone.m
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import "AndroidPhone.h"

@implementation AndroidPhone

- (void)call:(NSString *)phoneNum {
    NSLog(@"Android call");
}

@end
//
//  AndroidWatch.h
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "WatchProtocol.h"

@interface AndroidWatch : NSObject <WatchProtocol>

@end

//
//  AndroidWatch.m
//  AbstractFactory
//
//  Created by mye on 2019/3/1.
//  Copyright © 2019 mye. All rights reserved.
//

#import "AndroidWatch.h"

@implementation AndroidWatch

- (void)reminderTime {
    NSLog(@"Android reminder");
}

@end

相关文章

  • 第3章 创建型模式-抽象工厂模式

    ■ 抽象工厂模式的优点 ■ 抽象工厂模式的缺点 ■ 抽象工厂模式的使用场景 ■ 抽象工厂 AbstractFact...

  • 抽象工厂模式(选择产品簇)

    目录 回顾众多工厂模式 抽象工厂模式的理念 抽象工厂模式与工厂方法模式的差异 怎么来实现抽象工厂模式 抽象工厂模式...

  • 【抽象工厂模式】Abstract Factory Design

    抽象工厂模式 抽象工厂模式是**Creational **模式之一 抽象工厂模式和工厂模式很相似,甚至可以说抽象工...

  • 工厂模式

    工厂模式细分三种:简单工厂模式、工厂模式、抽象工厂模式。 工厂模式相当于抽象了简单工厂模式的工厂类,而抽象工厂模式...

  • 设计模式(三)——抽象工厂模式

    抽象工厂模式 抽象工厂模式是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。在抽象工厂中,接口是负责...

  • 工厂模式

    抽象工厂模式:抽象工厂,具体工厂,抽象产品,具体产品 形容词用接口,名词用抽象类 工厂方法就是只生成一种产品的抽象...

  • 抽象工厂

    iOS设计模式 - 抽象工厂 原理图 说明 抽象工厂相当于在简单工厂基础上将工厂进行了抽象 抽象工厂提供了创建一系...

  • 设计模式(3) 抽象工厂模式

    抽象工厂模式 优化抽象工厂 异步工厂 在学习抽象工厂模式前,先来回顾一下前面的简单工厂和工厂方法模式。简单工厂的职...

  • Android 源码设计模式解析与实战 读书笔记 6 抽象工厂模

    创建型设计模式 —— 抽象工厂模式 1. 抽象工厂模式介绍 抽象工厂模式(Abstract Factory Pat...

  • 3. 设计模式的代码表示之一

    一、工厂模式(【客户类】→【工厂类/工厂方法】→创建【服务类】) 二、抽象工厂(【客户类】→【抽象工厂类】→【工厂...

网友评论

      本文标题:抽象工厂

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