Re: data and interfaces



Mr Fish wrote:
howdi,

I'm curious to how people feel about this... I often create an
interface then realize that *all* subclasses will have similar data
such as, say, a list of floats so I define the interface as

class ISomeInterface
{
protected:

  list<float> m_Floats;

public:

  ~ ISomeInterface();

  virtual void SomeMethod()=0;

};


I'm sure though that the purists would exclude the data from the intertrface class and I'm sure there must be some good reasons to do so.

Can you provide me with any? (assuming that the data type will not
change from subclass to subclass *ever*)

The above is the reason.... we can not - or rather should not assume anything about the Implementation of the Interface.


The main aim of Interfaces is to separate the interface (aka the Type) from the implementation (aka the class that 'is a' Type).

Don't think of the implementors of Interfaces as subclasses - thats a C++ implementation detail. Classes 'implement' an Interface - they don't extend them.


If you decide that all current implementors of the Interface have commonality (like the float list) then you have a number of choices.


1) Create an Abstract Base Class and put the list there. Then each class that 'implements' the interface can also derives from the ABC.

2) Create an Abstract Adapter class - its a class that in C++ terms derives from the Interface, but still does not implement the interface methods. This adapter can then have the list of floats.

Either of these ways are maintaining the separation of Implementation from Interface, allowing other implementors of the interface to not be burdened with unneeded extras.

Andrew

BTW - there's nothing 'purist' about this.
.



Relevant Pages

  • 7.0 wishlist?
    ... Any object can be used where an interface type is expected if it has the right set of method signatures, whether or not it "implements" the interface using that keyword. ... the algorithm name is hard-coded and so this exception ... Most invocations of runnables ... Allow subclassing an enum with the subclasses able to REMOVE (not ...
    (comp.lang.java.programmer)
  • Re: bloated base class?
    ... Now the app needs to be extended to process a ... > interface depending on what kind of data they work with. ... you need different Process subclasses as well. ... but that is isolated to whoever instantiates the relationships ...
    (comp.object)
  • Re: How can I make this design tidier?
    ... Interface "AnyElement" with about 80 properties. ... A generalization with 60 sibling subclasses is almost surely wrong because it is too big. ... If so, then they don't share that property with other superclass members in other subclasses, which defeats one goal of generalization. ...
    (comp.object)
  • Re: Calling explicity interface implementation from a subclass
    ... which the implementation of the interface calls. ... > abstract class EventfulCollection: ICollection ... > The problem this causes is that I must declare ICollection.CopyTo, but> I don't want to implement the methods here, allowing my subclasses (some are> ILists, others are IDictionaries) to implement it themselves. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Classes and interfaces
    ... > I want to create a Shape class and some subclasses such as Rectangle, ... > to implement a Draw() function. ... Interface is a mechanism for communication between two parties. ...
    (comp.lang.cpp)