0

I have various interfaces i have to implement. Let's assume ITypeA, ITypeB. They have following methods:

ITypeA
  Connect
  Disconnect
  GetData
  SetData
  ClearData

ITypeB
  Connect
  Disconnect
  GetData
  SetData
  Start
  Pause
  Stop

They have some base functionality: Connect, Disconnect, GetData and SetData. If I want to add ITypeC in the future, it will have these base functionality too.

I want to access to the base functionality and if there is additional functionality, I want to get this too. What design patterns are available that are capable of this situations?

I found one here: Extension Interface. In this, you create a base interface and multiple extension interfaces that are implementing the additional functions. In the base interface you can ask for the available extension interfaces of the component. But this approach seems complicated: I have to create interfaces for each type, factories for the interfaces, I need some tables to save what Interface belongs to what Factory...

What is the basic solution for this problem? Extension Interfaces? Are there any other possible alternatives for my problem?

  • This is usually an anti-pattern. Adding functionality to some base class quickly gets you into "if X is B" all over the place. – Telastyn Sep 1 '14 at 16:36
  • @Telastyn I need the full functionality of these interfaces. I only see the way to use a base interface for standard functions and additional interfaces for the rest (where "if X is B" is a necessary evil). If you have an idea to avoid this "if X is B", it would be nice. – fedab Sep 5 '14 at 7:31
3

You may have more joy dividing your interfaces by behaviour and deciding on the types later.

interface IConnectable
    Connect
    Disconnect

interface IDataTransferrable
    GetData
    SetData

interface IPlayable
    Start
    Pause
    Stop


class TypeA : IConnectable, IDataTransferrable
class TypeB : IConnectable, IDataTransferrable, IPlayable

And then how can discover if your object supports these interfaces?

here's one way:

if (ob is IPlayable)
{
    Playback(ob);
}

where you have a function:

void Playback(IPlayable ob)
0

It may be me but I can't see the difference between your problem and the description of the inheritance concept wich work with interfaces the same way it work with class.

So the basic solution to this problem seems to be inheritance (an interface being in fact a purely abstract class without any method implementation or attribute).

If your problem is that you want to define an interface on wich you can call potential additionnals methods not defined at the compilation time, then you are looking for dynamic typing wich is a language feature. (for research purpose look also for the term "duck-typing")

Look at Ruby, Python or Perl for exemple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.