使用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
如果公司继续发展,再次获得其他品牌的生产资格,只需要在现有基础上增加产品类和相对应品牌的生产车间即可,不需要对现有模式进行改变,只需要对其进行扩展,这也完全符合设计模式的开关原则。
以上是我个人对抽象工厂模式的简单理解,如有不到之处,敬请各位补充。
未完待续。。。
首先,先说一下工厂方法模式与抽象工厂方法的不同之处,首先,工厂方法模式只有一个抽象工厂类和一个抽象产品类,可以在这一个抽象工厂类和抽象产品类的基础上实现多个具体工厂类和多个具体产品类,而抽象工厂模式则可以有一个抽象工厂类以及多个抽象产品类,工厂方法模式在工厂内部强调怎么生产,而抽象工厂模式在工厂内部则强调生产什么。
在上一篇博客中,提到的工厂方法模式的例子,在某某电子科技公司,生产有电脑和手机两款产品,则分别划分两个车间(工厂),对应生产这两种产品,这里理所应当的运用工厂方法模式,而这节课,依旧使用这个案例,暂且先假定该工厂之前仅仅取得了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
如果公司继续发展,再次获得其他品牌的生产资格,只需要在现有基础上增加产品类和相对应品牌的生产车间即可,不需要对现有模式进行改变,只需要对其进行扩展,这也完全符合设计模式的开关原则。
以上是我个人对抽象工厂模式的简单理解,如有不到之处,敬请各位补充。
未完待续。。。
相关文章推荐
- PropertyChangeListener简单理解
- 什么是设计模式
- 设计模式之创建型模式 - 特别的变量问题
- 七、设计模式——装饰模式
- 设计模式总结
- 设计模式之创建型模式
- 浅谈设计模式的学习
- Ruby设计模式编程之适配器模式实战攻略
- 实例讲解Ruby使用设计模式中的装饰器模式的方法
- 设计模式中的模板方法模式在Ruby中的应用实例两则
- Ruby设计模式编程中对外观模式的应用实例分析
- 实例解析Ruby设计模式编程中Strategy策略模式的使用
- Ruby中使用设计模式中的简单工厂模式和工厂方法模式
- Ruby使用设计模式中的代理模式与装饰模式的代码实例
- 详解组合模式的结构及其在Ruby设计模式编程中的运用
- C#编程中使用设计模式中的原型模式的实例讲解
- 使用设计模式中的工厂方法模式进行C#编程的示例讲解
- 实例解析C#设计模式编程中简单工厂模式的使用
- 详解C#设计模式编程中生成器模式的使用
- 深入解析C#设计模式编程中对建造者模式的运用