【发布时间】:2011-05-17 19:24:19
【问题描述】:
希望你能帮我解决我的问题:
我有一个班级正在做肥皂电话。但是如果肥皂定义发生变化,我将不得不编写一个新类或从它继承等。 所以我找到了解决方案来写这样的东西:
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 应该是静态的
【问题讨论】:
标签: oop design-patterns strategy-pattern