簡單工廠模式(FACTORY),通過面向對象的封裝,繼承和多態來降低程序的耦合度。將一個具體類的實例化交給一個靜態工廠方法來執行。
該模式中的角色包括:
工廠類(Simple Factory): 只包含了創建具體類的靜態方法。抽象產品(Product):定義簡單工廠中要返回的產品。具體產品(ConcreteProduct):具體產品。我們用一張類圖描述這個模式。這裡, 客戶類和工廠類分開。消費者任何時候需要某種產品,只需向工廠請求即可。消費者無須修改就可以接納新產品。ProductA、ProductB和ProductC繼承自Product虛擬類,Show方法是不同產品的自描述;Factory依賴於ProductA、ProductB和ProductC,Factory根據不同的條件創建不同的Product對象。
- (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/*! * 操作方法協議接口 * * @since V1.0 */ @protocol OperationProtocol -(double)getResult; @end
#import#import OperationProtocol.h /*! * 操作方法父類 * * @since V1.0 */ @interface Operation : NSObject @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#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]]; }