我希望你能帮我解决我的问题:
我有一个班级做肥皂电话。但是,如果肥皂定义发生变化,我将不得不编写一个新类或从它继承等。所以我找到了解决方案来编写类似的东西:
switch(version)
{
case "1.0":
saopV1.getData()
case "2.0":
soapV2.getData()
}
好吧,代码很糟糕,我知道。然后我读到了策略模式,我想,哇,这就是我需要摆脱这个糟糕的 switch-case 的东西:
abstract SoapVersion
{
public SoapVersion GetSoapVersion(string version)
{
//Damn switch-case thing
//with return new SoapV1() and return new SoapV2()
}
public string[] virtual getData()
{
//Basic Implementation
}
}
class SoapV1:SoapVersion
{
public override string[] getData()
{
//Detail Implementation
}
}
class SoapV2:SoapVersion
{//the same like soapv1}
但我无法避免在我的代码中使用“ifs”或 switch case。这可能使用 OO 技术吗?
编辑:GetSoapVersion-Function 应该是静态的