iOS应用设计模式开发中对简单工厂和工厂方法模式的运用

2020-01-14 22:13:39刘景俊
复制代码
#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


实现文件: