|What I would do in this case is have a virtual function in Block that does nothing (and perhaps signals the caller that nothing happened) and override it in the relevant classes.|
Maybe we are saying the same thing in different ways, because that's what my examples do (both the Bee and the Blocks) The difference is that my example makes use of the pointer to the derived class being a valid pointer to the base class, so the correct action is carried out. Again it takes advantage of the PTABLE & VTABLE. You guys have a lot of experience in C++ - I am guessing you know all about these?
There is no sending of lists of all the objects
, as mentioned in your OS scenario.
|Although nonsensical if stated like this, a better approach would be to have the bee ask the world "tell me where a flower is, any flower" or have a flower tell the bee (any bee?) "here, pollinate me" or perhaps to the world "I'm ready to be pollinated". The specifics depend on what exactly it is you're trying to do.|
Maybe it is nonsensical because it involves broadcasting and receiving messages - wouldn't that be harder or less efficient than using virtual functions as we both seem to be proposing.
Is there a Design Pattern that can be used for that?
|The main differences between this case and bees are:|
1. The bee wants to perform a particular action on any object of a class. The block wants to perform a particular action on a particular block.
2. It could be expected that a user attempt to store money in dirt, while some animal interactions will never happen. A fish should have no business knowing that it can't be pollinated by a bee merely because it's alive. (Note that this is partly a consequence of #1.)
1. Bees & Blocks are very similar (that's why L B
used them as examples) in that we don't want to have a bad solution to cope with different types. My use of the pointers deals with it elegantly IMO.
2. The Animal class will have it's virtual function returning false - all the derived animals will inherit that same function, so no more code needed for animals. This works because of the C++ system's internal VTABLE.
Animals are part of the problem because they can be landed upon. The Bee's detection system relies partly on colour, and it can only detect that is actually a flower once it has landed on it. So when it lands on a Yellow / Orange patch is doesn't know beforehand whether it might be a Tiger or an Orange / Yellow flower.
Your analogy of the OS isn't right IMO, because the OS does know
which are output devices beforehand. On Unix, where everything is a file, I can imagine the file being the base class, and things like processes and devices would be derived classes, so it would be easy to know which is what.
Perhaps one could use my example in a similar fashion for the OS. The OutputFile function takes a pointer to an OutPutDevice as a parameter. When the function is called, it doesn't matter whether it is sent a pointer to a printer, the console, or a disk as an argument - as long as the appropriate inheritance and virtual functions etc is set up.
I don't know the actual details of a kernel - it's just the concept I am putting out as being a logical solution.
Anyway, it is good to have an intelligent debate - hopefully not too boring at your end !! L B
hasn't said anything - hopefully he doesn't mind me running off with his thread.