Technical Architect, SCJP, SCWCD
Originally posted by Chris Nappin:
How can I know whether to trigger some behavior in instances of B, other than using instanceof, then casting and calling the method(s) only present in B?
Originally posted by Peer Reynders:
This is an indication that your abstraction/interface on A needs some work. In the simplest (but not the best) case you could just promote the B method to the A level and have C do nothing when that method is called. However it is more likely that you need to have a good look at the code that uses instanceof. Some of that code may need to go inside of A as a new suitably named method that makes sense for most, if not all subclasses of A. Then inside of that method, call template methods that the subclass can override - in which case B would put its code there, while C ignores it.
Technical Architect, SCJP, SCWCD
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Stan James:
Can you move the code specific to each subtype into the subtype behind a common method?
Originally posted by Stan James:
Or put the code specific to each subtype into its own Strategy?
Technical Architect, SCJP, SCWCD
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Chris Nappin:
Thanks for the suggestion. Unfortunately you are saying to move a method unique to B into A, then add a default implementation of that method to A.
That default implementation would have to do nothing, since that functionality makes no sense in the other subclasses (e.g. C).
I think this may cause more confusion for Junior developers, and bloat A with unrelated concepts?
I have seen other solutions where Enums or ids are defined for each class (B, C) and code checks that instead of using "instanceof", however that seems to be treating the symptom rather than the cause.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Andrew:
I essentially have the same question as Chris and I'm having trouble putting this together in my head. We know it's desirable to program to an interface. If class B in Chris' example contains a completely unique method, how should the design of the inheritance hierarchy proceed? It seems there are 2 options available.
I thought base classes were for common code and derived classes for more specific code. If this is true, how then do we program to an interface while still having access to unique, derived functionality?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Frank Carver:
Has that made any sense?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Andrew:
How does the client know which version of the ID3Tag (and therefore what methods are available) it's using?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
It's just a flesh wound! Or a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|