[Haskell-cafe] OO idioms redux
Ben Rudiak-Gould
br276 at cl.cam.ac.uk
Sun Oct 17 12:29:08 EDT 2004
John Goerzen wrote:
> I'm not sure I understand what you mean by containment and delegation
> -- could you elaborate?
This means that instead of inheriting all the member functions of the
base class and selectively overriding them, you store an object of the
"base" class as a member of the "derived" class, and make use of it in
your implementation. The standard C++ ColoredPoint example looks like
this if you use interface inheritance:
class ColoredPoint : public Point {
int color;
public:
// ColoredPoint-specific methods
};
and like this if you use C&D:
class ColoredPoint {
Point p; // "containment"
int color;
public:
int getX() { return p.getX(); } // "delegation"
void setX(int x) { p.setX(x); }
// ...
// ColoredPoint-specific methods
};
If you want to take advantage of inheritance polymorphism with this
scheme, then you create an interface IPoint with virtual methods like
getX and setX, and have both Point and ColoredPoint implement it (by
inheriting it, in C++).
>> 2. Try revisiting the original problem and thinking about how to
>> solve it in a Haskellish way, rather than solving it in another
>> language and then translating.
>>
>>
>Thats exactly what I'm trying to do here :-) I've thought of having a
>type that basically stores a bunch of functions -- an implementation
>would simply provide an instance of that type with the functions,
>maybe.
>
>
Yes, this is often a good approach, especially when you combine it with
labelled constructor fields.
-- Ben
More information about the Haskell-Cafe
mailing list