您的位置:首页 > 移动开发 > Objective-C

使用Object-C实现23种设计模式之抽象工厂模式

2016-05-20 10:08 429 查看
前面介绍了用OC怎么实现工厂方法模式(如果对工厂方法模式还不了解的请先参考我的上一篇博客http://blog.csdn.net/u013054715/article/details/51452667),那么今天,在工厂方法模式的基础上,继续介绍一下抽象方法模式。

首先,先说一下工厂方法模式与抽象工厂方法的不同之处,首先,工厂方法模式只有一个抽象工厂类和一个抽象产品类,可以在这一个抽象工厂类和抽象产品类的基础上实现多个具体工厂类和多个具体产品类,而抽象工厂模式则可以有一个抽象工厂类以及多个抽象产品类,工厂方法模式在工厂内部强调怎么生产,而抽象工厂模式在工厂内部则强调生产什么。

在上一篇博客中,提到的工厂方法模式的例子,在某某电子科技公司,生产有电脑和手机两款产品,则分别划分两个车间(工厂),对应生产这两种产品,这里理所应当的运用工厂方法模式,而这节课,依旧使用这个案例,暂且先假定该工厂之前仅仅取得了Sony品牌的代理生产资格,整个工厂仅仅生产Sony品牌的电脑、手机,现在随着公司规模的日益扩大,公司的到了联想公司授权的生产资格,现在该公司同时具有了Sony以及Lenovo两个品牌的生产资格,由于两种品牌施工技术上的差异性,仅仅一件电脑生产车间和一间手机生产车间已经不能满足公司当前的需求了,公司整体规模有待改善。

经过董事会的集体商议,最终确定,将整个公司车间在整体上一分为二,分别为Sony品牌生产车间以及Lenovo品牌生产车间,Sony品牌的手机和电脑在Sony生产车间进行生产,Lenovo品牌的手机和电脑在Lenovo车间进行生产,在两个车间内部,分别都有手机生产线以及电脑生产线,两者之间互不干涉,这种模式就是抽象工厂模式。

下面便用这个实例讲解一下抽象工厂模式具体怎么使用Object-C进行实现:

同工厂方法一般,首先明确该公司生产什么——Sony手机、Sony电脑、Lenovo手机、Lenovo电脑四类产品,然而这四类又可以抽象为电脑产品及手机产品两大类,所以,抽象产品类的构建,就从PhoneProduct类和ComputerProduct类开始。

PhoneProduct.h

#import <Foundation/Foundation.h>

@protocol PhoneProduct <NSObject>

@required

-(void)introduce;

@end

ComputerProduct.h

#import <Foundation/Foundation.h>

@protocol ComputerProduct <NSObject>

@required

-(void)introduce;

@end

有了这两个大类之后,再去考虑具体的生产Sony的那些产品,生产Lenovo的那些产品,这便是具体累的实现:

SonyPhone.h

#import <Foundation/Foundation.h>

#import "PhoneProduct.h"

@interface SonyPhone :
NSObject<PhoneProduct>

@end
SonyPhone.m

#import "SonyPhone.h"

@implementation SonyPhone

-(void)introduce{

    NSLog(@"I am SonyPhone!");

}

@end
SonyComputer.h

#import <Foundation/Foundation.h>

#import "ComputerProduct.h"

@interface SonyComputer :
NSObject<ComputerProduct>

@end

SonyComputer.m

#import "SonyComputer.h"

@implementation SonyComputer

-(void)introduce{

    NSLog(@"I am SonyComputer!");

}

@end
LenovoPhone.h

#import <Foundation/Foundation.h>

#import "PhoneProduct.h"

@interface LenovoPhone :
NSObject<PhoneProduct>

@end

LenovoPhone.m

#import "LenovoPhone.h"

@implementation LenovoPhone

-(void)introduce{

    NSLog(@"I am LenovoPhone!");

}

@end
LenovoComputer.h

#import
<Foundation/Foundation.h>

#import
"ComputerProduct.h"

@interface LenovoComputer :
NSObject<ComputerProduct>

@end
LenovoComputer.m

#import "LenovoComputer.h"

@implementation LenovoComputer

-(void)introduce{

    NSLog(@"I am LenovoComputer!");

}

@end
接下来,再去考虑工厂车间的设计,首先定义一个总的工厂协议(接口):

Factory.h

之后,抽象问题具体化,设计实现LenovoFactory类和SonyFactory类:

LeovoFactory.h

#import <Foundation/Foundation.h>

#import "Factory.h"

@interface LenovoFactory :
NSObject<Factory>

@end
LeovoFactory.m

#import "LenovoFactory.h"

#import "LenovoPhone.h"

#import "LenovoComputer.h"

@implementation LenovoFactory

-(NSObject<PhoneProduct> *)createPhone{

    return [LenovoPhonealloc];

}

-(NSObject<ComputerProduct> *)createComputer{

    return [LenovoComputeralloc];

}

@end
SonyFactory.h

#import <Foundation/Foundation.h>

#import "Factory.h"

@interface SonyFactory :
NSObject<Factory>

@end
SonyFactory.m

#import
"SonyFactory.h"

#import "SonyPhone.h"

#import "SonyComputer.h"

@implementation SonyFactory

-(NSObject<PhoneProduct> *)createPhone{

    return [SonyPhonealloc];

}

-(NSObject<ComputerProduct> *)createComputer{

    return [SonyComputeralloc];

}

@end
现在抽象工厂模式已经实现好了,接下来静等工厂开工就好了。

>>>>>>>>>>>>>>>>>>开工<<<<<<<<<<<<<<<<<<<<<<<<<

#import <Foundation/Foundation.h>

#import "SonyFactory.h"

#import "LenovoFactory.h"

#import "PhoneProduct.h"

#import "ComputerProduct.h"

int main(int argc,constchar * argv[]) {

    @autoreleasepool {

        

        LenovoFactory* lenovoFactory = [LenovoFactoryalloc];

        SonyFactory* sonyFactory = [SonyFactoryalloc];

        NSObject<PhoneProduct>* lenovoPhone = [lenovoFactorycreatePhone];

        [lenovoPhone introduce];

        NSObject<ComputerProduct>* lenovoComputer = [lenovoFactorycreateComputer];

        [lenovoComputer introduce];

        NSObject<PhoneProduct>*sonyPhone = [sonyFactorycreatePhone];

        [sonyPhone introduce];

        NSObject<ComputerProduct>* sonyComputer = [sonyFactorycreateComputer];

        [sonyComputer introduce];

    }

    return 0;

}

下面展示一下运行结果:

2016-05-20 09:53:06.292 设计模式之二抽象工厂模式[1255:51615] I am LenovoPhone!

2016-05-20 09:53:06.293 设计模式之二抽象工厂模式[1255:51615] I am LenovoComputer!

2016-05-20 09:53:06.293 设计模式之二抽象工厂模式[1255:51615] I am SonyPhone!

2016-05-20 09:53:06.293 设计模式之二抽象工厂模式[1255:51615] I am SonyComputer!

Program ended with exit code: 0

如果公司继续发展,再次获得其他品牌的生产资格,只需要在现有基础上增加产品类和相对应品牌的生产车间即可,不需要对现有模式进行改变,只需要对其进行扩展,这也完全符合设计模式的开关原则。

以上是我个人对抽象工厂模式的简单理解,如有不到之处,敬请各位补充。
未完待续。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息