星空网 > 软件开发 > 操作系统

大话设计模式之简单的工厂模式

第一章:代码无错就是优-简单的工厂模式

先建立一个计算类Operation

Operation.h文件

@interface Operation : NSObject@property(nonatomic,assign)double numberA;@property(nonatomic,assign)double numberB;@end

 

 Operation.m文件

@implementation Operation @end

 

 然后分别创建他们的子类Add,Sub,Mul,Div

Add.h

#import "Operation.h" @interface Add : Operation-(double)OperationAdd;@end

 

 

Add.m

#import "Add.h" @implementation Add-(double)OperationAdd{  return self.numberA+self.numberB;}
@end

Sub.h

#import "Operation.h"@interface Sub : Operation-(double)operaSub;@end

Sub.m

#import "Sub.h"@implementation Sub- (double)operaSub{   return self.numberA-self.numberB;}@end

Mul.h

#import "Operation.h"@interface Mul : Operation-(double)OperaMul;@end

Mul.m

#import "Mul.h"@implementation Mul-(double)OperaMul{  return self.numberA*self.numberB;}@end

Div.h

#import "Operation.h"@interface Div : Operation-(double)OperaDiv;@end

Div.m

#import "Div.h"@implementation Div-(double)OperaDiv{//  double result=0.0;  if (self.numberB==0) {      NSLog(@"除数不能为0");    }      return self.numberA/self.numberB;}@end

计算的工厂类OperationFactory.h

#import <Foundation/Foundation.h>#import "Add.h"#import "Sub.h"#import "Mul.h"#import "Div.h"#import "Operation.h"@interface OperationFactory : NSObject@property(nonatomic,strong)Add *add;@property(nonatomic,strong)Sub *sub;@property(nonatomic,strong)Mul *mul;@property(nonatomic,strong)Div *div;-(double)createOperate:(char)opreate;@end

OperationFactory.m

#import "OperationFactory.h"@implementation OperationFactory-(double)createOperate:(char)opreate{   double result = 0.0;    switch (opreate) {    case '+':     result= self.add.OperationAdd;             break;    case '-':        result=self.sub.operaSub;      break;          case '*':        result=self.mul.OperaMul;        break;          case '/':        result=self.div.OperaDiv;      break;  }  return result;}@end

主函数

#import <Foundation/Foundation.h>#import "OperationFactory.h"int main(int argc, const char * argv[]) {  @autoreleasepool {    double result;    OperationFactory *factory=[[OperationFactory alloc]init];    Add *ad=[Add new];    ad.numberA=10;    ad.numberB=6;    factory.add=ad;     //    Div *di=[Div new];//    di.numberA=21;//    di.numberB=0;//    factory.div=di;////    result =[factory createOperate:'/'];        result=[factory createOperate:'+'];        NSLog(@"%lf",result);  }    return 0;}

大话设计模式之简单的工厂模式images/loading.gif' data-original="http://images2015.cnblogs.com/blog/818241/201603/818241-20160301112952283-2035632336.png" />




原标题:大话设计模式之简单的工厂模式

关键词:设计模式

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

巴基斯坦节日大全:https://www.goluckyvip.com/tag/28662.html
巴基斯坦空派:https://www.goluckyvip.com/tag/28663.html
巴基斯坦空运专线:https://www.goluckyvip.com/tag/28664.html
巴基斯坦快递:https://www.goluckyvip.com/tag/28665.html
巴基斯坦陆运:https://www.goluckyvip.com/tag/28666.html
巴基斯坦日:https://www.goluckyvip.com/tag/28667.html
川藏线自驾游要怎么走才比较划算呢?:https://www.vstour.cn/a/411240.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流