我希望你能帮助我解决问题:
我有一个进行SOAP调用的类。但是,如果SOAP定义发生更改,我将不得不编写一个新的类或继承它等等。 因此,我想到了编写如下内容的解决方案:
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}
但我无法避免在我的代码中使用"if"或switch语句。 是否可以使用面向对象技术来解决这个问题?
编辑: GetSoapVersion函数应该是静态的。