简单工厂模式--加减乘除运算
生活随笔
收集整理的這篇文章主要介紹了
简单工厂模式--加减乘除运算
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面基于簡單的<加減乘除運算>實例來講講實用簡單工廠模式:<備注:以后根據認識的加深,可以添加和修改內容>
需求分析:希望程序提供“加減乘除”四種功能。
功能分析:四種功能都是并列的,相互獨立的。
拓展分析:很有可能拓展新的功能,比如“開根運算”。
如何設計:
1、根據“功能分析”可以將四種功能都歸并一個父類出來,然后創建四個子類繼承它,并且提供空的方法(OC中模擬抽象方法或者虛函數),這四個子類分別都要繼承并重寫實現這個空方法。這樣,一個抽象的父類,四個具體實現的子類,就可以形成多態的應用:父類聲明,子類創建實例。
2、然后創建一個工廠類與功能的抽象父類相互關聯,然后實現根據需求,應用多態來創建功能的實例。
3、拓展:如果需要添加“開根”的功能,只要添加一個繼承運算類的子類,然后在簡單工廠類創建方法中添加修改一點。
附加代碼:
1 #import <Foundation/Foundation.h> 2 3 @interface Operation : NSObject 4 5 @property double a; 6 @property double b; 7 @property double result; 8 -(double)getResult:(double)a second:(double)b; 9 10 @end 11 12 //>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<< 13 14 #import "Operation.h" 15 16 @implementation Operation 17 18 -(double)getResult:(double)a second:(double)b{ 19 self.a = a; 20 self.b = b; 21 return self.a+self.b; 22 }; 23 24 @end 1 #import "Operation.h" 2 3 @interface SubOperation : Operation 4 5 @end 6 7 //>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<< 8 9 #import "SubOperation.h" 10 11 @implementation SubOperation 12 13 -(double)getResult:(double)a second:(double)b{ 14 self.a = a; 15 self.b = b; 16 self.result = self.a - self.b; 17 return self.result; 18 } 19 20 @end 1 #import "Operation.h" 2 3 @interface AddOperation : Operation 4 5 @end 6 7 #import "AddOperation.h" 8 9 @implementation AddOperation 10 11 -(double)getResult:(double)a second:(double)b{ 12 self.a = a; 13 self.b = b; 14 15 self.result = self.a + self.b; 16 return self.result; 17 }; 18 19 @end 1 #import "Operation.h" 2 3 @interface MulOperation : Operation 4 5 @end 6 7 8 #import "MulOperation.h" 9 10 @implementation MulOperation 11 12 -(double)getResult:(double)a second:(double)b{ 13 self.a = a; 14 self.b = b; 15 16 self.result = self.a * self.b; 17 return self.result; 18 } 19 @end 1 #import "Operation.h" 2 3 @interface DivOperation : Operation 4 5 @end 6 7 #import "DivOperation.h" 8 9 @implementation DivOperation 10 11 -(double)getResult:(double)a second:(double)b{ 12 self.a = a; 13 self.b = b; 14 if (self.b == 0) { 15 NSLog(@"除數不能為0"); 16 return 0; 17 } 18 self.result = self.a / self.b; 19 return self.result; 20 } 21 22 @end 1 #import <Foundation/Foundation.h> 2 #import "Operation.h" 3 @interface OperationFactory : NSObject 4 +(Operation*)creatOperation:(NSString*)str; 5 @end 6 7 //>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<< 8 9 #import "OperationFactory.h" 10 #import "AddOperation.h" 11 #import "SubOperation.h" 12 #import "MulOperation.h" 13 #import "DivOperation.h" 14 @implementation OperationFactory 15 16 +(Operation*)creatOperation:(NSString*)str{ 17 Operation *oper = NULL; 18 if ([str isEqual: @"+"]) { 19 oper = [[AddOperation alloc] init]; 20 }else if ([str isEqual: @"-"]) { 21 oper = [[SubOperation alloc] init]; 22 }else if ([str isEqual: @"*"]) { 23 oper = [[MulOperation alloc] init]; 24 }else if ([str isEqual: @"/"]) { 25 oper = [[DivOperation alloc] init]; 26 }else { 27 NSLog(@"輸入無效的內容"); 28 } 29 return oper; 30 } 31 32 @end 1 #import <Foundation/Foundation.h> 2 #import "OperationFactory.h" 3 int main(int argc, const char * argv[]) { 4 @autoreleasepool { 5 // insert code here... 6 Operation *op = [OperationFactory creatOperation:@"+"]; 7 NSLog(@"簡單工廠模式算出結果%f",[op getResult:18 second:8]); 8 Operation *op2 = [OperationFactory creatOperation:@"/"]; 9 NSLog(@"簡單工廠模式算除法%f",[op2 getResult:18 second:0]); 10 NSLog(@"Hello, World!"); 11 } 12 return 0; 13 }運行結果:
總結
以上是生活随笔為你收集整理的简单工厂模式--加减乘除运算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java.util.Date和java.
- 下一篇: Appium环境搭建简介