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

2020-01-14 22:13:39刘景俊

#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的类代码。