iOS 工厂模式(抽象工厂)

抽象工厂管理类

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

#import 
#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 
#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 

@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 

@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 工厂模式(抽象工厂))