抽象工厂模式
抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体的情况下,创建多个产品族中的产品对象。根据里氏替换原则,任何接受父类型的地方,都应当能够接受子类型。因此,实际上系统所需要的,仅仅是类型与这些抽象产品角色相同的一些实例,而不是这些抽象产品的实例。换言之,也就是这些抽象产品的具体子类的实例。工厂类负责创建抽象产品的具体子类的实例。比如iOS中的NSNumber对象就是抽象对象,不能被实例化。
- (void)viewDidLoad {
[super viewDidLoad];
NSNumber *num = [[NSNumber alloc] init];
NSNumber *intNum = [NSNumber numberWithInt:97];
NSNumber *floatNum = [NSNumber numberWithFloat:1.0f];
NSNumber *boolNum = [NSNumber numberWithBool:YES];
NSLog(@"num = %@",[[num class] description]);
NSLog(@"intNum = %@",[[intNum class] description]);
NSLog(@"floatNum = %@",[[floatNum class] description]);
NSLog(@"boolNum = %@",[[boolNum class] description]);
NSLog(@"转化 = %c",[intNum charValue]);
}
2018-06-23 12:39:54.268165+0800 测试NSNumber抽象类[41376:2245510] num = (null)
2018-06-23 12:39:54.268313+0800 测试NSNumber抽象类[41376:2245510] intNum = __NSCFNumber
2018-06-23 12:39:54.268419+0800 测试NSNumber抽象类[41376:2245510] floatNum = __NSCFNumber
2018-06-23 12:39:54.268521+0800 测试NSNumber抽象类[41376:2245510] boolNum = __NSCFBoolean
2018-06-23 12:39:54.268658+0800 测试NSNumber抽象类[41376:2245510] 转化 = a
可以看出NSNumber不能被实例化,具体调用由子类实例化,比如:int、float都是由__NSCFNumber类生成,bool是由__NSCFBoolean类生成。
抽象工厂模式的UML类图
iOS测试代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
ColorViewFactory *base = [[ColorViewFactory alloc] init];
NSLog(@"base:%@", base);
UIView* view = [RedCodeViewFactory createView];
[self.view addSubview:view];
}
2018-06-23 12:53:48.630500+0800 Factory1[41795:2259264] base:(null)
ColorViewFactory文件:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ColorViewFactory : NSObject
+(UIView*)createView;
+(UIButton*)createButton;
@end
#import "ColorViewFactory.h"
@implementation ColorViewFactory
- (instancetype)init
{
if ([self isMemberOfClass:[ColorViewFactory class]]) {
return nil;
} else {
self = [super init];
if (self) {
}
return self;
}
}
+(UIView*)createView {return nil;}
+(UIButton*)createButton {return nil;}
@end
RedCodeViewFactory文件:
#import <Foundation/Foundation.h>
#import "ColorViewFactory.h"
@interface RedCodeViewFactory : ColorViewFactory
@end
#import "RedCodeViewFactory.h"
@implementation RedCodeViewFactory
+(UIView*)createView{
UIView* redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(0, 0, 100, 100);
return redView;
}
+(UIButton*)createButton{
UIButton* redBtn = [UIButton buttonWithType:0];
redBtn.frame = CGRectMake(100, 100, 100, 100);
return redBtn;
}
@end
ColorViewFactory文件:
#import <Foundation/Foundation.h>
#import "ColorViewFactory.h"
@interface BlueCodeViewFactory : ColorViewFactory
@end
#import "BlueCodeViewFactory.h"
@implementation BlueCodeViewFactory
+(UIView*)createView{
UIView* blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor redColor];
blueView.frame = CGRectMake(0, 0, 100, 100);
return blueView;
}
+(UIButton*)createButton{
UIButton* blueBtn = [UIButton buttonWithType:0];
blueBtn.frame = CGRectMake(100, 100, 100, 100);
return blueBtn;
}
@end