首页 > 其他 > 详细

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

时间:2016-03-05 10:08:18      阅读:149      评论:0      收藏:0      [点我收藏+]

转载自 http://www.cnblogs.com/qianLL/p/5230323.html

 

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

先建立一个计算类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;
}
技术分享

技术分享

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

原文:http://www.cnblogs.com/gaomaolin_88/p/5244153.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!