An example of why multiple inheritance can be a good thing

Jan 31, 2007 15:36

In one of my (Java) projects at work, we have an interface which represents the contents of a "savable window", which can be modified by the user, and where changes should be saved back to the underlying object when the user is done (usually indicated via an "OK" button).

The (vastly simplified) implementation looks like this:

/** Contains all ( Read more... )

programming languages, geeky, java

Leave a comment

Comments 4

kion February 1 2007, 02:05:18 UTC
Were I doing this in C++ (or any language with multiple inheritance), I could just do the following:

* Add the appropriate listener machinery to the Savable base class

* Create a new pure virtual (or abstract) method in Savable called doSave(), realSave(), or something similar.
* Rename the save() methods in all the Savable-derived classes to doSave().
* Change Savable.save() to be a non-virtual (or final) method that calls doSave(), then fires the SaveEvent.

Reply


kion February 1 2007, 02:08:17 UTC
Were I doing this in C++ (or any language with multiple inheritance), I could just do the following:

* Add the appropriate listener machinery to the Savable base class

Just add this code to your main window Save() call

* Create a new pure virtual (or abstract) method in Savable called doSave(), realSave(), or something similar.

Rename your Saveable interface "save" method to doSave()

* Rename the save() methods in all the Savable-derived classes to doSave().

Same step works in java.

* Change Savable.save() to be a non-virtual (or final) method that calls doSave(), then fires the SaveEvent.

Final is a keyword in java your new Save method would look like this:

public final void Save()
{
//fire event code
doSave(); //calls proper virtual function
}

Reply

deskitty February 1 2007, 02:51:42 UTC
Just add this code to your main window Save() call

Sometimes the Savable objects call their own save(), so the CMWindow isn't always aware of when a save happens.(See Option #1.)

Final is a keyword in java your new Save method would look like this:Savable is an interface. AFAIK you can't add method implementations to an interface ( ... )

Reply


stargazr417 February 2 2007, 01:01:33 UTC
Woah...that went so far over my head I think it'll take a couple of weeks before I feel the slightest bit intelligent again, heh...

But suffice it to say that I'm duly impressed! ^^

Reply


Leave a comment

Up