Re: data and interfaces
- From: Andrew McDonagh <news@xxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 27 Sep 2005 19:31:31 +0100
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. .
- References:
- data and interfaces
- From: Mr Fish
- data and interfaces
- Prev by Date: Re: Why encapsulate state pattern......
- Next by Date: (Slightly)OT : Any patterns/idioms for implementing a decision table in OO language such as Java
- Previous by thread: data and interfaces
- Next by thread: Strategy or A factory with Template Method
- Index(es):
Relevant Pages
|