使用多态获取相同类型的实例

Get Instance of the same type using polymorphism

我有下一个场景: 有一个 class 负责管理应用程序的所有货币。所有货币都扩展了 Currency,所以它可以表现得像它。货币是抽象的,因此无法实例化。 在应用程序的某些部分,我将软货币、硬货币或事件货币作为成本货币,例如,玩家按下某些菜单上的某个购买按钮。此操作触发购买,购买有货币作为参考,在本例中可以是 costSoftCurrency。 这个想法是,如果 PlayerCurrencies 收到货币,玩家货币会评估他的货币和 return 相同类型的相关货币,然后您可以安全地减去成本。

我的问题是...如果没有这个可怕的 ifs,我如何才能获得相同的逻辑?我读到 double dispatch 但我不知道它是否可以在这里应用。

有谁知道这是否可以实现?

public class PlayerCurrencies
{
    public SoftCurrency softCurrency = new SoftCurrency();
    public HardCurrency hardCurrency = new HardCurrency();
    public EventCurrency eventCurrency = new EventCurrency();

    public Currency GetCurrencyFromType(Currency currency)
    {
        if (currency is SoftCurrency)
        {
            return this.softCurrency;
        }

        if (currency is HardCurrency)
        {
            return this.hardCurrency;
        }

        if (currency is EventCurrency)
        {
            return this.eventCurrency;
        }

        return null;
    }
}

public abstract class Currency
{
    public float Value
    {
        get;
        set;
    }

    public void Add(Currency currency)
    {
        this.Value += currency.Value;
    }

    public void Substract(Currency currency)
    {
        this.Value -= currency.Value;
    }
}

public class EventCurrency : Currency
{

}

public class HardCurrency : Currency
{

}

public class SoftCurrency : Currency
{

}

internal class Program
{
    private static void Main(string[] args)
    {
        PlayerCurrencies playerCurrencies = new PlayerCurrencies();

        Currency costSoftCurrency = new SoftCurrency();
        Currency costHardCurrency = new HardCurrency();

        playerCurrencies.GetCurrencyFromType(costSoftCurrency).Substract(costSoftCurrency); // there i get a SoftCurrency from Player Currencies 
        playerCurrencies.GetCurrencyFromType(costHardCurrency).Substract(costHardCurrency); // there i get a HardCurrency from Player Currencies 
    }
}

我会为此使用工厂方法设计模式,因为它专门用于您使用 sub类 来确定应该创建什么 类 的时候。这基本上就是你在这里所做的。我最初的回答提到了反射,因为我在 link 访问的网站上看到了映射配置并认为这很酷,但在昨晚考虑之后它也有很多开销。使用字典绕过 if 块或 switch 语句会更简单、更快捷。

这就是我 add/change 对您的代码的看法:

//The Factory class that creates the different types of currency based
//on a name parameter
static class CurrencyFactory
{
    private static readonly Dictionary<string, Currency> _currencyDictionary =
        new Dictionary<string, Currency>
    {
        { "HardCurrency", new HardCurrency() },
        { "SoftCurrency", new SoftCurrency() },
        { "EventCurrency", new EventCurrency() }
    };

    public static Currency Create(string currencyTypeName)
    {
        return _currencyDictionary[currencyTypeName];
    }
}

internal class Program
{
    private static void Main(string[] args)
    {
        //This class will no longer be needed as it is redundant
        PlayerCurrencies playerCurrencies = new PlayerCurrencies();

        var costSoftCurrency = CurrencyFactory.Create("softCurrency");
        var costHardCurrency = CurrencyFactory.Create("hardCurrency");

        //I wasn't sure of your goals here so I kept it as is,
        //but as I said above, playerCurrencies won't be needed.
        playerCurrencies.GetCurrencyFromType(costSoftCurrency)
            .Substract(costSoftCurrency); // there i get a SoftCurrency from Player Currencies 

        playerCurrencies.GetCurrencyFromType(costHardCurrency)
            .Substract(costHardCurrency); // there i get a HardCurrency from Player Currencies 
    }
}

您可以在此处找到更详尽的示例和对模式的精彩解释:https://dev.to/gary_woodfine/how-to-use-factory-method-design-pattern-in-c-3ia3

你可以用 pattern matching:

public class PlayerCurrencies
{
    public SoftCurrency Soft = new SoftCurrency();
    public HardCurrency Hard = new SoftCurrency();
    public EventCurrency Event = new EventCurrency();

    public Currency GetCurrencyFromType(Currency currency)
    {
        switch (currency)
        {
            case EventCurrency: return Event;
            case HardCurrency: return Hard;
            case SoftCurrency: return Soft;
        }
        return null;
    }
}

我将字段的名称更改为 HardSoftEvent,以便更容易将它们与具有相同名称的类型区分开来。

在这个特定的示例中,传递 currency 参数并仅将其用于其类型似乎很奇怪。可能有另一种方法可以解决您的特定问题。但是至于不用多条if语句怎么办,那就是怎么办。

您最常看到的是使用声明的变量进行演示,如下所示:

switch (currency)
{
    case EventCurrency ec : return Event;
    case HardCurrency hc: return Hard;
    case SoftCurrency sc: return Soft;
}
return null;

例如,如果 currency 的类型是 EventCurrency 那么在 case 的范围内,ec 将是 currency 参数转换作为 EventCurrency。但由于在此示例中您没有使用该值,因此不需要变量。