抽象工厂的灵活性要比简单工程的灵活性强。
抽象工厂的UML图(第一次画UML图,可能关系和箭头的图意义有错误,但是请不要以建模规范去看图,以最基本的结合后面OC代码,理解相关关系):
抽象工厂 较 简单工厂 多了抽象级别 而已。
因为需要创建抽象工厂,所以需要工厂管理器:
新建FactoryManager.h和FactoryManager.m:
FactoryManager.h:
#import <Foundation/Foundation.h>
#import "BaseFactory.h"
#import "AppleFactory.h"
#import "GoogleFactory.h" 在抽象工厂类中为什么需要枚举列举工厂类,因为需要用户指定工厂,
然后让工厂来生产具体产品
typedef enum : NSUInteger { kApple = 0x11,
kGoogle, } EFactoryType; @interface FactoryManager : NSObject /**
* 获取工厂
*
* @param factoryType 工厂类型
*
* @return 创建出的工厂
*/
+ (BaseFactory *)factoryWithBrand:(EFactoryType)factoryType; @end
FactoryManager.h
FactoryManager.m:
#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
FactoryManager.m
===============================================
建立具体工厂对应的父类工厂:
BaseFactory.h:
#import <Foundation/Foundation.h>
#import "BasePhone.h"
#import "BaseWatch.h" @interface BaseFactory : NSObject /**
* 创建手机
*
* @return 手机
*/
- (BasePhone *)createPhone; /**
* 创建手表
*
* @return 手表
*/
- (BaseWatch *)createWatch; @end
BaseFactory.h
BaseFactory.m:
#import "BaseFactory.h" @implementation BaseFactory - (BasePhone *)createPhone { return nil;
} - (BaseWatch *)createWatch { return nil;
} @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
#import "iWatch.h" @implementation iWatch @end
iWatch.h
iWatch.m
#import "BaseWatch.h" @interface AndroidWatch : BaseWatch @end #import "AndroidWatch.h" @implementation AndroidWatch @end
iWatch.m
=========================================================
客户端程序>>>>ViewController.m:
#import "ViewController.h"
#import "FactoryManager.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 获取工厂
BaseFactory *factory = [FactoryManager factoryWithBrand:kGoogle]; // 创建商品
BasePhone *phone = [factory createPhone];
BaseWatch *watch = [factory createWatch]; NSLog(@"%@ %@", phone, watch);
} @end
ViewController.m