工廠模式也稱為虛構(gòu)造器窃诉,它適用于:一個(gè)類無法預(yù)期生成那個(gè)類對象耳鸯,想讓其子類來指定所生成具體對象榨乎。
工廠模式總體在同一類型差異性較小的子類之間以抽象基類作為其返回類型來適應(yīng)未來新增產(chǎn)品的動態(tài)調(diào)整丁屎,由于具有同樣的接口鲁沥,我們可以在新增產(chǎn)品類型時(shí)盡可能保障原有客戶端代碼邏輯的穩(wěn)定性呼股。同時(shí),由于各自類型的產(chǎn)品的初始化方案都已隔離進(jìn)各自的工廠方法中画恰,避免了牽一發(fā)而動其身的尷尬境地彭谁。
簡單工廠
簡單工廠模式不屬于23種GOF設(shè)計(jì)模式之一。它也稱作靜態(tài)工作方法模式允扇,是工廠方法模式的特殊實(shí)現(xiàn)(也就是說工廠模式包含簡單工廠模式)缠局。這里對簡單工廠模式進(jìn)行介紹,是為后面的工廠方法和抽象工廠模式做一個(gè)引子考润。
OC
typedef NS_ENUM(NSInteger) {
kApple,
kOrange,
kBanana
} FruitsType;
@interface FruitsFactory : NSObject
// 創(chuàng)造水果的工廠
+ (Fruits *)fruitsFactory:(FruitsType)type;
@end
@implementation FruitsFactory
+ (Fruits *)fruitsFactory:(FruitsType)type {
// 創(chuàng)建空的對象.在工廠方法里面進(jìn)行水果的制造
Fruits *fruits = nil;
switch (type) {
case kApple:
fruits = [[Apple alloc] init];
break;
case kOrange:
fruits = [[Orange alloc] init];
break;
case kBanana:
fruits = [[Banana alloc] init];
default:
break;
}
return fruits;
}
@end
@interface Fruits : NSObject
- (void)sweet; /**< 甜 */
- (void)poorTaste; /**< 不好吃 */
@end
@implementation Fruits
- (void)sweet {
}
- (void)poorTaste {
}
@end
@interface Banana : Fruits
@end
@implementation Banana
// 甜
- (void)sweet {
NSLog(@"Banana 非常甜");
}
// 不好吃
- (void)poorTaste {
NSLog(@"Banana 不好吃");
}
@end
@interface Apple : Fruits
- (void)freshApple; /**< 新鮮的蘋果 */
@end
@interface Apple : Fruits
- (void)freshApple; /**< 新鮮的蘋果 */
@end
@implementation Apple
// 甜
- (void)sweet {
NSLog(@"Apple 非常甜");
}
// 不好吃
- (void)poorTaste {
NSLog(@"Apple 不好吃");
}
// 新鮮的蘋果
- (void)freshApple {
NSLog(@"Apple 新鮮的蘋果");
}
@end
@interface Orange : Fruits
- (void)acidOrange; /**< 酸橘子 */
@end
@implementation Orange
// 甜
- (void)sweet {
NSLog(@"Orange 非常甜");
}
// 不好吃
- (void)poorTaste {
NSLog(@"Orange 不好吃");
}
/**< 酸橘子 */
- (void)acidOrange {
NSLog(@"Orange 有點(diǎn)酸");
}
@end
##################
// 在水果工廠里面創(chuàng)建出蘋果
Fruits *fruits = [FruitsFactory fruitsFactory:kApple];
[fruits sweet];
// 在水果工廠里面創(chuàng)建出蘋果, 調(diào)用私有的方法
Apple *apple = (Apple *)[FruitsFactory fruitsFactory:kApple];
[apple freshApple];
Swift
enum KFruitsType {
case kBanana,kApple,KOrange
}
class KFruitsFactory: NSObject {
static func fruitsFactory(type:KFruitsType) ->KFruits{
switch type {
case .kBanana:
return kBanana()
case .kApple:
return kBanana()
case .KOrange:
return KOrange()
}
}
}
class KFruits: NSObject {
func sweet(){
}
func poorTaste(){
}
}
class kBanana: KFruits {
override func sweet(){
print("Banana 非常甜")
}
override func poorTaste(){
print("Banana 不好吃")
}
}
class kApple: KFruits {
func freshApple(){
print("freshApple")
}
override func sweet(){
print("kApple 非常甜")
}
override func poorTaste(){
print("kApple 不好吃")
}
}
class KOrange: KFruits {
override func sweet(){
print("KOrange 非常甜")
}
override func poorTaste(){
print("KOrange 不好吃")
}
}
工廠方法(Factory Method)
@interface ColorViewGenerator : NSObject
- (ColorView *)colorViewWithFrame:(CGRect)frame;
@end
@implementation ColorViewGenerator
- (ColorView *)colorViewWithFrame:(CGRect)frame {
return [[ColorView alloc] initWithFrame:frame];
}
@end
@interface RedViewGenerator : ColorViewGenerator
@end
@implementation RedViewGenerator
- (ColorView *)colorViewWithFrame:(CGRect)frame {
return [[RedView alloc] initWithFrame:frame];
}
@end
@interface BlueViewGenerator : ColorViewGenerator
@end
@implementation BlueViewGenerator
- (ColorView *)colorViewWithFrame:(CGRect)frame {
return [[BlueView alloc] initWithFrame:frame];
}
@end
@interface ColorView : UIView
@end
@implementation ColorView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor grayColor]];
}
return self;
}
@end
@interface BlueView : ColorView
@end
@implementation BlueView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blueColor];
UIImage *backgroundImage = [UIImage imageNamed:@"tupian2"];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[self addSubview:backgroundView];
}
return self;
}
@end
@interface RedView : ColorView
@end
@implementation RedView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
UIImage *backgroundImage = [UIImage imageNamed:@"tupian"];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[self addSubview:backgroundView];
}
return self;
}
@end
#######
ColorViewGenerator *colorGen = [[RedViewGenerator alloc] init];
CGRect rect = CGRectMake(0, 0, 300, 600);
ColorView *red = [colorGen colorViewWithFrame:rect];