我有一个类:A这是一个综合了一些更小的类,的B,C和D。
B,C和D实现接口IB,IC和ID分别。
由于A支持的所有功能B,C并且D,A器具IB,IC并ID为好,但不幸的是这导致了大量的重路由在执行A
像这样:
interface IB
{
int Foo {get;}
}
public class B : IB
{
public int Foo {get {return 5;}}
}
interface IC
{
void Bar();
}
public class C : IC
{
public void Bar()
{
}
}
interface ID
{
string Bash{get; set;}
}
public class D: ID
{
public string Bash {get;set;}
}
class A : IB, IC, ID
{
private B b = new B();
private C c = new C();
private D d = new D();
public int Foo {get {return b.Foo}}
public A(string bash)
{
Bash = bash;
}
public void Bar()
{
c.Bar();
}
public string Bash
{
get {return d.Bash;}
set {d.Bash = value;}
}
}
有没有办法摆脱任何样板重定向A?B,C并D都实现了不同,但共同的功能,我这样的A工具IB,IC而且ID因为这意味着我可以通过A自己的匹配这些接口的依赖,而不必暴露内部的辅助方法。
A落实IB,IC并且ID是感觉比较明智的写入Person.Walk(),而不是Person.WalkHelper.Walk(),例如。