iOS设计模式 - (3)简单工厂模式 by Colin丶
转载请注明出处: http://blog.csdn.net/hitwhylz/article/details/40381721
简单工厂模式(FACTORY),通过面向对象的封装,继承和多态来降低程序的耦合度。将一个具体类的实例化交给一个静态工厂方法来执行。
该模式中的角色包括:
- (IBAction)getResult:(id)sender { //得到三个文本输入框的内容 NSString* strFirstNum = self.FirstNumTextField.text; NSString* strSecondNum = self.SecondNumTextField.text; NSString* strOperation = self.OperationTextField.text; //进行运算操作 if ([strOperation isEqualToString:@"+"]) { NSLog(@"+"); double result = [strFirstNum doubleValue]+[strSecondNum doubleValue]; self.ResultTextField.text = [NSString stringWithFormat:@"%f",result]; } else if([strOperation isEqualToString:@"-"]) { NSLog(@"-"); double result = [strFirstNum doubleValue]-[strSecondNum doubleValue]; self.ResultTextField.text = [NSString stringWithFormat:@"%f",result]; } else if([strOperation isEqualToString:@"*"]) { NSLog(@"*"); double result = [strFirstNum doubleValue]*[strSecondNum doubleValue]; self.ResultTextField.text = [NSString stringWithFormat:@"%f",result]; } else if([strOperation isEqualToString:@"/"]) { NSLog(@"/"); //判断除数不能为0 if ([strSecondNum isEqualToString:@"0"]) { NSLog(@"除数不能为0"); UIAlertView* tempAlert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"除数不能为0" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; [tempAlert show]; } else { double result = [strFirstNum doubleValue]/[strSecondNum doubleValue]; self.ResultTextField.text = [NSString stringWithFormat:@"%f",result]; } } }
#import <Foundation/Foundation.h> /*! * 操作方法协议接口 * * @since V1.0 */ @protocol OperationProtocol <NSObject> -(double)getResult; @end
#import <Foundation/Foundation.h> #import "OperationProtocol.h" /*! * 操作方法父类 * * @since V1.0 */ @interface Operation : NSObject<OperationProtocol> @property double firstNum;//第一个操作数 @property double secondNum;//第二个操作数 @end
#import "Operation.h" /*! * 加法实现类 * * @since V1.0 */ @interface OperationAdd : Operation @end
#import "OperationAdd.h" @implementation OperationAdd -(double)getResult { double result = 0; result = self.firstNum+self.secondNum; return result; } @end
#import <Foundation/Foundation.h> #import "Operation.h" #import "OperationAdd.h" #import "OperationSub.h" #import "OperationMultiply.h" #import "OperationDivide.h" /*! * 操作工厂类 * * @since V1.0 */ @interface OperationFactory : NSObject //获得操作对象 +(Operation*)createOperate:(NSString*)operateStr; @end
#import "OperationFactory.h" @implementation OperationFactory +(Operation*)createOperate:(NSString*)operateStr { Operation* oper = nil; if ([operateStr isEqualToString:@"+"]) { oper = [[OperationAdd alloc] init]; } else if ([operateStr isEqualToString:@"-"]) { oper = [[OperationSub alloc] init]; } else if ([operateStr isEqualToString:@"*"]) { oper = [[OperationMultiply alloc] init]; } else if ([operateStr isEqualToString:@"/"]) { oper = [[OperationDivide alloc] init]; } return oper; } @end
- (IBAction)clickingOperation:(id)sender { NSString* strFirstNum = self.firstNumTextField.text; NSString* strSecondNum = self.secondNumTextField.text; Operation* oper; oper = [OperationFactory createOperate:self.operationTextField.text]; oper.firstNum = [strFirstNum doubleValue]; oper.secondNum = [strSecondNum doubleValue]; self.resultTextField.text = [NSString stringWithFormat:@"%f",[oper getResult]]; }