简单工厂模式
正如此模式的名称一样,简单工厂模式基本上是所有设计模式里最简单的一种,类与类之间的关系一目了然。这次我就用很多地方经常举的例子--计算器,来说明这个模式。首先给大家展示一下类之间的结构图:
通过这张结构图,可以清晰的看到,加法类、减法类、乘法类、除法类继承自运算类,简单工厂类依赖于运算类的实例化来实现相应的运算功能,好的,看起来并不复杂,让我们直接展示一下代码吧(鉴于目前点点不支持Objective C的代码高亮,所以就直接写啦,尽量保持整齐吧。另,为了照顾像我一样基础不是很好的同学,我尽量把代码写全,方便大家调试)。
注意:本文所有代码均在ARC环境下编译通过。
首先是运算类(父类):
接口文件:
@interface Operation :NSObject{
double numberA;
double numberB;
}
@property double numberA;
@property double numberB;
-(double) GetResult;
@end
@implementation Operation
@synthesize numberA, numberB;
-(double) GetResult{
return -1.0; //此处默认返回-1.0,无其他意义
}
@end
@interface OperationAdd:Operation
@end
@implementation OperationAdd
-(double) GetResult{
double result =0;
result =numberA+numberB;
return result;
}
@end
@implementation OperationSub
-(double)GetResult{
double result =0;
result = numberA-numberB;
return result;
}
@end
@implementation OperationMul
-(double)GetResult{
double result =0;
result = numberA*numberB;
return result;
}
@end
@interface OperationDiv:Operation
@end
@implementation OperationDiv
-(double)GetResult{
double result =0;
@try{
result = numberA/numberB;
}
@catch(NSException *exception) {
NSLog(@"除数不能为0");
}
return result;
}
@end
@interface OperationFactory:NSObject
+(Operation*)CreateOperate:(char)operate;
@end
+(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;
}
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;
}
工厂方法模式
上面关于简单工厂模式中就有提到过一次关于“工厂类”模式。为了帮助大家能够回忆一下简单工厂模式,在这里提一下简单工厂模式的优点,简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。其实,工厂方法模式是简单工厂模式的进一步抽象和推广。由于使用了多态性,工厂方法模式保持了简单工厂模式的优点,而且克服了它的缺点。但缺点是,由于每加一个产品,就需要加一个产品工厂的类,增加了额外的开发量。
下面还是以计算器为例子,详细介绍工厂方法模式,还是老样子,先向大家展示一下类结构图。
上面这张图向大家展示了各个类之间的关系。其实和简单工厂模式不同的是,类图的右边抽象工厂接口是相比简单工厂模式多出来的抽象接口。
下面直接上代码吧,别的不多说了。
注意:本文所有代码均在ARC环境下编译通过。
Operation类接口
@interface Operation :NSObject{
double numberA;
double numberB;
}
@property double numberA;
@property double numberB;
-(double) GetResult;
@end
@implementation Operation
@synthesize numberA, numberB;
-(double) GetResult{
return -1.0;
}
@end
@interface OperationAdd :Operation
@end
@implementation OperationAdd
-(double) GetResult{
double result =0;
result = numberA+numberB;
return result;
}
@end
@interface OperationDiv :Operation
@end
@implementation OperationDiv
-(double)GetResult{
double result =0;
@try{
result = numberA/numberB;
}
@catch(NSException *exception) {
NSLog(@"除数不能为0");
}
return result;
}
@end
@interface OperationMul :Operation
@end
OperationMul类实现
#import "OperationMul.h"
@implementation OperationMul
-(double)GetResult{
double result =0;
result = numberA*numberB;
return result;
}
@end
@interface OperationSub :Operation
@end
@implementation OperationSub
-(double)GetResult{
double result =0;
result = numberA-numberB;
return result;
}
@end
#import "Operation.h"
@interface IFactory :NSObject
-(Operation*)CreateOperation;
@end
@implementation IFactory
-(Operation*)CreateOperation{
return [[Operation alloc]init];
}
@end
@interface AddFactory :IFactory
@end
@implementation AddFactory
-(Operation*)CreateOperation{
return [[OperationAdd alloc]init];
}
@end
@interface SubFactory :IFactory
@end
@implementation SubFactory
-(Operation*)CreateOperation{
return [[OperationSub alloc]init];
}
@end
@interface MulFactory :IFactory
@end
@implementation MulFactory
-(Operation*)CreateOperation{
return [[OperationMul alloc]init];
}
@end
@interfaceDiv Factory :IFactory
@end
@implementation DivFactory
-(Operation*)CreateOperation{
return [[OperationDiv alloc]init];
}
@end
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;
}
新闻热点
疑难解答