iOS 工厂模式(抽象工厂)

抽象工厂管理类

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

#import <Foundation/Foundation.h>
#import "BaseFactory.h"
#import "AppleFactory.h"
#import "GoogleFactory.h"

typedef enum : NSUInteger {
    kApple,
    kGoogle,
} EFactoryType;

@interface FactoryManager : NSObject

///获取工厂
+ (BaseFactory *)factoryWithBrand:(EFactoryType)factoryType;

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

#import "FactoryManager.h"

@implementation FactoryManager

+ (BaseFactory *)factoryWithBrand:(EFactoryType)factoryType {
    BaseFactory *factory = nil;
    if (factoryType == kApple) {
        factory = [[AppleFactory alloc] init];
    }
    else if (factoryType == kGoogle) {
        factory = [[GoogleFactory alloc] init];
    }
    return factory;
}

@end

抽象工厂类

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

#import <Foundation/Foundation.h>
#import "BasePhone.h"
#import "BaseWatch.h"

@interface BaseFactory : NSObject

///创建手机
- (BasePhone *)createPhone;

///创建手表
- (BaseWatch *)createWatch; 

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

#import "BaseFactory.h"

@implementation BaseFactory

- (BasePhone *)createPhone {
    return nil;///真实开发中需要使其崩溃
}

- (BaseWatch *)createWatch {
    return nil;
}

@end

苹果工厂

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

#import "BaseFactory.h"

@interface AppleFactory : BaseFactory

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

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

@implementation AppleFactory

- (BasePhone *)createPhone {
    return [[iPhone alloc] init];
}

- (BaseWatch *)createWatch {
    return [[iWatch alloc] init];
}

@end

谷歌工厂

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

#import "BaseFactory.h"

@interface GoogleFactory : BaseFactory

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

#import "GoogleFactory.h"
#import "AndroidPhone.h"
#import "AndroidWatch.h"

@implementation GoogleFactory

- (BasePhone *)createPhone {
    return [[AndroidPhone alloc] init];
}

- (BaseWatch *)createWatch {
    return [[AndroidWatch alloc] init];
}
@end

手机基类

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

#import <Foundation/Foundation.h>

@interface BasePhone : NSObject

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

#import "BasePhone.h"

@implementation BasePhone

@end

苹果手机

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

#import "BasePhone.h"

@interface iPhone : BasePhone

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

#import "iPhone.h"

@implementation iPhone

@end

安卓手机

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

#import "BasePhone.h"

@interface AndroidPhone : BasePhone

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

#import "AndroidPhone.h"

@implementation AndroidPhone

@end

手表基类

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

#import <Foundation/Foundation.h>

@interface BaseWatch : NSObject

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

#import "BaseWatch.h"

@implementation BaseWatch

@end

苹果手表

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

#import "BaseWatch.h"

@interface iWatch : BaseWatch

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

#import "iWatch.h"

@implementation iWatch

@end
//
//  AndroidWatch.h
//  LearnAbstractFactory
//
//  Created by 印林泉 on 2017/3/6.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "BaseWatch.h"

@interface AndroidWatch : BaseWatch

@end

安卓手表

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

#import "AndroidWatch.h"

@implementation AndroidWatch

@end

使用

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

#import "ViewController.h"
#import "FactoryManager.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    ///获取工厂
    BaseFactory *factory = [FactoryManager factoryWithBrand:kApple];
    ///创建商品
    BasePhone *phone = [factory createPhone];
    BaseWatch *watch = [factory createWatch];
    NSLog(@"%@ %@", phone, watch);
}

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

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,447评论 25 709
  • 文/小叶 纯属虚构,与他人无关,若有雷同,唯有抱歉,请勿对号入座。 人物形象,若有碍观瞻,与本人无关。 2016....
    博土阅读 3,355评论 0 0
  • 里面太吵,而且没有座位,只好到门口等一会,就是这个时候看见了那三个戴着黄色安全帽的工人。 三个人,面色...
    读书小哥阅读 3,142评论 1 1
  • 主要想回答两个问题: map端(shuffle-write)如何对数据进行分片? reduce端(shuffle-...
    jinxing阅读 8,340评论 1 1
  • 今天学习手帐学院的视频,知道手帐的用途。
    lemei阅读 754评论 0 0