【问题标题】:Known ways of mixing a parent class and a child class code in the one method [closed]在一种方法中混合父类和子类代码的已知方法[关闭]
【发布时间】:2021-05-18 22:33:27
【问题描述】:

代码示例:

public class BaseCustomer
{
    public long GetBalance()
    {
        var result = default(long);

        // 1. some BigBatch1 of code that identical for Wallet and Merchant

        // 2.A. Wallet specified code if GetBalance called for Wallet

        // 2.B. Merchant specified code if GetBalance called for Merchant

        // 3. some BigBatch2 of code that identical for Wallet and Merchant

        return result;
    }
}

public class Merchant : BaseCustomer { }

public class Wallett : BaseCustomer { }

....

void Main(){
    var wallet = new Wallet();
    var merchant = new Merchant();
            
    Console.WriteLine($"Wallet balance: {wallet.GetBalance()}");
    Console.WriteLine($"Merchant balance: {merchant.GetBalance()}");
}

Wallet 和 Merchant 都有 GetBalance 方法。该方法包含许多相同的两个类代码(参见上面代码中的 cmets 1. 和 3.)。此外,GetBalance 方法的某些部分对于 Wallet(评论 2.A.)和 Merchant(评论 2.B.)有所不同。此外,可能会有更多相同和不同代码的混合。

任务:GetBalance 应该适用于 Wallet 和 Merchant。

我正在尝试找到所有已知的方法来解决该任务。 目前,我知道以下几种方式:

1) 将 BaseCustomer.GetBalance 标记为抽象并在 Wallet 和 Merchant 中实现。优点:它会起作用,缺点:BigBatch1 和 BigBatch2 将在 Wallet 和 Merchant 中翻倍:

public abstract class BaseCustomer
{
    public abstract long GetBalance();
}

public class Wallet : BaseCustomer
{
    public override long GetBalance()
    {
        var result = default(long);

        // 1. some BigBatch1 of code that identical for Wallet and Merchant

        // 2.A. Wallet specified code

        // 3. some BigBatch2 of code that identical for Wallet and Merchant

        return result;
    }
}

public class Merchant : BaseCustomer
{
    public override long GetBalance()
    {
        var result = default(long);

        // 1. some BigBatch1 of code that identical for Wallet and Merchant

        // 2.B. Merchant specified code

        // 3. some BigBatch2 of code that identical for Wallet and Merchant

        return result;
    }
}

2) 选项 1 + 实现 BigBatch1 和 BigBatch2 作为 BaseCustomer 中的方法。优点:可以,不会加倍代码,缺点:BigBatch1 和 BigBatch2 的方法本身没有任何意义,它们的签名可能非常庞大,可能有 BigBatch3/4/5 等代码片段。强>

public abstract class BaseCustomer
{
    public abstract long GetBalance();

    protected BigBatch1OutParams DoBigBatch1(BigBatch1InParams @params) { /* some BigBatch1 of code that identical for Wallet and Merchant */ }

    protected BigBatch2OutParams DoBigBatch2(BigBatch2InParams @params) { /* some BigBatch2 of code that identical for Wallet and Merchant */ }
}

public class Wallet : BaseCustomer
{
    public override long GetBalance()
    {
        var result = default(long);

        var bb1 = DoBigBatch1(new BigBatch1InParams { ... });

        // 2.A. Wallet specified code if GetBalance called for Wallet

        var bb2 = DoBigBatch2(new BigBatch2InParams { ... });
        
        // some code

        return result;
    }
}

public class Merchant : BaseCustomer 
{
    public override long GetBalance()
    {
        var result = default(long);

        var bb1 = DoBigBatch1(new BigBatch1InParams { ... });

        // 2.B. Merchant specified code if GetBalance called for Merchant

        var bb2 = DoBigBatch2(new BigBatch2InParams { ... });

        // some code

        return result;
    }
}

3) 放置 2.A.钱包指定代码和 2.B. BaseCustomer 抽象方法中商户指定代码。优点:可以,缺点:和第2点一样,可能有DoSpecifiedWork1/2/3等代码片段。

public abstract class BaseCustomer
{
    public long GetBalance()
    {
        var result = default(long);

        // 1. some BigBatch1 of code that identical for Wallet and Merchant

        var t = DoSpecifiedWork(new ClassSpecifiedInParams { ... });

        // 3. some BigBatch2 of code that identical for Wallet and Merchant

        return result;
    }

    protected abstract ClassSpecifiedOutParams DoSpecifiedWork(ClassSpecifiedInParams @params);
}

public class Wallet : BaseCustomer
{
    protected override ClassSpecifiedOutParams DoSpecifiedWork(ClassSpecifiedInParams @params)
    {
        // 2.A. Wallet specified code
    }
}

public class Merchant : BaseCustomer
{
    protected override ClassSpecifiedOutParams DoSpecifiedWork(ClassSpecifiedInParams @params)
    {
        // 2.B. Merchant specified code
    }
}

您还知道哪些其他选项?也许,通过额外的抽象级别或使用某种模式可以更优雅地实现任务?我将不胜感激一些代码示例。

【问题讨论】:

  • 如果您将 GetBalance 声明为虚拟而不是抽象,那么您可以在派生类的实现中调用 base.GetBalance() 吗?
  • BaseCustomer.GetBalanse 没有意义
  • “我正在努力寻找所有已知的方法”——这几乎就是 Stack Overflow 范式中“需要专注”的定义。
  • @Sergey 为什么不呢?您可以将“钱包和商家的代码相同”放在那里。
  • 客户不是一种钱包,钱包也不是一种客户。所以在这里使用继承的选择对我来说似乎真的很奇怪。也许您需要一个具有余额的第三个实体——“帐户”。然后钱包和客户都可以拥有一个返回帐户的属性,然后您可以从中获取余额。

标签: c# oop design-patterns


【解决方案1】:

这听起来像是Template Method pattern 的案例:

public abstract class BaseCustomer
{
    public long GetBalance()
    {
        var result = default(long);

        var bb1 = BigBatch1();
        var specificResult = DoSpecificThing();
        var bb2 = BigBatch2();

        return result;
    }

    protected abstract Foo DoSpecificThing();

    private BB1 BigBatch1()
    {
        // ....
    }

    private BB2 BigBatch2()
    {
        // ...
    }
}

由于BaseCustomer 是abstract,Merchant 和Wallet 必须实现DoSpecificThing:

public class Merchant : BaseCustomer
{
    protected override Foo DoSpecificThing()
    {
        // ...
    }
}

public class Wallett : BaseCustomer
{
    protected override Foo DoSpecificThing()
    {
        // ...
    }
}

Main 方法与 OP 中的方法相同:

void Main(){
    var wallet = new Wallet();
    var merchant = new Merchant();
            
    Console.WriteLine($"Wallet balance: {wallet.GetBalance()}");
    Console.WriteLine($"Merchant balance: {merchant.GetBalance()}");
}

我承认我没有在 C# 编辑器中编写或编译过上面的代码,所以可能有错别字。

【讨论】:

  • 与问题中的第 3 点相同
【解决方案2】:

把共享代码放在基类中?

    public abstract class BaseCustomer
    {
        public void CommonCode() => Console.WriteLine("I'm common");
        public virtual long CusomCode() => 0;

        public long GetBalance()
        {
            CommonCode();
            return 1 + CusomCode();
        }
    }

    public class Wallet : BaseCustomer
    {
        public override long CusomCode() => 2;
    }

    public class Merchant : BaseCustomer
    {
        public override long CusomCode() => 3;
    }

【讨论】:

  • 问题是通用代码和特定代码混合在一起。代码是这样的,在 GetBalance 方法中,首先执行通用代码,然后执行特定代码,然后再次执行通用代码,然后必须调用特定代码。
  • @Sergey 更新了答案
  • 同题中的第3点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
  • 2017-05-24
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
相关资源
最近更新 更多