美文网首页
iOS开发之设计模式 - 简单工厂模式

iOS开发之设计模式 - 简单工厂模式

作者: JoeyM | 来源:发表于2019-03-28 15:49 被阅读0次

    简单工厂模式

    • 简单工厂模式, 解决对象的创建问题,工厂本身包括了多有的方法集合

    实现计算器部分功能

    swift

    import Foundation
    
    // 计算的根类
    class Operation: NSObject {
        
        var numberA: Double?
        var numberB: Double?
        
        public func getResult() -> Double {
            let result: Double  = 0
            return result
        }
    }
    
    // 计算符操作类
    class OperationAdd: Operation {
        
        override func getResult() -> Double {
            var result = 0.0
            result = numberA! + numberB!
            return result
        }
    }
    
    class OperationMulyi: Operation {
        
        override func getResult() -> Double {
            var result = 0.0
            result = numberA! * numberB!
            return result
        }
    }
    /**
     .
     . 多种运算类
     .
     **/
    
    // 简单工厂模式
    class OperationFactory: NSObject {
        
        class func createOperate(operate: String) -> Operation
        {
            var oper = Operation()
            
            switch operate {
            case "+":
                oper = OperationAdd()
            case "*":
                oper = OperationMulyi()
            default:
                break
            }
            
            return oper
        }
    }
    
    // 计算
    var op = Operation()
    op = OperationFactory.createOperate(operate: "+")
    op.numberA = 1.0
    op.numberB = 2.0
    let result = op.getResult()
    

    OC

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    
    // 基类
    @interface Operation : NSObject
    @property (nonatomic, assign, readwrite) double numberA;
    @property (nonatomic, assign, readwrite) double numberB;
    @property (nonatomic, assign, readwrite) double result;
    - (double)getResult;
    @end
    
    @implementation Operation
    
    @end
    
    
    // 子类 +
    @interface  OperationAdd : Operation
    @end
    
    @implementation OperationAdd // 加
    
    - (double)getResult
    {
        double result = 0;
        result = self.numberA + self.numberB;
        return result;
    }
    
    @end
    
    @interface OperationSub: Operation
    
    @end
    
    // 子类 -
    @implementation OperationSub // 减法
    
    - (double)getResult
    {
        double result = 0;
        result = self.numberB - self.numberA;
        return result;
    }
    
    @end
    
    
    // 工厂类
    @interface OperationFactory : NSObject
    + (Operation *)createOperate:(NSString *)operate1;
    @end
    
    @implementation OperationFactory
    
    + (Operation *)createOperate:(NSString *)operate1 {
        Operation *operation = [[Operation alloc] init];
        if ([operate1 isEqualToString:@"+"]) {
            operation = [[OperationAdd alloc] init];
        } else {
            operation = [[OperationSub alloc] init];
        }
        return operation;
    }
    @end
    
    
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        Operation *oper = [Operation new];
        oper = [OperationFactory createOperate:@"+"];
        oper.numberB = 1;
        oper.numberA = 2;
        double result = oper.getResult;
        
        NSLog(@"和为 = %f", result);
        
    }
    
    @end
    
    

    想一下如果这样写代码, 我们又要加上一些其他的操作符,这样仅仅在创建一个类,继承一下Operation,在工厂里面添加一行代码 即可简单创建了, 今后想一下我们是否需要抽出一个抽象类来进行实体运算!

    反思, 如果是项目中, 如何用简单工厂模式?

    下一篇 策略模式

    相关文章

      网友评论

          本文标题:iOS开发之设计模式 - 简单工厂模式

          本文链接:https://www.haomeiwen.com/subject/ufzqbqtx.html