设计模式——简单工厂,工厂方法,抽象工厂

2016-04-11  本文已影响1282人  game3108

前言

本文csdn地址:http://blog.csdn.net/game3108/article/details/51147953
设计模式在工作中经常会用到:

本身设计模式的诞生,也是基于设计模式的六大原则:

具体这六大原则的含义,可以参考设计模式六大原则

我自己在用设计模式的过程当中,主要感觉到其实设计模式有以下作用:

本文主要以代码形式实现每一种设计模式,算是自己的一种复习和实践。相应的代码,也会放到github上。
本文的类图均来自于《Objective-C编程之道 iOS设计模式解析》。

本篇主要讲:

0.简单工厂

简单工厂本身并不是23种设计模式的一种,更比较像一种编程习惯,但也是经常使用。
《Head First》的例子:
客户到pizza store点不同类型的pizza,有cheese的,也有clam的。

pizza类:

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, PizzaType){
    Cheese  = 0,
    Clam    = 1
};

@interface SFPizza : NSObject
- (void) prepare;
- (void) bake;
- (void) cut;
- (void) box;
@end
#import "SFPizza.h"

@interface SFCheesePizza : SFPizza

@end

@interface SFClamPizza : SFPizza

@end

Simple Factory:

#import <Foundation/Foundation.h>
#import "SFPizza.h"

@interface SFPizzaSimpleFactory : NSObject
- (SFPizza *) createPizza:(PizzaType) pizzaType;
@end
#import "SFPizzaSimpleFactory.h"
#import "SFCheesePizza.h"
#import "SFClamPizza.h"

@implementation SFPizzaSimpleFactory

- (SFPizza *) createPizza:(PizzaType) pizzaType{
    SFPizza *pizza = nil;
    if (pizzaType == Cheese){
        pizza = [[SFCheesePizza alloc]init];
    }else if (pizzaType == Clam){
        pizza = [[SFClamPizza alloc]init];
    }
    return pizza;
}

@end

Pizza Store:

#import <Foundation/Foundation.h>
#import "SFPizza.h"

@interface SFPizzaStore : NSObject
- (SFPizza *) orderPizza:(PizzaType)pizzaType;
@end
#import "SFPizzaStore.h"
#import "SFPizzaSimpleFactory.h"

@interface SFPizzaStore(){
    SFPizzaSimpleFactory *_simpleFactory;
}
@end

@implementation SFPizzaStore

- (instancetype)init{
    self = [super init];
    if ( self ){
        _simpleFactory = [[SFPizzaSimpleFactory alloc]init];
    }
    return self;
}

- (SFPizza *) orderPizza:(PizzaType)pizzaType{
    SFPizza *pizza = [_simpleFactory createPizza:pizzaType];
    
    [pizza prepare];
    [pizza bake];
    [pizza cut];
    [pizza box];
    
    return pizza;
}
@end

1.工厂方法

概念:定义创建对象的接口,让子类决定实例化哪一个类。工厂方法使得一个类的实例化延迟到子类。

类图:

Paste_Image.png

何时使用:

《Head First》的例子:
纽约和芝加哥的pizza store有各自差异化的风味,提供当地风格的cheese和clam的pizza。

pizza类:

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, PizzaType){
    Cheese  = 0,
    Clam    = 1
};

@interface FMPizza : NSObject
- (void) prepare;
- (void) bake;
- (void) cut;
- (void) box;
@end
#import "FMPizza.h"

@interface FMNYCheesePizza : FMPizza

@end

@interface FMNYClamPizza : FMPizza

@end

@interface FMChicagoCheesePizza : FMPizza

@end

@interface FMChicagoClamPizza : FMPizza

@end

pizza store抽象:

#import <Foundation/Foundation.h>
#import "FMPizza.h"

@protocol FMPizzaStore <NSObject>
- (FMPizza *) createPizza:(PizzaType)pizzaType;
- (FMPizza *) orderPizza:(PizzaType)pizzaType;
@end

pizza store实现:

#import <Foundation/Foundation.h>
#import "FMPizzaStore.h"

@interface FMNYPizzaStore : NSObject<FMPizzaStore>

@end
#import "FMNYPizzaStore.h"
#import "FMNYCheesePizza.h"
#import "FMNYClamPizza.h"

@implementation FMNYPizzaStore
- (FMPizza *) createPizza:(PizzaType)pizzaType{
    FMPizza *pizza = nil;
    if (pizzaType == Cheese){
        pizza = [[FMNYCheesePizza alloc]init];
    } else if (pizzaType == Clam){
        pizza = [[FMNYClamPizza alloc]init];
    }
    return pizza;
}
- (FMPizza *) orderPizza:(PizzaType)pizzaType{
    FMPizza *pizza = [self createPizza:pizzaType];
    
    [pizza prepare];
    [pizza bake];
    [pizza cut];
    [pizza box];
    
    return pizza;
}
@end
#import <Foundation/Foundation.h>
#import "FMPizzaStore.h"

@interface FMChicagoPizzaStore : NSObject<FMPizzaStore>

@end
#import "FMChicagoPizzaStore.h"
#import "FMChicagoCheesePizza.h"
#import "FMChicagoClamPizza.h"

@implementation FMChicagoPizzaStore
- (FMPizza *) createPizza:(PizzaType)pizzaType{
    FMPizza *pizza = nil;
    if (pizzaType == Cheese){
        pizza = [[FMChicagoCheesePizza alloc]init];
    } else if (pizzaType == Clam){
        pizza = [[FMChicagoClamPizza alloc]init];
    }
    return pizza;
}
- (FMPizza *) orderPizza:(PizzaType)pizzaType{
    FMPizza *pizza = [self createPizza:pizzaType];
    
    [pizza prepare];
    [pizza bake];
    [pizza cut];
    [pizza box];
    
    return pizza;
}
@end

2.抽象工厂

概念:提供一个创建一系列相关或相互以来对象的接口,而无需指定它们的具体的类。

类图:

Paste_Image.png

何时使用:

抽象工厂和工厂方法的区别:
抽象工厂:

工厂方法:

《Head First》的例子:
纽约和芝加哥的pizza store提供的cheese和clam的pizza,但原料不同,做出来的pizza也不同。

原料类:

@interface AFCheese : NSObject
@end
@interface AFNYCheese : AFCheese
@end
@interface AFChicagoCheese : AFCheese
@end

@interface AFClam : NSObject
@end
@interface AFNYClam : AFClam
@end
@interface AFChicagoClam : AFClam
@end

@interface AFSauce : NSObject
@end

pizza类:

#import <Foundation/Foundation.h>
#import "AFCheese.h"
#import "AFClam.h"
#import "AFSauce.h"
#import "AFPizzaIngredientFactory.h"

typedef NS_ENUM(NSInteger, PizzaType){
    Cheese  = 0,
    Clam    = 1
};

@interface AFPizza : NSObject
@property (nonatomic, strong) AFCheese  *cheese;
@property (nonatomic, strong) AFClam    *clam;
@property (nonatomic, strong) AFSauce   *sauce;
- (instancetype)initWithPizzaIngredientFactory:(id<AFPizzaIngredientFactory>) factory;
- (void) prepare;
- (void) bake;
- (void) cut;
- (void) box;
@end
#import "AFPizza.h"

@implementation AFPizza
- (instancetype)initWithPizzaIngredientFactory:(id<AFPizzaIngredientFactory>) factory{
    self = [super init];
    return self;
}
- (void) prepare{
}
- (void) bake{
    NSLog(@"start bake");
}
- (void) cut{
    NSLog(@"start cut");
}
- (void) box{
    NSLog(@"start box");
}
@end

pizza 子类:

#import "AFPizza.h"

@interface AFCheesePizza : AFPizza
@end

#import "AFCheesePizza.h"

@interface AFCheesePizza(){
    id<AFPizzaIngredientFactory> _factory;
}
@end

@implementation AFCheesePizza

- (instancetype)initWithPizzaIngredientFactory:(id<AFPizzaIngredientFactory>) factory{
    self = [super init];
    if (self){
        _factory = factory;
    }
    return self;
}

- (void)prepare{
    self.cheese = [_factory createCheese];
    self.sauce  = [_factory createSource];
}
@end
#import "AFPizza.h"

@interface AFClamPizza : AFPizza
@end

#import "AFClamPizza.h"

@interface AFClamPizza(){
    id<AFPizzaIngredientFactory> _factory;
}
@end

@implementation AFClamPizza

- (instancetype)initWithPizzaIngredientFactory:(id<AFPizzaIngredientFactory>) factory{
    self = [super init];
    if (self){
        _factory = factory;
    }
    return self;
}

- (void)prepare{
    self.clam   = [_factory createClam];
    self.sauce  = [_factory createSource];
}
@end

pizza ingredient factory 抽象

#import <Foundation/Foundation.h>

@class AFCheese;
@class AFClam;
@class AFSauce;

@protocol AFPizzaIngredientFactory
- (AFCheese *)  createCheese;
- (AFClam *)    createClam;
- (AFSauce *)   createSource;
@end

pizza ingredient factory 实现

#import <Foundation/Foundation.h>
#import "AFPizzaIngredientFactory.h"

@interface AFNYPizzaIngredientFactory : NSObject<AFPizzaIngredientFactory>

@end

#import "AFNYPizzaIngredientFactory.h"
#import "AFNYCheese.h"
#import "AFNYClam.h"
#import "AFSauce.h"

@implementation AFNYPizzaIngredientFactory
- (AFCheese *) createCheese{
    return [[AFNYCheese alloc]init];
}
- (AFClam *) createClam{
    return [[AFNYClam alloc]init];
}
- (AFSauce *) createSource{
    return [[AFSauce alloc]init];
}
@end
#import <Foundation/Foundation.h>
#import "AFPizzaIngredientFactory.h"

@interface AFChicagoIngredientFactory : NSObject<AFPizzaIngredientFactory>

@end

#import "AFChicagoIngredientFactory.h"
#import "AFChicagoCheese.h"
#import "AFChicagoClam.h"
#import "AFSauce.h"

@implementation AFChicagoIngredientFactory
- (AFCheese *) createCheese{
    return [[AFChicagoCheese alloc]init];
}
- (AFClam *) createClam{
    return [[AFChicagoClam alloc]init];
}
- (AFSauce *) createSource{
    return [[AFSauce alloc]init];
}
@end

pizza store抽象:

#import <Foundation/Foundation.h>
#import "AFPizza.h"

@protocol AFPizzaStore
- (AFPizza *) createPizza:(PizzaType)pizzaType;
- (AFPizza *) orderPizza:(PizzaType)pizzaType;
@end

pizza store实现:

#import <Foundation/Foundation.h>
#import "AFPizzaStore.h"

@interface AFNYPizzaStore : NSObject<AFPizzaStore>
@end

#import "AFNYPizzaStore.h"
#import "AFNYPizzaIngredientFactory.h"
#import "AFCheesePizza.h"
#import "AFClamPizza.h"

@implementation AFNYPizzaStore
- (AFPizza *) createPizza:(PizzaType)pizzaType{
    AFPizza *pizza = nil;
    AFNYPizzaIngredientFactory *factory = [[AFNYPizzaIngredientFactory alloc]init];
    
    if ( pizzaType == Cheese ){
        pizza = [[AFCheesePizza alloc]initWithPizzaIngredientFactory:factory];
    }else if ( pizzaType == Clam ){
        pizza = [[AFClamPizza alloc]initWithPizzaIngredientFactory:factory];
    }
    
    return pizza;
}
- (AFPizza *) orderPizza:(PizzaType)pizzaType{
    AFPizza *pizza = [self createPizza:pizzaType];
    
    [pizza prepare];
    [pizza bake];
    [pizza cut];
    [pizza box];
    
    return pizza;
}
@end
#import <Foundation/Foundation.h>
#import "AFPizzaStore.h"

@interface AFChicagoPizzaStore : NSObject<AFPizzaStore>

@end

#import "AFChicagoPizzaStore.h"
#import "AFChicagoIngredientFactory.h"
#import "AFCheesePizza.h"
#import "AFClamPizza.h"

@implementation AFChicagoPizzaStore
- (AFPizza *) createPizza:(PizzaType)pizzaType{
    AFPizza *pizza = nil;
    AFChicagoIngredientFactory *factory = [[AFChicagoIngredientFactory alloc]init];
    
    if ( pizzaType == Cheese ){
        pizza = [[AFCheesePizza alloc]initWithPizzaIngredientFactory:factory];
    }else if ( pizzaType == Clam ){
        pizza = [[AFClamPizza alloc]initWithPizzaIngredientFactory:factory];
    }
    
    return pizza;
}
- (AFPizza *) orderPizza:(PizzaType)pizzaType{
    AFPizza *pizza = [self createPizza:pizzaType];
    
    [pizza prepare];
    [pizza bake];
    [pizza cut];
    [pizza box];
    
    return pizza;
}
@end

一些个人总结

1.什么时候会去用工厂模式?(简单工厂,工厂方法,抽象工厂)
为了不再需要直接创建对象,降低代码耦合度。对于创建多种具有同一系列行为的物体,使用工厂方法。对于产品族使用抽象工厂。
2.简单工厂的缺点
简单工厂主要违反了开闭原则:对扩展开放,对修改关闭。添加一种对象,就要去修改简单工厂中的获取对象方法。

参考资料

1.《Objective-C编程之道 iOS设计模式解析》
2.《Head First设计模式》
3.设计模式六大原则
4.设计模式六大原则

上一篇下一篇

猜你喜欢

热点阅读