Design problem instanceof

Started by
3 comments, last by MattieB 18 years, 4 months ago
Hi, i'm making a domotica design tool. I have the following design problem: I've got 6 domotica items (thermometer, doorsensor, pushbutton, alarmbell, jacuzzi, lightbulb) and 4 blueprintitems(wall, room, window, door). I have a blueprintItemManager who manages the blueprintitems: location(coordinate), name and owners(owner of a room are a couple of walls, owner of a window is a wall, a room and the "outside" room, the owner of a door is a wall and the two rooms it connects). The domoticaitemmanager manages the domotica items: name and owners (owner of an alarmbell is a wall, owner of a doorbutton is a door, owner of a thermometer is a wall, owner of a jacuzzi is a room). Basically, the owner of a domotica item is a blueprintitem where it can be placed on. That means that there are some rules where a domotica item can be placed. I'm trying to figure out how i can define those rules (using a good design). At the moment I have a class with a method check() and two parameters: BlueprintItem and DomoticaItem. The check method uses instanceof for the two parameters and returns true if the given domoticaitem can be placed on the given blueprint item... which I think is crap, because I don't like instanceof. I'm not seeiing another way. I don't want to make an abstract method in the superclass domoticaitem (or blueprintItem) and overloading it in the subclasses. The subclasses don't need to know where they can be placed on (and i still have to use instanceof). Anyone got an idea? Thanks, Jan (PS: I'm using java, but that is rather irrelevant)
Here we are now,entertain us.
Advertisement
Maybe use composition. A given blueprint item has a list of domotica items it owns. It has addDomotica() overloaded for all the types it can own, say addDomotica(Thermometer t) and addDomotica(Wallknob k). Is that what you're after?

Edit: By the way, I don't think domotica is a valid english word ^_^.
Sounds like you want a visitor based design.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
What you're looking for is double dispatch. This should do exactly what you're after. The snag is that all objects (on both sides of the dispatch) end up 'knowing' about each other, so this is less than ideal if you want to add lots of new types at a later point.

Edit: Better link.
Yep, double dispatch is what I need and going to use.
Thanks

Jan

(It should be domotics indeed :-))
Here we are now,entertain us.

This topic is closed to new replies.

Advertisement