如何将基础 class 重新设计为通用
How to re-design base class to generic
这是来自 TeamA
的基本代码
public class BaseClass
{
public Method1();
public Method2();
Other methods...
}
public class A : BaseClass
{
public override Method1();
public override Method2();
}
public class B : BaseClass
{
public override Method1();
public override Method2();
}
public class C : BaseClass
{
public override Method1();
public override Method2();
}
TeamB 必须自定义以上代码,代码必须如下:
public class BaseClass1 : BaseClass
{
public override Method1();
}
public class A1 : A
//There is a big problem here,
//if A1 inherit A, then cannot get changes from BaseClass1
//if A1 inherit BaseClass1, then cannot get changes from A
//Same issue to class B1, C1, D1...
{
public override Method1();
}
public class B1 : B
{
public override Method1();
}
public class C1 : C
{
public override Method1();
}
所有其他团队将仅使用来自 TeamB 的代码,他们可以轻松地做到这些:
public class BaseClass2 : BaseClass1
{
public override Method1();
}
public class A2 : A1
//Same problem here with TeamB
{
public override Method1();
}
public class B2 : B1
{
public override Method1();
}
public class C2 : C1
{
public override Method1();
}
问题很明确:除了TeamA,不管是哪个队,一旦我们要自定义BaseClass,就得自定义所有的类,就是太重了!
我的问题是:是否有一种方法可以从 TeamA 重新设计代码结构,使代码通用,然后我们就不必进行大量定制?
你的问题不是 100% 清楚,但我猜这是一种情况,你有一个核心产品工程团队 (TeamA) 来生成平台代码,还有一个顾问或 "professional services" 团队然后获取产品并为客户实施它。专业服务团队希望能够修改内容,但也能够从产品工程中获取补丁或版本升级,而无需进行代码合并。 (否则,简单的解决方案是就地修改而不是扩展和覆盖。)
如果是这种情况,简单的解决方案是让 TeamA 提供一个 pass-through 基础 class 供 TeamB 用作扩展点。
//This is what TeamA provides in a release
public class BaseClass
{
public Method1();
public Method2();
}
public class CustomBaseClass : BaseCLass
{
//No code. This is just a pass-through class
}
public class A : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class B : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class C : CustomBaseClass
{
public override Method1();
public override Method2();
}
那么当TeamB需要自定义一些关于base的东西时class,他们只需要在一个地方做,其他的都不需要改变:
//This is how it looks when TeamB is done with it
public class BaseClass
{
public Method1();
public Method2();
}
public class CustomBaseClass : BaseCLass
{
public override Method1(); //customized now, and will affect all existing classes automatically
}
public class A : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class B : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class C : CustomBaseClass
{
public override Method1();
public override Method2();
}
当 TeamB 需要从 TeamA 获取产品版本时,他们可以保留 CustomBaseClass(必须有一个仅用于自定义的协议)并更新其他所有内容。这样自定义 forward-compatible 并且不需要合并,只需进行一些测试。
//This is how it looks when TeamA provides a new release with a new method
public class BaseClass
{
public Method1();
public Method2();
public NewMethod();
}
public class CustomBaseClass : BaseCLass
{
public override Method1(); //customized now, and will affect all existing classes automatically
}
public class A : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class B : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class C : CustomBaseClass
{
public override Method1();
public override Method2();
}
在此示例中,TeamA 更改了基础 class,而 TeamB 无需费力即可保留其自定义设置。
这是来自 TeamA
的基本代码public class BaseClass
{
public Method1();
public Method2();
Other methods...
}
public class A : BaseClass
{
public override Method1();
public override Method2();
}
public class B : BaseClass
{
public override Method1();
public override Method2();
}
public class C : BaseClass
{
public override Method1();
public override Method2();
}
TeamB 必须自定义以上代码,代码必须如下:
public class BaseClass1 : BaseClass
{
public override Method1();
}
public class A1 : A
//There is a big problem here,
//if A1 inherit A, then cannot get changes from BaseClass1
//if A1 inherit BaseClass1, then cannot get changes from A
//Same issue to class B1, C1, D1...
{
public override Method1();
}
public class B1 : B
{
public override Method1();
}
public class C1 : C
{
public override Method1();
}
所有其他团队将仅使用来自 TeamB 的代码,他们可以轻松地做到这些:
public class BaseClass2 : BaseClass1
{
public override Method1();
}
public class A2 : A1
//Same problem here with TeamB
{
public override Method1();
}
public class B2 : B1
{
public override Method1();
}
public class C2 : C1
{
public override Method1();
}
问题很明确:除了TeamA,不管是哪个队,一旦我们要自定义BaseClass,就得自定义所有的类,就是太重了!
我的问题是:是否有一种方法可以从 TeamA 重新设计代码结构,使代码通用,然后我们就不必进行大量定制?
你的问题不是 100% 清楚,但我猜这是一种情况,你有一个核心产品工程团队 (TeamA) 来生成平台代码,还有一个顾问或 "professional services" 团队然后获取产品并为客户实施它。专业服务团队希望能够修改内容,但也能够从产品工程中获取补丁或版本升级,而无需进行代码合并。 (否则,简单的解决方案是就地修改而不是扩展和覆盖。)
如果是这种情况,简单的解决方案是让 TeamA 提供一个 pass-through 基础 class 供 TeamB 用作扩展点。
//This is what TeamA provides in a release
public class BaseClass
{
public Method1();
public Method2();
}
public class CustomBaseClass : BaseCLass
{
//No code. This is just a pass-through class
}
public class A : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class B : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class C : CustomBaseClass
{
public override Method1();
public override Method2();
}
那么当TeamB需要自定义一些关于base的东西时class,他们只需要在一个地方做,其他的都不需要改变:
//This is how it looks when TeamB is done with it
public class BaseClass
{
public Method1();
public Method2();
}
public class CustomBaseClass : BaseCLass
{
public override Method1(); //customized now, and will affect all existing classes automatically
}
public class A : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class B : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class C : CustomBaseClass
{
public override Method1();
public override Method2();
}
当 TeamB 需要从 TeamA 获取产品版本时,他们可以保留 CustomBaseClass(必须有一个仅用于自定义的协议)并更新其他所有内容。这样自定义 forward-compatible 并且不需要合并,只需进行一些测试。
//This is how it looks when TeamA provides a new release with a new method
public class BaseClass
{
public Method1();
public Method2();
public NewMethod();
}
public class CustomBaseClass : BaseCLass
{
public override Method1(); //customized now, and will affect all existing classes automatically
}
public class A : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class B : CustomBaseClass
{
public override Method1();
public override Method2();
}
public class C : CustomBaseClass
{
public override Method1();
public override Method2();
}
在此示例中,TeamA 更改了基础 class,而 TeamB 无需费力即可保留其自定义设置。