c# - 使用多态获取相同类型的实例
问题描述
我有下一个场景:有一个类负责管理应用程序的所有货币。所有货币都扩展了 Currency,因此它可以表现得像它。货币是抽象的,因此无法实例化。在应用程序的某些部分中,我将软货币、硬货币或事件货币作为成本货币,例如,玩家按下某个菜单上的某个购买按钮。这个动作触发了一次购买,购买有货币作为参考,在这个例子中它可以是costSoftCurrency。这个想法是,如果 PlayerCurrencies 收到货币,玩家货币会评估他的货币并返回相同类型的相关货币,然后您可以安全地减去成本。
我的问题是......如果没有这个可怕的如果,我怎么能得到同样的逻辑?我读到了双重调度,但我不知道它是否可以在这里应用。
有谁知道这是否可以实现?
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
}
}
解决方案
我会为此使用工厂方法设计模式,因为它专门用于您使用子类来确定应该创建哪些类的时候。这基本上就是你在这里所做的。我最初的回答提到了反射,因为我在链接到的网站上看到了映射配置,并认为这很酷,但是在昨晚考虑之后,它也有很多开销。使用字典绕过 if 块或 switch 语句会更简单和更快。
这就是我要添加/更改您的代码的内容:
//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
推荐阅读
- sql-server - 如何为负数返回空值?
- django - 使用 Docker heroku.yml 在 Heroku 上部署 Django
- c# - 将包和引导程序添加到先决条件 Visual Studio 2017 sql express 2016
- java - 使用 elasticsearch 搜索最相似的字符串
- rust - 在返回位置具有泛型关联类型的 Impl trait 会导致生命周期错误
- c# - Postman 表单数据到 ASP.NET Core 2.2 控制器未绑定到具有私有设置器的命令
- asp-classic - 是否有适用于 Jetbrains Rider 的 ASP Classic 的运行/调试配置模板?
- python - 如何逐行读取和写入代码输出到文本文件中
- c++ - 如何在多个函数中操作 ifstream 对象
- unix - grep 和 awk,组合命令?