实现一个接口但具有不同的成员名称

时间:2011-07-27 15:00:14

标签: c# .net design-patterns

我有IPrice界面:

public interface IPrice
{
    decimal TaxPercent { get; set; }
    decimal TotalDebt { get; set; }
    decimal Discount { get; set; }
    DiscountTypeEnum DiscountType { get; set; }
    decimal Commission { get; set; }
    DiscountTypeEnum CommissionType { get; set; }
}

我有IExtendPrice接口及其默认实现:

public interface IExtendPrice
{
    decimal TotalDebtWithoutTax { get; }
    decimal TaxSum { get; }
    decimal DiscountSum { get; }
    decimal CommissionSum { get; }
    decimal DebitPrice { get; }
}

public class ExtendPrice : IExtendPrice
    {
        private IPrice m_Price = null;
        public ExtendPrice(IPrice price)
        {
            m_Price = price;
        }

        public decimal TotalDebtWithoutTax { get { return (m_Price.TotalDebt / (1 + (m_Price.TaxPercent / 100))); } }
        public decimal TaxSum { get { return m_Price.TotalDebt - TotalDebtWithoutTax; } }
        public decimal DiscountSum
        {
            get
            {
                decimal discount = m_Price.Discount;
                if (m_Price.DiscountType == DiscountTypeEnum.PERCENTS)
                {
                    discount = discount * NetoPrice / 100;
                }
                return discount;
            }
        }

        public decimal CommissionSum
        {
            get
            {
                decimal commission = m_Price.Commission;
                if (m_Price.CommissionType == DiscountTypeEnum.PERCENTS)
                {
                    commission = commission * NetoPrice / 100;
                }
                return commission;
            }
        }

        public decimal NetoPrice { get { return CalculateNetoPrice(); } }



        private decimal CalculateNetoPrice()
        {
            decimal debitPrice = 0;
            if (m_Price.DiscountType == DiscountTypeEnum.COINS &&
                m_Price.CommissionType == DiscountTypeEnum.COINS)
            {
                //TotalDebtWithoutTax=X-Discount+Commission
                debitPrice = TotalDebtWithoutTax + m_Price.Discount - m_Price.Commission;
            }
            else if (m_Price.DiscountType == DiscountTypeEnum.COINS &&
               m_Price.CommissionType == DiscountTypeEnum.PERCENTS)
            {
                //TotalDebtWithoutTax=X-Discount+Commission*X/100
                debitPrice = (TotalDebtWithoutTax + m_Price.Discount) / (1 + m_Price.Commission / 100);
            }
            else if (m_Price.DiscountType == DiscountTypeEnum.PERCENTS &&
               m_Price.CommissionType == DiscountTypeEnum.COINS)
            {
                //TotalDebtWithoutTax=X-Discount*X/100+Commission
                debitPrice = (TotalDebtWithoutTax - m_Price.Commission) / (1 - m_Price.Discount / 100);
            }
            else if (m_Price.DiscountType == DiscountTypeEnum.PERCENTS &&
               m_Price.CommissionType == DiscountTypeEnum.PERCENTS)
            {
                //TotalDebtWithoutTax=X-Discount*X/100+Commission*X/100
                debitPrice = TotalDebtWithoutTax / (1 - m_Price.Discount / 100 + m_Price.Commission / 100);
            }
            return debitPrice;
        }
    }

我有Invoice,PurchaseInvoice,DeliveryNote等类,每个类: 1.使用其成员实现IPrice。 2.使用ExtendPrice默认实现实现IExtendPrice。 这样的课程看起来像:

public class Invoice : IPrice, IExtendPrice
    {
        public virtual decimal TotalDebt { get; set; }
        public virtual decimal TaxPercent { get; set; }
        public virtual decimal Discount { get; set; }
        public virtual DiscountTypeEnum DiscountType { get; set; }
        public virtual decimal Commission { get; set; }
        public virtual DiscountTypeEnum CommissionType { get; set; }

        private IExtendPrice extendPrice = null;
        public Invoice()
        {
            extendPrice = new ExtendPrice(this);
        }

        public decimal TotalDebtWithoutTax { get { return extendPrice.TotalDebtWithoutTax; } }
        public decimal TaxSum { get { return extendPrice.TaxSum; } }
        public decimal DiscountSum { get { return extendPrice.DiscountSum; } }
        public decimal CommissionSum { get { return extendPrice.CommissionSum; } }
        public decimal DebitPrice { get { return extendPrice.DebitPrice; } }
    }

但是我有一个名为CreditInvoice的课程。它有以下成员:

    public decimal TotalCreditSumWithoutTax
    {
        get { return Math.Round(m_TotalCreditSum / (1 + (m_Tax / 100)), 2); }
    }

与ExtendPrice的TotalDebtWithoutTax相同的实现。区别在于它的名称而不是信用 - 借方/债务。

在CreditInvoice中使用ExtendPrice而不更改其成员名称的最佳做法是什么?

3 个答案:

答案 0 :(得分:13)

为该成员使用explicit interface implementation

class CreditInvoice : IExtendPrice
{
    // ...
    public decimal TotalCreditSumWithoutTax { /* ... */ }


    // Only seen when accessing an instance by its IExtendPrice interface
    decimal IExtendPrice.TotalDebtWithoutTax {
        get { return TotalCreditSumWithoutTax; }
    }
}

答案 1 :(得分:4)

你必须做这样的事情:

public decimal TotalDebtWithoutTax { get { return TotalCreditSumWithoutTax; } }

虽然当你读到它时,听起来好像你的代码中存在一些设计缺陷。


在评论中,您明确表示要实现接口,但不实现名为TotalDebtWithoutTax的方法。那是不可能的。为了实现接口,您需要实现所有方法。

我的猜测是,您对TotalDebtWithoutTax的名称选择太过限制。您可能反对必须使用方法名TotalCreditSumWithoutTax来实现它。信用卡如何成为借记卡?我想你需要概括底层接口以消除这种阻抗。

答案 2 :(得分:1)

您正在寻找Adapter Pattern。这将创建一个实现接口的适配器,然后调用适应类型的现有方法。