aar*_*ona 2 c# design-patterns
我正在接管一个项目的代码,我在多个类中看到了一堆复制的代码.主人对重构这段代码非常厌倦,但我想出了一个听起来不错的想法.
鉴于:
多个"客户端"类没有完全相同的接口但相当接近:
class Client1
{
public static string FunctionA(a);
public static string FunctionB(a, b, c);
public static string FunctionC(a, b);
}
class Client2
{
public static string FunctionA(a);
public static string FunctionB(a, b, c);
public static string FunctionC(a, b);
}
class Client3
{
public static string FunctionA(a);
public static string FunctionC(a, b);
public static string FunctionD();
}
... etc
Run Code Online (Sandbox Code Playgroud)
让我们说这FunctionA是每个类中完全相同的功能.所有者以某种方式认为这个功能需要在每个类中,因为"未来对于另一个客户可能会有所不同"(fyi:FunctionA将标准时间转换为军事时间......所以我非常怀疑这一点).
他的每个客户端都有一个特殊的代码(即文件中的"abc"或"xyz"),web.config因此当访问客户端的代码时,使用类似于此的代码获取正确的行为:
public static string FunctionA(string a)
{
switch(getClientCode())
{
case "abc":
return Client1.FunctionA(a);
case "xyz":
return Client2.FunctionA(a);
case "def":
return Client3.FunctionA(a);
default:
throw new Exception("code not supported");
}
}
Run Code Online (Sandbox Code Playgroud)
让我们知道,我绝不认为这是理想的.我实际上和我的客户(谁拥有这个代码)在一些关于他对这个项目做出的决定的热烈讨论中进行了斗争,所以不要射击信使.
我的客户认为这种做事方式很有用,比如说,我想实现一个新客户端,我可以运行应用程序并执行一些步骤,直到找到并"修复"这些抛出的异常.这就是所有者喜欢这种方式设置代码的原因.但是,每个客户端类中大约一半的功能对于每个客户端是相同的,或者对于大约80%的客户端它们是相同的.
我问他为什么他没有抽象类,原因是不是每个派生类都需要或应该实现任何基类函数.此外,所有公共函数都是静态的(并且没有成员变量),因此实例化对象没有意义.
我的模式:
使用上面的客户端类,我想实现这样的东西:
class Client1
{
// FunctionA is the same for each class
//public static string FunctionA(a);
public static string FunctionB(string a, string b, string c);
// Client1 and Client3 share the same code.
//public static string FunctionC(a, b);
}
class Client2
{
// FunctionA is the same for each class
//public static string FunctionA(a);
public static string FunctionB(string a, string b, string c);
public static string FunctionC(string a, string b);
}
class Client3
{
// FunctionA is the same for each class
//public static string FunctionA(a);
// Client1 and Client3 share the same code.
//public static string FunctionC(a, b);
public static string FunctionD();
}
... etc.
class DefaultClient
{
public static string FunctionA(string a);
public static string FunctionB(string a, string b, string c);
public static string FunctionC(string a, string b);
}
class ProxyUtility
{
private static string getClientCode();
public static string FunctionA(string a)
{
switch (getClientCode())
{
case "abc":
case "def":
case "xyz":
return DefaultClient.FunctionA(a);
default:
throw new Exception("code not supported");
}
}
public static string FunctionB(string a, string b, string c)
{
switch (getClientCode())
{
case "abc":
case "xyz":
return DefaultClient.FunctionB(a, b, c);
case "def":
return string.Empty; // or throw an exception since they don't support this
default:
throw new Exception("code not supported");
}
}
public static string FunctionC(string a, string b)
{
switch (getClientCode())
{
case "abc":
case "def":
return DefaultClient.FunctionC(a, b);
case "xyz":
return Client2.FunctionC(a, b);
default:
throw new Exception("code not supported");
}
}
public static string FunctionD()
{
switch (getClientCode())
{
case "abc":
case "xyz":
return string.Empty; // or throw an exception since they don't support this function.
case "def":
return Client3.FunctionD();
default:
throw new Exception("code not supported");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个流程图,以了解其工作原理:
