zoukankan      html  css  js  c++  java
  • 典型重构1 (接口)

    析取类或接口

      将一个类分割为更小、针对性更强的类;从中析取出一系列更精细的接口

    public class InvoiceService
    {
        public string CreateInvoice(Invoice invoice){...}
        public string ProcessPayment(Invoice invoice,double amount){...}
        public double GetAmountOwed(Invoice invoice){...}
        public double GetTotalAmountInvoiceLastFY(Customer customer){...}
        public double GetTotalAmountPaidLastFY(Customer customer){...}
    }

    该类执行了几个相关但却互相分离的功能,供系统的不同部分使用
    通过析取该类的接口,对其进行重构

    public interface IInvoiceCreatingService
    {
        string CreateInvoice(Invoice invoice);
    }
    public interface IInvoicePaymentService
    {
        string ProcessPayment(Invoice invoice,double amount);
        double GetAmountOwed(Invoice invoice);
    }
    public interface IInvoiceReportingService
    {
        double GetTotalAmountInvoiceLastFY(Customer customer);
        double GetTotalAmountPaidLastFY(Customer customer);
    }

    重构结果:

    public class InvoiceService:IInvoiceCreatingService,IInvoicePaymentService,IInvoiceReportingSrvice
    {
        public string CreateInvoice(Invoice invoice){...}
        public string ProcessPayment(Invoice invoice,double amout){...}
        public double GetAmountOwed(Invoice invoice){...}
        public double GetToatlAmountInvoicedLastFY(Customer customer){...}
        public double GetTotalAmountPaidLastFY(Customer customer){...}
    }
  • 相关阅读:
    面向对象之魔术方法
    装饰器和单例模式练习
    装饰器练习
    扩展数据类型练习
    Vlan的 tag 和 untagged
    【转】OSI 七层模型和TCP/IP模型及对应协议(详解)
    性能测试的相关概念和指标。
    Python接口自动化之数据驱动
    Python接口自动化之登录接口测试
    Python接口自动化之unittest单元测试
  • 原文地址:https://www.cnblogs.com/Evelia/p/3493796.html
Copyright © 2011-2022 走看看