IOS设计模式-抽象工厂

抽象工厂的灵活性要比简单工程的灵活性强。

抽象工厂的UML图(第一次画UML图,可能关系和箭头的图意义有错误,但是请不要以建模规范去看图,以最基本的结合后面OC代码,理解相关关系):

抽象工厂原理:
抽象工厂 较 简单工厂 多了抽象级别 而已。

因为需要创建抽象工厂,所以需要工厂管理器:
新建FactoryManager.h和FactoryManager.m:
FactoryManager.h:
 1 #import <Foundation/Foundation.h>
 2 #import "BaseFactory.h"
 3 #import "AppleFactory.h"
 4 #import "GoogleFactory.h"
 5 
 6 在抽象工厂类中为什么需要枚举列举工厂类,因为需要用户指定工厂,
 7 然后让工厂来生产具体产品
 8 typedef enum : NSUInteger {
 9     
10     kApple   = 0x11,
11     kGoogle,
12     
13 } EFactoryType;
14 
15 @interface FactoryManager : NSObject
16 
17 /**
18  *  获取工厂
19  *
20  *  @param factoryType 工厂类型
21  *
22  *  @return 创建出的工厂
23  */
24 + (BaseFactory *)factoryWithBrand:(EFactoryType)factoryType;
25 
26 @end
FactoryManager.h
FactoryManager.m:
 1 #import "FactoryManager.h"
 2 
 3 @implementation FactoryManager
 4 
 5 + (BaseFactory *)factoryWithBrand:(EFactoryType)factoryType {
 6     
 7     BaseFactory *factory = nil;
 8     
 9     if (factoryType == kApple) {
10         
11         factory = [[AppleFactory alloc] init];
12         
13     } else if (factoryType == kGoogle) {
14         
15         factory = [[GoogleFactory alloc] init];
16     }
17     
18     return factory;
19 }
20 
21 @end
FactoryManager.m
===============================================
建立具体工厂对应的父类工厂:
BaseFactory.h:
 1 #import <Foundation/Foundation.h>
 2 #import "BasePhone.h"
 3 #import "BaseWatch.h"
 4 
 5 @interface BaseFactory : NSObject
 6 
 7 /**
 8  *  创建手机
 9  *
10  *  @return 手机
11  */
12 - (BasePhone *)createPhone;
13 
14 /**
15  *  创建手表
16  *
17  *  @return 手表
18  */
19 - (BaseWatch *)createWatch;
20 
21 @end
BaseFactory.h
BaseFactory.m:
 1 #import "BaseFactory.h"
 2 
 3 @implementation BaseFactory
 4 
 5 - (BasePhone *)createPhone {
 6     
 7     return nil;
 8 }
 9 
10 - (BaseWatch *)createWatch {
11     
12     return nil;
13 }
14 
15 @end
BaseFactory.m

========================================================
关于继承的细节:
继承来自父类的方法,就不需要在子类的@interface声明中重写,直接在@implementation实现中实现父类的方法
========================================================
根据继承上面父类工厂新建两个具体工厂子类AppleFactory和GoogleFactory子类:
AppleFactory.h
#import "BaseFactory.h"

@interface AppleFactory : BaseFactory

@end
AppleFactory.m
#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.m
#import "BaseFactory.h"

@interface GoogleFactory : BaseFactory

@end

GoogleFactory.h
#import "GoogleFactory.h"
#import "Android.h"
#import "AndroidWatch.h"

@implementation GoogleFactory

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

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

@end


=========================================================
手机也有抽象的父类
BasePhone.h:
#import <Foundation/Foundation.h>

@interface BasePhone : NSObject

@end
BasePhone.m:
#import "BasePhone.h"

@implementation BasePhone

@end

=========================================================
手机具体的子类
iPhone.h
#import "BasePhone.h"

@interface iPhone : BasePhone

@end

iPhone.m
#import "iPhone.h"

@implementation iPhone

@end

=========================================================
Android.h
#import "BasePhone.h"

@interface Android : BasePhone

@end
Android.m
#import "Android.h"

@implementation Android

@end

=========================================================
手表抽象的类
BaseWatch.h
#import <Foundation/Foundation.h>

@interface BaseWatch : NSObject

@end
BaseWatch.m
#import "BaseWatch.h"

@implementation BaseWatch

@end

=========================================================
手表具体的类
iWatch.h
1 #import "iWatch.h"
2 
3 @implementation iWatch
4 
5 @end
iWatch.h
iWatch.m
 1 #import "BaseWatch.h"
 2 
 3 @interface AndroidWatch : BaseWatch
 4 
 5 @end
 6 
 7 #import "AndroidWatch.h"
 8 
 9 @implementation AndroidWatch
10 
11 @end
iWatch.m
=========================================================
客户端程序>>>>ViewController.m:
 1 #import "ViewController.h"
 2 #import "FactoryManager.h"
 3 
 4 @interface ViewController ()
 5 
 6 @end
 7 
 8 @implementation ViewController
 9 
10 - (void)viewDidLoad {
11     
12     [super viewDidLoad];
13     
14     // 获取工厂
15     BaseFactory *factory = [FactoryManager factoryWithBrand:kGoogle];
16     
17     // 创建商品
18     BasePhone *phone = [factory createPhone];
19     BaseWatch *watch = [factory createWatch];
20     
21     NSLog(@"%@ %@", phone, watch);
22 }
23 
24 @end
ViewController.m
 
 
 
原文地址:https://www.cnblogs.com/goodboy-heyang/p/4919161.html