我有两个类都坚持相同的接口(IAccount)。它们每个都有一个名为Preferences的属性,它遵守另一个接口(IAccountPreference)。一般创建正确的混凝土类型属性
我想通常的方式来创建偏好属性(即一种方法,它需要一个IAccount并为它创建一个偏好,而不关心通过的IAccount的实际类型)。达到此目的的最佳方法是什么?
我能想出的最好的解决方案是一种实用工具工厂方法,像如下(半pesudocode):
private IAccountPreference getAccountPreference(IAccount acc){
switch(acc.GetType()){
case AccountType1:
return new PreferenceForAccountType1();
case AccountType2:
return new PreferenceForAccountType2();
.
.
.
}
}
并用它来获得正确的具体类型的引用。这看起来很乱。
我错过了一个更明显的解决方案吗?
您可以更改具体类以实现具有所需属性的另一个接口吗? – Lee 2014-12-01 20:20:26