This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
ManagedObject is a class provided by a framework. Our own classes all inherit from this class. The framework also provides some API to retreive ManagedObject from database. We have kinds of childrens of ManagedObject. But we only get their parent reference via the framework's API. We add a group attribute to all the children classes. When we want to get this attribute, the code like this:
We do the downcast only to get the group attribute! We haven't the privilege to modify the ManagedObject to add this attribute. How to eliminate these if-else in this situation?
Let both ChildA and ChildB implement that interface. That way, you can simply cast the ManagedObject to the Grouped interface - no need for the instanceof test.
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
Joined: Aug 19, 2005
Originally posted by Ilja Preuss: Introduce an interface