zoukankan      html  css  js  c++  java
  • 大话设计模式之简单的工厂模式

    第一章:代码无错就是优-简单的工厂模式

    先建立一个计算类Operation

    Operation.h文件

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

     Operation.m文件

    @implementation Operation
    
     
    
    @end

     然后分别创建他们的子类Add,Sub,Mul,Div

    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(@"除数不能为0");
           }
               return self.numberA/self.numberB;
    }
    @end

    计算的工厂类OperationFactory.h

    #import <Foundation/Foundation.h>

    #import "Add.h"

    #import "Sub.h"

    #import "Mul.h"

    #import "Div.h"

    @interface OperationFactory : NSObject

    -(Operation *)createOperate:(char)opreate;

    @end

    OperationFactory.m

    #import "OperationFactory.h"
    
    @implementation OperationFactory
    -(Operation *)createOperate:(char)opreate{
        Operation *oper;
        switch (opreate) {
            case '+':
                oper=[[Add alloc]init];
                break;
            case '-':
                oper=[[Sub alloc]init];
             
                break;
                
            case '*':
                oper=[[Mul alloc]init];
              
                   break;
                
            case '/':
                oper=[[Div alloc]init];
             
                break;
        }
        return oper;
    }
    @end

    主函数

    #import <Foundation/Foundation.h>
    #import "OperationFactory.h"
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            OperationFactory *factory=[[OperationFactory alloc]init];
            Operation *oper=[factory createOperate:'+'];
            oper.numberA=100;
            oper.numberB=200;
            NSLog(@"%f",[oper opera]);
        }
            return 0;
    }

  • 相关阅读:
    1366. Rank Teams by Votes
    1361. Validate Binary Tree Nodes
    1359. Count All Valid Pickup and Delivery Options
    1358. Number of Substrings Containing All Three Characters
    JQuery跳出each循环的方法(包含数组遍历)【转】
    JS数组转字符串(3种方法)【转】
    js的15种循环遍历,你掌握了几种【转】
    js/jQuery获取data-*属性值【转】
    Thinkphp volist 多重循环原样输出数组key值的使用总结【转】
    php 循环【转】
  • 原文地址:https://www.cnblogs.com/qianLL/p/5230323.html
Copyright © 2011-2022 走看看