工廠模式-抽象工廠
- 所謂的抽象工廠是指一個(gè)工廠等級(jí)結(jié)構(gòu)可以創(chuàng)建出分屬于不同產(chǎn)品等級(jí)結(jié)構(gòu)的一個(gè)產(chǎn)品族中的所有對(duì)象毕箍。
- 抽象工廠UML圖
3.png
- 抽象工廠(Abstract Factory== ColorViewFactory )角色:擔(dān)任這個(gè)角色的是工廠方法模式的核心财岔,它是與應(yīng)用系統(tǒng)商業(yè)邏輯無關(guān)的签则。
- 具體工廠(Concrete Factory== BlueViewFactory || RedViewFactory)角色:這個(gè)角色直接在客戶端的調(diào)用下創(chuàng)建產(chǎn)品的實(shí)例晴及。這個(gè)角色含有選擇合適的產(chǎn)品對(duì)象的邏輯郁季,而這個(gè)邏輯是與應(yīng)用系統(tǒng)的商業(yè)邏輯緊密相關(guān)的园爷。
- 抽象產(chǎn)品(Abstract Product)角色:擔(dān)任這個(gè)角色的類是工廠方法模式所創(chuàng)建的對(duì)象的父類构拳,或它們共同擁有的接口咆爽。
- 具體產(chǎn)品(Concrete Product)角色:抽象工廠模式所創(chuàng)建的任何產(chǎn)品對(duì)象都是某一個(gè)具體產(chǎn)品類的實(shí)例。這是客戶端最終需要的東西置森,其內(nèi)部一定充滿了應(yīng)用系統(tǒng)的商業(yè)邏輯斗埂。
demo實(shí)現(xiàn)
- ColorViewFactory(抽象工廠)
#import <UIKit/UIKit.h>
@interface ColorViewFactory : NSObject
+ (UIView *)colorView;
+ (UIButton *)colorButton;
+ (UILabel *)colorLabel;
@end
#import "ColorViewFactory.h"
@implementation ColorViewFactory
+ (UIView *)colorView
{
return nil;
}
+ (UIButton *)colorButton
{
return nil;
}
+ (UILabel *)colorLabel{
return nil;
}
- BlueViewFactory (具體工廠)
#import "ColorViewFactory.h"
@interface BlueViewFactory : ColorViewFactory
@end
#import "BlueViewFactory.h"
#import "BlueView.h"
#import "BlueLabel.h"
#import "BlueButton.h"
@implementation BlueViewFactory
+ (UIView *)colorView
{
return [[BlueView alloc] init];
}
+ (UIButton *)colorButton
{
//創(chuàng)建具體的產(chǎn)品對(duì)象
return [BlueButton buttonWithType:UIButtonTypeCustom];
}
+ (UILabel *)colorLabel
{
return [[BlueLabel alloc] init];
}
@end
- RedViewFactory(具體工廠)
#import "ColorViewFactory.h"
@interface RedViewFactory : ColorViewFactory
@end
#import "RedViewFactory.h"
#import "RedView.h"
#import "RedButton.h"
#import "RedLabel.h"
@implementation RedViewFactory
+ (UIView *)colorView
{
return [[RedView alloc] init];
}
+ (UIButton *)colorButton
{
return [RedButton buttonWithType:UIButtonTypeCustom];
}
+ (UILabel *)colorLabel
{
return [[RedLabel alloc] init];
}
@end
- RedView (具體產(chǎn)品)
#import <UIKit/UIKit.h>
@interface RedView : UIView
@end
#import "RedView.h"
@implementation RedView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
self.frame = CGRectMake(0, 0, 100, 100);
self.backgroundColor = [UIColor redColor];
return self;
}
@end
- ViewController 類
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "BlueViewFactory.h"
#import "RedViewFactory.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [BlueViewFactory colorView];
[self.view addSubview: view];
UIButton *btn = [BlueViewFactory colorButton];
[self.view addSubview: btn];
UILabel *lab = [BlueViewFactory colorLabel];
lab.frame = CGRectMake(100, 200, 100, 100);
lab.text = @"測(cè)試";
lab.backgroundColor = [UIColor blueColor];
[self.view addSubview:lab];
}
@end
源碼詳見:https://github.com/defuliu/AbstractFactory.git
小結(jié)
工廠方法:
1.通過類繼承創(chuàng)建抽象產(chǎn)品
2.創(chuàng)建一種產(chǎn)品
3.子類化創(chuàng)建并重寫工廠方法以創(chuàng)建新產(chǎn)品
抽象工廠:
1.通過對(duì)象組合創(chuàng)建抽象產(chǎn)品
2.創(chuàng)建多個(gè)系列的產(chǎn)品
3.必須要修改父類的接口才能支持新的產(chǎn)品