工厂方法模式:定义创建对象的接口,让子类决定实列化哪一个类,工厂方法使得一个类的实例化延迟到其子类。
- 工厂方法也称虚构造器,当一个类无法预期需要生成哪个类的对象,想让其子类来指定所生成的对象,这时就会用到工厂模式,可能这样说你还不理解,没事,会在后面附上代码,一步步带你理解何谓工厂方法
何时使用工厂方法
- 编译时无法确定预期要创建的对象的类型
- 类想让其子类决定在运行期间创建哪种类型的对象
- 类有若干个辅助类为其子类,而你想将返回哪个子类这一信息局部化
代码演示及讲解
这里我们用一个最简单创建不同类型的画布的例子来演示工厂模式的实现方式
- 首先定义其父类,我们把他命名为CanvasView(意思为画布),你可以在里面实现画布的默认样式,比如我们想画布默认为黑色
//.h里面可以定义一些属性和方法,这个取决于具体需求
#import <UIKit/UIKit.h>
@interface CanvasView : UIView
{}
...
@end
//.m文件就实现画布的默认样式
#import "CanvasView.h"
@implementation CanvasView
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor blackColor];
}
return self;
}
@end
*第二步,定义生成器,用来生成画布对象,这个生成器就生成了一张默认为黑色的画布
#import <Foundation/Foundation.h>
#import "CanvasView.h"
@interface CanvasViewGenerator : NSObject
-(CanvasView *)canvasViewWithFrame:(CGRect)frame;
@end
#import "CanvasViewGenerator.h"
@implementation CanvasViewGenerator
-(CanvasView *)canvasViewWithFrame:(CGRect)frame
{
return [[CanvasView alloc] initWithFrame:frame];
}
@end
*下面我们开始定义不同类型的画布对象及其生成器,我们把他们定义为红色和黄色两种类型,分别命名为RedCanvasView
,RedCanvasGenerator
和YellowCanvasView
,YellowCanvasGenerator
,其中RedCanvasView和YellowCanvasView继承于CanvasView,RedCanvasGenerator和YellowCanvasGenerator继承于CanvasGenerator
//--------------------------红色画布代码--------------------------
#import "CanvasView.h"
@interface RedCanvasView : CanvasView
@end
@implementation RedCanvasView
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
@end
#import <Foundation/Foundation.h>
#import "CanvasViewGenerator.h"
#import "RedCanvasView.h"
@interface RedCanvasViewGenerrator :CanvasViewGenerator
@end
#import "RedCanvasViewGenerrator.h"
@implementation RedCanvasViewGenerrator
-(CanvasView *)canvasViewWithFrame:(CGRect)frame
{
return [[RedCanvasView alloc] initWithFrame:frame];
}
@end
//--------------------------黄色画布代码--------------------------
#import "CanvasView.h"
@interface YellowCanvasView : CanvasView
@end
#import "YellowCanvasView.h"
@implementation YellowCanvasView
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor YellowColor];
}
return self;
}
@end
#import <Foundation/Foundation.h>
#import "CanvasViewGenerator.h"
#import "YellowCanvasView.h"
@interface YellowCanvasViewGenerrator : CanvasViewGenerator
@end
#import "YellowCanvasViewGenerrator.h"
@implementation YellowCanvasViewGenerrator
-(CanvasView *)canvasViewWithFrame:(CGRect)frame
{
return [[YellowCanvasView alloc] initWithFrame:frame];
}
@end
*画布使用
#import "ViewController.h"
#import "RedCanvasViewGenerrator.h"
#import "YellowCanvasViewGenerrator.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
RedCanvasViewGenerrator *defaultGenerator = [[RedCanvasViewGenerrator alloc] init];
[self loadCanvasViewWithGenerator:defaultGenerator];
}
-(void)loadCanvasViewWithGenerator:(CanvasViewGenerator *)generator
{
[_canvasView removeFromSuperview];
CGRect frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
CanvasView *canvasView = [generator canvasViewWithFrame:frame];
self.canvasView = canvasView;
[self.view addSubview:self.canvasView];
}
在控制器中,我们使用RedCanvasViewGenerrator *defaultGenerator = [[RedCanvasViewGenerrator alloc] init];
和
[self loadCanvasViewWithGenerator:defaultGenerator];
这两个方法来创建了一个红色画布。
在下一篇中,我们将继续讨论抽象工厂模式,人们经常容易将这两个模式混淆