4

I have a reasonably large application that has a lot of base classes. I want to change this into interfaces, to allow (easier) unit testing.

However, when I make a new 'design' of the interfaces I might use, I notice that some classes will inherit from 5 or more interfaces. Can this cause problems?

I ask this since I don't want to refactor the whole application to find out it will not work; or if there is a nice solution, to take this into the refactoring steps.

Some of the interfaces I had in mind (pseudo code). It is incomplete but it shows the interface relations I want to make (there will be many more). E.g. IBank derives from 5 interfaces, and I think more will be needed.

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
2
  • 4
    Sometimes we tend to "over engineer". You might want to try to find a common logical group for a couple of these interfaces so you can join them together. Commented Dec 12, 2014 at 9:33
  • ICountable implemented two times may be a typo Commented Dec 12, 2014 at 9:34

1 Answer 1

4

I wonder what problems you expect. List<T> derives from 8 interfaces (IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable) and I have never seen the problem with it.

The problem might be when you are going to test this, having that much relationships between interfaces, it takes a lot of work to create test objects, but that comes with the job I think. Try to find out if you really need all those interfaces, maybe you can combine them or skip them.

Sign up to request clarification or add additional context in comments.

6 Comments

A List<T> is something you implement once. If any of the types he creates in his project has to implement all these multiple interfaces, it can get messy.
@YuvalItzchakov: True, but the initial question is: I notice that some classes will inherit from 5 or more interfaces. Can this cause problems? and that answers to know. The second paragraph points on the messy stuff.
@YuvalItzchakov: And do you know how many classes implement IList? A lot.
Thanks for this answer, it seems that I can best start making the interfaces as small as possible and only extend if really needed.
@MichelKeijzers: Sometimes it is just a matter of one addition property which could be added to an existing interface, or creating a new one. Try to keep it simple.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.