Hi Paul,
I also think that representing the parts as strings is a failure to do object-oriented design
I totally agree. That is what I meant by "clumsy".
And the Device can ask the Widget<T> object to give out its List<T> which is a list of parts. But why? The Device doesn't know anything about the parts so what does it do with them?
Mostly it doesn't touch the parts, it just touches Widgets. But, it needs to
test the status of Parts and "repair" them as needed. The key part is user interaction. Device basically represents the GUI of my system (or at least it controls many of the GUI's components). Here is some more psuedo code to make it more clear.
Device{
ArrayList<Widget> widgetList = new ArralyList<Widget>;
// Constructor code that extracts widgets from files to build widgetList
// display list of Widgets to user and allow consolidation and elimination of widgets.
// user selects widget from WidgetList.
activeWidget = GUI.component.getSelected();
Object[] partsList = activeWidget.getPartsList(); //at this time Widget was using the ArrayList.toArray() to return an array of Object.
Object activePart = null
for (i = 0, i< partsList.length, i++){
activePart = partsList(i);
if (activeWidget.getStatus( partsList(i) )){
addPartToActivePartsList (activePart);
}
.// interact with user some more (see notes)
...
}
// Now Device has a list of active parts for the chosen widget. If the widget has no active parts, it might simply call Widget.close, or Widget.destroy. Or, depending on parameters by the user it might go in to a waiting routine where by it monitors the status of the parts, and displays a count of active parts on the GUI. If the user gets impatient, He can click a button to tell Device to call activeWidget.repair(part) for each part that was active when the initial status check was done. The widget will know if deactivating the part makes sense or is even physically possible.
And now you can see how this
thread got named. Device was passing things of type java.lang.Object to methods like Widget.getStatus, and then having getStatus(part) recast those Objects into instances of a specific class
public class BikeWidget extends Widget{
public Boolean getStatus (Object part){
BikePartOS myPart = (BikePartOS)part; //BikePartOS comes from 3rd party libraries.
return myPart.doStuff()
...
}
But the cast of Object to BikePartOS reminds me a lot of casting void pointers in C to just about anything you want. The Java compiler will let me do it and it will throw a _runtime_ exception if I pass a BikeWidget.getStatus something of type Object which was not originally something of type BikePartOS. But it still annoyed me.
Can't you encapsulate the part-handling inside Widget?
That is exactly what I want to do. But I don't want a lot of GUI stuff in Widgets.
Having written all this out, I think I could change things a little bit so that Widget keeps the list of 'active' parts. then reports status on that list, or reports changes to that list in real time to Device (which can then update the GUI). That is a little counter intuitive in the big picture of my code, but maybe it would be better.
I could extend the third party "PartOS" classes to include the handling code that is currently in Widget for that purpose. This new class could then implement some of the interface (partInterface) that is currently implemented by Widget for that purpose. Then Device, could not know the details of Part, but could still call on parts to do the status checking.
Anyway, thanks again. Just talking it out has really helped clarify the whole thing. Often just explaining something helps me see relationships.