zoukankan      html  css  js  c++  java
  • 典型重构2 (方法)

    析取方法

    public string PlaceOrderForWidgets(int quantity,string customerNumber)
    {
        var invoice=new Invoice    
      { InvoiceNumber
    =Guid.NewGuid().ToString(), TotalPrice=PricePerWidget*quantity, Quantity=quantity
      };
    var customer=_customerService.GetCustomer(customerNumber); invoice.CustomerName=customer.CustomerName; invoice.CustormerAddress=customer.CustormerAddress; invoice.CustomerBillingInformation=customer.CustomerBillingInformation; double tax; switch(invoice.CustormerAddress.State.ToUpper()) { case "OH": tax=invoice.TotalPrice*.15; break; case "MI": tax=invoice.TotalPrice*.22; break; case "NV": tax=invoice.TotalPrice*.05; break; default: tax=0.0; break; } var shippingPrice=invoice.TotalPrice*.1; invoice.TotalPrice+=shippingPrice; invoice.TotalPrice+=tax; var parmentAuthorizationCode=_paymentProcessingService.ProcessPayment(invoice.TotalPrice,customer.CustomerBillingInformation); invoice.Approved= !string.IsNullOrEmpty(paymentAuthorizationCode); _invoiceService.Post(invoice); return invoice.InvoiceNumber; }

    析取计算订单中税负的代码

    pirvate double CalculateTaxForInvoice(Invoice invoice)
    {
        double tax;
        switch(invoice.CustormerAddress.State.ToUpper())
        {
        case "OH": tax=invoice.TotalPrice*.15;
            break;
        case "MI": tax=invoice.TotalPrice*.22;
            break;
        case "NV": tax=invoice.TotalPrice*.05;
            break;
        default: tax=0.0;
            break;
        }
        return tax;    
    }

     析取创建并填充Invoice对象的代码
         修改调用ProcessPayment方法的代码

    var paymentAuthorizationCode=_paymentProcessingService.ProcessPayment(invoice.ToatalPrice,invoice.CustomerBillingInformation);
    //提取发货单功能
    private
    Invoice GetInvoice(int quantity,string customerNumber) { var invoice=new Invoice { InvoiceNumber=Guid.NewGuid().ToString(), TotalPrice=PricePerWidget*quantity, Quantity=quantity }; var customer=_customerService.GetCustomer(customerNumber); invoice.CustomerName=customer.CustomerName; invoice.CustormerAddress=customer.CustormerAddress; invoice.CustomerBillingInformation=customer.CustomerBillingInformation; return invoice; }

    重构结果(暂时):

    public string PlaceOrderForWidgets(int quantity,string customerNumber)
    {
        var invoice= GetInvoice(quantity,customerNumber);
        var tax=CalculateTaxForInvoice(invoice);
        var shippingPrice=invoice.TotalPrice*.1;
        invoice.TotalPrice+=shippingPrice;
        invoice.TotalPrice+=tax;
        var paymentAuthorizationCode=_paymentProcessingService.ProcessPayment(invoice.ToatalPrice,invoice.CustomerBillingInformation);
        invoice.Approved= !string.IsNullOrEmpty(paymentAuthorizationCode);
        _invoiceService.Post(invoice);
        return invoice.InvoiceNumber;
    }
  • 相关阅读:
    jquery 删除cookie失效的解决方法
    SQL Server 抛出自定义异常,由C#程序俘获之并进行相应的处理
    SqlServer中的自增的ID的最后的值:
    Stream/Bytes[]/Image对象相互转化
    TextBox禁止复制粘贴和数字验证,小数验证,汉字验证
    扩展WPF的DataGrid按方向键移动焦点
    WPF 中获取DataGrid 模板列中控件的对像
    IIS设置文件 App_Offline.htm 网站维护
    IIS设置文件 Robots.txt 禁止爬虫
    js中的整除运算
  • 原文地址:https://www.cnblogs.com/Evelia/p/3494085.html
Copyright © 2011-2022 走看看