抽象工厂

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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容