iOS应用设计模式开发中对简单工厂和工厂方法模式的运用
简单工厂模式
正如此模式的名称一样,简单工厂模式基本上是所有设计模式里最简单的一种,类与类之间的关系一目了然。这次我就用很多地方经常举的例子--计算器,来说明这个模式。首先给大家展示一下类之间的结构图:
通过这张结构图,可以清晰的看到,加法类、减法类、乘法类、除法类继承自运算类,简单工厂类依赖于运算类的实例化来实现相应的运算功能,好的,看起来并不复杂,让我们直接展示一下代码吧(鉴于目前点点不支持Objective C的代码高亮,所以就直接写啦,尽量保持整齐吧。另,为了照顾像我一样基础不是很好的同学,我尽量把代码写全,方便大家调试)。
注意:本文所有代码均在ARC环境下编译通过。
首先是运算类(父类):
接口文件:
#import <Foundation/Foundation.h>
@interface Operation :NSObject{
double numberA;
double numberB;
}
@property double numberA;
@property double numberB;
-(double) GetResult;
@end
实现文件:
#import"Operation.h"
@implementation Operation
@synthesize numberA, numberB;
-(double) GetResult{
return -1.0; //此处默认返回-1.0,无其他意义
}
@end
加法类(运算子类):
接口文件:
#import "Operation.h"
@interface OperationAdd:Operation
@end
实现文件:
#import "OperationAdd.h"
@implementation OperationAdd
-(double) GetResult{
double result =0;
result =numberA+numberB;
return result;
}
@end
减法类(运算子类):
接口文件:
#import "Operation.h"
@interface OperationSub:Operation
@end
实现文件:
#import "OperationSub.h"
@implementation OperationSub
-(double)GetResult{
double result =0;
result = numberA-numberB;
return result;
}
@end
乘法类(运算子类)
#import "Operation.h"
@interface OperationMul:Operation
@end
实现文件:
#import "OperationMul.h"
@implementation OperationMul
-(double)GetResult{
double result =0;
result = numberA*numberB;
return result;
}
@end
除法类(运算子类):
接口文件:
#import "Operation.h"
@interface OperationDiv:Operation
@end
实现文件:
#import "OperationDiv.h"
@implementation OperationDiv
-(double)GetResult{
double result =0;
@try{
result = numberA/numberB;
}
@catch(NSException *exception) {
NSLog(@"除数不能为0");
}
return result;
}
@end
下面是工厂类(依赖实力化运算类实现具体功能):
接口文件:
#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "OperationDiv.h"
#import "OperationSub.h"
#import "OperationMul.h"
@interface OperationFactory:NSObject
+(Operation*)CreateOperate:(char)operate;
@end
实现文件:
#import "OperationFactory.h"
+(Operation*)CreateOperate:(char)operate{
Operation *oper;
switch(operate) {
case '+':
oper = [[OperationAdd alloc]init];
break;
case '-':
oper = [[OperationSub alloc]init];
break;
case '*':
oper = [[OperationMul alloc]init];
break;
case '/':
oper = [[OperationDiv alloc]init];
break;
default:
oper = nil;
break;
}
return oper;
}
具体调用
#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "OperationDiv.h"
#import "OperationMul.h"
#import "OperationSub.h"
#import "OperationFactory.h"
int main (int argc,const char* argv[])
{
@autoreleasepool{
Operation *oper = [OperationFactory CreateOperate:'*'];
[oper setNumberA:1];
[oper setNumberB:2];
double result = 0;
result = [oper GetResult];
NSLog(@"Result is %f", result);
}
return 0;
}
好啦,上面罗列的是简单工厂模式的基础代码。其实还是挺简单的,对吧,只有一层继承关系,一个依赖关系,在工厂类里面用switch语句判别需要实例化哪种类型,之后进行计算,获取结果。
工厂方法模式
上面关于简单工厂模式中就有提到过一次关于“工厂类”模式。为了帮助大家能够回忆一下简单工厂模式,在这里提一下简单工厂模式的优点,简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。其实,工厂方法模式是简单工厂模式的进一步抽象和推广。由于使用了多态性,工厂方法模式保持了简单工厂模式的优点,而且克服了它的缺点。但缺点是,由于每加一个产品,就需要加一个产品工厂的类,增加了额外的开发量。
下面还是以计算器为例子,详细介绍工厂方法模式,还是老样子,先向大家展示一下类结构图。
上面这张图向大家展示了各个类之间的关系。其实和简单工厂模式不同的是,类图的右边抽象工厂接口是相比简单工厂模式多出来的抽象接口。
下面直接上代码吧,别的不多说了。
注意:本文所有代码均在ARC环境下编译通过。
Operation类接口
#import <Foundation/Foundation.h>
@interface Operation :NSObject{
double numberA;
double numberB;
}
@property double numberA;
@property double numberB;
-(double) GetResult;
@end
Operation类实现
#import "Operation.h"
@implementation Operation
@synthesize numberA, numberB;
-(double) GetResult{
return -1.0;
}
@end
OperationAdd类接口
#import "Operation.h"
@interface OperationAdd :Operation
@end
OperationAdd类实现
#import "OperationAdd.h"
@implementation OperationAdd
-(double) GetResult{
double result =0;
result = numberA+numberB;
return result;
}
@end
OperationDiv类接口
#import "Operation.h"
@interface OperationDiv :Operation
@end
OperationDiv类实现
#import "OperationDiv.h"
@implementation OperationDiv
-(double)GetResult{
double result =0;
@try{
result = numberA/numberB;
}
@catch(NSException *exception) {
NSLog(@"除数不能为0");
}
return result;
}
@end
OperationMul类接口
#import "Operation.h"
@interface OperationMul :Operation
@end
OperationMul类实现
#import "OperationMul.h"
@implementation OperationMul
-(double)GetResult{
double result =0;
result = numberA*numberB;
return result;
}
@end
OperationSub类接口
#import "Operation.h"
@interface OperationSub :Operation
@end
OperationSub类实现
#import "OperationSub.h"
@implementation OperationSub
-(double)GetResult{
double result =0;
result = numberA-numberB;
return result;
}
@end
IFactory类接口
#import <Foundation/Foundation.h>
#import "Operation.h"
@interface IFactory :NSObject
-(Operation*)CreateOperation;
@end
IFactory类实现
#import "IFactory.h"
@implementation IFactory
-(Operation*)CreateOperation{
return [[Operation alloc]init];
}
@end
AddFactory类接口
#import "IFactory.h"
@interface AddFactory :IFactory
@end
AddFactory类实现
#import "AddFactory.h"
#import "OperationAdd.h"
@implementation AddFactory
-(Operation*)CreateOperation{
return [[OperationAdd alloc]init];
}
@end
SubFactory类接口
#import "IFactory.h"
@interface SubFactory :IFactory
@end
SubFactory类实现
#import "SubFactory.h"
#import "OperationSub.h"
@implementation SubFactory
-(Operation*)CreateOperation{
return [[OperationSub alloc]init];
}
@end
MulFactory类接口
#import "IFactory.h"
@interface MulFactory :IFactory
@end
MulFactory类实现
#import "MulFactory.h"
#import "OperationMul.h"
@implementation MulFactory
-(Operation*)CreateOperation{
return [[OperationMul alloc]init];
}
@end
DivFactory类接口
#import "IFactory.h"
@interfaceDiv Factory :IFactory
@end
DivFactory类实现
#import "DivFactory.h"
#import "OperationDiv.h"
@implementation DivFactory
-(Operation*)CreateOperation{
return [[OperationDiv alloc]init];
}
@end
Main方法调用
#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "AddFactory.h" //加法工厂,你可以根据需要添加其他运算工厂
int main (int argc,const char* argv[])
{
@autoreleasepool{
IFactory *operFactory = [[AddFactory alloc]init];
Operation *oper = [operFactory CreateOperation];
[oper setNumberA:1];
[oper setNumberB:2];
double result = [oper GetResult];
NSLog(@"The result is %f", result);
}
return 0;
}
好啦,上面就是工厂方法模式的Objective C的类代码。
相关文章
IOS AFNetworking的Post失败及requestSerializer的正确使用
这篇文章主要介绍了IOS AFNetworking的Post失败及requestSerializer的正确使用的相关资料,需要的朋友可以参考下2017-05-05
最新评论