工厂模式也称为虚构造器,它适用于:一个类无法预期生成那个类对象,想让其子类来指定所生成具体对象。
工厂模式总体在同一类型差异性较小的子类之间以抽象基类作为其返回类型来适应未来新增产品的动态调整,由于具有同样的接口,我们可以在新增产品类型时尽可能保障原有客户端代码逻辑的稳定性。同时,由于各自类型的产品的初始化方案都已隔离进各自的工厂方法中,避免了牵一发而动其身的尴尬境地。
简单工厂
简单工厂模式不属于23种GOF设计模式之一。它也称作静态工作方法模式,是工厂方法模式的特殊实现(也就是说工厂模式包含简单工厂模式)。这里对简单工厂模式进行介绍,是为后面的工厂方法和抽象工厂模式做一个引子。
OC
typedef NS_ENUM(NSInteger) {
kApple,
kOrange,
kBanana
} FruitsType;
@interface FruitsFactory : NSObject
// 创造水果的工厂
+ (Fruits *)fruitsFactory:(FruitsType)type;
@end
@implementation FruitsFactory
+ (Fruits *)fruitsFactory:(FruitsType)type {
// 创建空的对象.在工厂方法里面进行水果的制造
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 有点酸");
}
@end
##################
// 在水果工厂里面创建出苹果
Fruits *fruits = [FruitsFactory fruitsFactory:kApple];
[fruits sweet];
// 在水果工厂里面创建出苹果, 调用私有的方法
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];
网友评论