多接口继承中的问题

时间:2014-12-12 09:30:21

标签: c# interface

我有一个相当大的应用程序,它有很多基类。我想将其更改为接口,以允许(更容易)单元测试。

然而,当我对我可能使用的接口进行新的“设计”时,我注意到某些类将继承5个或更多接口。这会引起问题吗?

我问这个,因为我不想重构整个应用程序,发现它不起作用;或者如果有一个很好的解决方案,可以将其纳入重构步骤。

我想到的一些接口(伪代码)。它不完整但它显示了我想要的界面关系(还会有更多)。例如。 IBank来自5个接口,我认为还需要更多接口。

INavigation: IOffset
   INavigation Root { get; }
   INavigation Parent { get; }

IMemory
   string FileName { get; set; }
   byte[] Content { get; }
   Model Model { get; }

IPcgMemory: IMemory
   ProgramBanks ProgramBanks { get; }
   CombiBanks CombiBanks { get; }
   SetLists SetLists { get; }
   DrumKitBanks { get; }
   WaveSequenceBanks WaveSequenceBanks { get; }


ISelectable
   bool IsSelected { get; set; }

ICountable
   int Count { get; }

IName
   string Name { get; set; }

IOffset
   int ByteOffset { get; set; }


IPatch: IName, ISelectable, IOffset, INavigation


IBank: ICountable, IName, ISelectable, IOffset, ICountable, INavigation
   IEnumerable<Patch> Patches { get; }

IProgramBank: IBank

ICombiBank: IBank

ISetList: IBank

IWaveSequenceBank: IBank

IDrumKitBank: IBank


IBanks: ICountable, INavigation, ISelectable, IOffset, ICountable
   IEnumerable<IBank> Banks { get; }

IProgramBanks : IBanks
   int CountModeledPrograms { get; }
   int CountSampledPrograms { get; }

ICombiBanks: IBanks

ISetLists: IBanks

IWaveSequenceBanks: IBanks

IDrumKitBanks: IBanks

PcgMemory: IPcgMemory

ProgramBanks: IPatchCollection

1 个答案:

答案 0 :(得分:4)

我想知道你期待什么问题。 List<T>派生自8个接口(IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable),我从未见过它的问题。

问题可能在于你要测试它,在接口之间有那么多关系时,创建测试对象需要做很多工作,但这就是我认为的工作。试着找出你是否真的需要所有这些接口,也许你可以将它们组合起来或跳过它们。

相关问题