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

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.

Pursuant to our discussion on IRC: I don't think data models are quite what I want, either.

The class hierarchy looks like this:

JFrame -> CMWindow
JPanel -> [class generated by UI tool] -> [class that implements Savable]

CMWindow has a Savable object, which goes into its contentPane (along with other boilerplate things like "OK" and "Cancel" buttons).

A bit more context/example: I have a Client object representing a therapy client. The Client has a first name, last name, birthdate, etc. A Savable for this client object would be a form that allows the user to fill in all these fields. When the user presses the "OK" button, the Savable's save() method gets called. The save() method pushes the user's changes back to the Client object and saves the Client object to disk.

I want other, indeterminate parts of the program to be notified when the save() happens. (Said other parts--for example, parent windows--would be responsible for installing their own listeners.)

Reply


Leave a comment

Up