简单工厂模式
正如此模式的名称一样,简单工厂模式基本上是所有设计模式里最简单的一种,类与类之间的关系一目了然。这次我就用很多地方经常举的例子--计算器,来说明这个模式。首先给大家展示一下类之间的结构图:
通过这张结构图,可以清晰的看到,加法类、减法类、乘法类、除法类继承自运算类,简单工厂类依赖于运算类的实例化来实现相应的运算功能,好的,看起来并不复杂,让我们直接展示一下代码吧(鉴于目前点点不支持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的类代码。