美文网首页
iOS 工厂模式(抽象工厂)

iOS 工厂模式(抽象工厂)

作者: 印林泉 | 来源:发表于2017-03-07 01:07 被阅读92次

抽象工厂管理类

//
//  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

相关文章

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

    设计模式系列传送门 iOS设计模式(一)之简单工厂模式iOS设计模式(二)之工厂模式iOS设计模式(三)之抽象工厂...

  • iOS设计模式(一)之简单工厂模式

    设计模式系列传送门 iOS设计模式(一)之简单工厂模式iOS设计模式(二)之工厂模式iOS设计模式(三)之抽象工厂...

  • iOS设计模式(二)之工厂模式

    设计模式系列传送门 iOS设计模式(一)之简单工厂模式iOS设计模式(二)之工厂模式iOS设计模式(三)之抽象工厂...

  • iOS设计模式(3)适配器模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(4)抽象工...

  • iOS设计模式(1)简单工厂模式

    设计模式系列文章 《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器模式》《iOS设计模式(4)抽象工厂...

  • 2016.06笔记

    iOS设计模式之工厂模式(简单工厂,工厂方法,抽象工厂) 简单工厂:简单工厂模式的工厂类一般是使用静态方法,通过接...

  • 工厂模式

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

  • 抽象工厂

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

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

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

  • 责任链模式

    Objective-C编程之道 iOS设计模式解析iOS设计模式解析-工厂模式iOS设计模式解析-抽象工厂模式iO...

网友评论

      本文标题:iOS 工厂模式(抽象工厂)

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