前言
自上一篇文章有两个礼拜的时间了,本来想余下一段时间内尽量用英文写文章,包括技术文。前两天点点网的统计数据里添加了对每个博客里文章访问量的一个排名,发现我的博客里访问最多的竟然是Objective C!说实话,有些意外,后细想来也许有很多和我一样的初学者朋友想学习这门语言,犹豫再三,本着分享知识的原则,还是用中文写吧,其他类型的文章再当练习英语,用英文来写。好啦,废话就到这里吧,接下来进入正题。
今天开始,我们来探讨一下用Objective C来简单实现的常见的设计模式。这次想和大家分享的是:简单工厂模式。
严格说来,简单工厂模式不属于GoF的23种设计模式之列,但是作为简单的入门知识,我还是想从简单工厂模式开始说起。
正如此模式的名称一样,简单工厂模式基本上是所有设计模式里最简单的一种,类与类之间的关系一目了然。这次我就用很多地方经常举的例子--计算器,来说明这个模式。首先给大家展示一下类之间的结构图:
通过这张结构图,可以清晰的看到,加法类、减法类、乘法类、除法类继承自运算类,简单工厂类依赖于运算类的实例化来实现相应的运算功能,好的,看起来并不复杂,让我们直接展示一下代码吧(鉴于目前点点不支持Objective C的代码高亮,所以就直接写啦,尽量保持整齐吧。另,为了照顾像我一样基础不是很好的同学,我尽量把代码写全,方便大家调试)。
1 2 3 4 5 6 7 8 9 10 | #import <Foundation/Foundation.h> @interface Operation : NSObject { double numberA; double numberB; } @property double numberA; @property double numberB; -( double ) GetResult; @end |
1 2 3 4 5 6 7 8 9 10 | #import"Operation.h" @implementation Operation @synthesize numberA, numberB; -( double ) GetResult{ return -1.0; //此处默认返回-1.0,无其他意义 } @end |
1 2 3 4 | #import "Operation.h" @interface OperationAdd:Operation @end |
1 2 3 4 5 6 7 8 9 10 11 | #import "OperationAdd.h" @implementation OperationAdd -( double ) GetResult{ double result =0; result =numberA+numberB; return result; } @end |
1 2 3 | #import "Operation.h" @interface OperationSub:Operation @end |
1 2 3 4 5 6 7 8 9 10 11 | #import "OperationSub.h" @implementation OperationSub -( double )GetResult{ double result =0; result = numberA-numberB; return result; } @end |
1 2 3 | #import "Operation.h" @interface OperationMul:Operation @end |
1 2 3 4 5 6 7 8 9 10 11 | #import "OperationMul.h" @implementation OperationMul -( double )GetResult{ double result =0; result = numberA*numberB; return result; } @end |
1 2 3 4 | #import "Operation.h" @interface OperationDiv:Operation @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #import "OperationDiv.h" @implementation OperationDiv -( double )GetResult{ double result =0; @try { result = numberA/numberB; } @catch ( NSException *exception) { NSLog (@ "除数不能为0" ); } return result; } @end |
1 2 3 4 5 6 7 8 9 | #import <Foundation/Foundation.h> #import "OperationAdd.h" #import "OperationDiv.h" #import "OperationSub.h" #import "OperationMul.h" @interface OperationFactory: NSObject +(Operation*)CreateOperate:( char )operate; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #import "OperationFactory.h" @implementation OperationFactory +(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; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #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语句判别需要实例化哪种类型,之后进行计算,获取结果。
That's all!下次继续吧!