简单工厂模式--加减乘除运算

学习来自《大话设计模式》

下面基于简单的<加减乘除运算>实例来讲讲实用简单工厂模式:<备注:以后根据认识的加深,可以添加和修改内容>

 

需求分析:希望程序提供“加减乘除”四种功能。

功能分析:四种功能都是并列的,相互独立的。

拓展分析:很有可能拓展新的功能,比如“开根运算”。

 

如何设计:

1、根据“功能分析”可以将四种功能都归并一个父类出来,然后创建四个子类继承它,并且提供空的方法(OC中模拟抽象方法或者虚函数),这四个子类分别都要继承并重写实现这个空方法。这样,一个抽象的父类,四个具体实现的子类,就可以形成多态的应用:父类声明,子类创建实例。

2、然后创建一个工厂类与功能的抽象父类相互关联,然后实现根据需求,应用多态来创建功能的实例。

3、拓展:如果需要添加“开根”的功能,只要添加一个继承运算类的子类,然后在简单工厂类创建方法中添加修改一点。

 附加代码:

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Operation : NSObject
 4 
 5 @property double a;
 6 @property double b;
 7 @property double result;
 8 -(double)getResult:(double)a second:(double)b;
 9 
10 @end
11 
12 //>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<
13 
14 #import "Operation.h"
15 
16 @implementation Operation
17 
18 -(double)getResult:(double)a second:(double)b{
19     self.a = a;
20     self.b = b;
21     return self.a+self.b;
22 };
23 
24 @end
Operation抽象父类
 1 #import "Operation.h"
 2 
 3 @interface SubOperation : Operation
 4 
 5 @end
 6 
 7 //>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<
 8 
 9 #import "SubOperation.h"
10 
11 @implementation SubOperation
12 
13 -(double)getResult:(double)a second:(double)b{
14     self.a = a;
15     self.b = b;
16     self.result = self.a - self.b;
17     return self.result;
18 }
19 
20 @end
减法类 继承 Operation抽象父类
 1 #import "Operation.h"
 2 
 3 @interface AddOperation : Operation
 4 
 5 @end
 6 
 7 #import "AddOperation.h"
 8 
 9 @implementation AddOperation
10 
11 -(double)getResult:(double)a second:(double)b{
12     self.a = a;
13     self.b = b;
14 
15     self.result = self.a + self.b;
16     return self.result;
17 };
18 
19 @end
加法类 继承 Operation抽象父类
 1 #import "Operation.h"
 2 
 3 @interface MulOperation : Operation
 4 
 5 @end
 6 
 7 
 8 #import "MulOperation.h"
 9 
10 @implementation MulOperation
11 
12 -(double)getResult:(double)a second:(double)b{
13     self.a = a;
14     self.b = b;
15 
16     self.result = self.a * self.b;
17     return self.result;
18 }
19 @end
乘法类 继承 Operation抽象父类
 1 #import "Operation.h"
 2 
 3 @interface DivOperation : Operation
 4 
 5 @end
 6 
 7 #import "DivOperation.h"
 8 
 9 @implementation DivOperation
10 
11 -(double)getResult:(double)a second:(double)b{
12     self.a = a;
13     self.b = b;
14     if (self.b == 0) {
15         NSLog(@"除数不能为0");
16         return 0;
17     }
18     self.result = self.a / self.b;
19     return self.result;
20 }
21 
22 @end
除法类 继承 Operation抽象父类
 1 #import <Foundation/Foundation.h>
 2 #import "Operation.h"
 3 @interface OperationFactory : NSObject
 4 +(Operation*)creatOperation:(NSString*)str;
 5 @end
 6 
 7 //>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<
 8 
 9 #import "OperationFactory.h"
10 #import "AddOperation.h"
11 #import "SubOperation.h"
12 #import "MulOperation.h"
13 #import "DivOperation.h"
14 @implementation OperationFactory
15 
16 +(Operation*)creatOperation:(NSString*)str{
17     Operation *oper = NULL;
18     if ([str  isEqual: @"+"]) {
19         oper = [[AddOperation alloc] init];
20     }else if ([str isEqual: @"-"]) {
21         oper = [[SubOperation alloc] init];
22     }else if ([str isEqual: @"*"]) {
23         oper = [[MulOperation alloc] init];
24     }else if ([str isEqual: @"/"]) {
25         oper = [[DivOperation alloc] init];
26     }else {
27         NSLog(@"输入无效的内容");
28     }
29     return oper;
30 }
31 
32 @end
OperationFactory 依赖 Operation类
 1 #import <Foundation/Foundation.h>
 2 #import "OperationFactory.h"
 3 int main(int argc, const char * argv[]) {
 4     @autoreleasepool {
 5         // insert code here...
 6         Operation *op = [OperationFactory creatOperation:@"+"];
 7         NSLog(@"简单工厂模式算出结果%f",[op getResult:18 second:8]);
 8         Operation *op2 = [OperationFactory creatOperation:@"/"];
 9         NSLog(@"简单工厂模式算除法%f",[op2 getResult:18 second:0]);
10         NSLog(@"Hello, World!");
11     }
12     return 0;
13 }
main方法

运行结果:

 

 

 

 

 

 
 
 
 
 
 
 
 
posted @ 2015-08-07 16:38  何杨  阅读(1248)  评论(0编辑  收藏  举报