Big talk design model: simple factory model; big talk design model factory

Source: Internet
Author: User

Big talk design model: simple factory model; big talk design model factory

Chapter 1: code error-free Optimization-simple factory Mode

Create a computing class Operation first

Operation. h file

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

 

Operation. m file

@implementation Operation @end

 

Create their subclass Add, Sub, Mul, and Div respectively.

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 (@ "the divisor cannot be 0");} return self. numberA/self. numberB;} @ end

Computing factory class 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

Main Function

#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;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.