Re: Downcasting - whats the problem?

From: Shashank (shashank_at_icmgworld.com)
Date: 05/18/04


Date: Tue, 18 May 2004 14:29:18 +0530

Root of the problem is while trying to model a problem domain we do abstractions,
and identify classes. In the process we do refinement to identified classes and
identify classes that can serve as base class for derived classes, that is
class's whose objects shares some common feature/ behavior we tend to put them
into a separate class (base class).

When we want to invoke certain behavior on all objects that have common behavior.
This is start of our processing. Then suddenly we realize that in case of a
particular (specific) instance we want to do something different. We didn't
included this in the common base class as this was not common among all the
objects, and so unnecessarily we cant put some behavior in common base class if
it is really not common. But now most of our processing are actually business
logic process that works on specific types. So it mandates now to identify the
specific type before calling appropriate behavior on it.

As you agree that ,to resolve these type of problem we cant unnecessarily change
the base class, to make our code clean, by making object identity redundant (but
at a cost)

So we may use visitor pattern (well again depends on your actual way of usage,
frequency of updates/ enhancement etc.) to select a appropriate solution.

Object Developer wrote:

> While Visitor offers a means of getting away from the problem, I am trying
> to find out what is the root of the problem.
>
> "Shashank" <shashank@icmgworld.com> wrote in message
> news:40A87F85.3BE5FAD5@icmgworld.com...
> > You may use Visitor pattern to resolve this kind of problem (of casting
> > into different super types). That eases maintenance and extension for new
> > type added. and you also may get rid of so many if-else.
> >
> > You may visit:
> >
> http://exciton.cs.oberlin.edu/javaresources/DesignPatterns/VisitorPattern.htm
> >
> >
> >
> > Object Developer wrote:
> >
> > > I would like to ask what will be a very basic question for many on
> > > this list, so I hope those people offer some helpful advice on this.
> > > I would like to understand what the real problem is with downcasting
> > > in a staticly typed language. I know about the OCP, and I understand
> > > the reasons for why this type of thing is wrong:
> > >
> > > public SomeMethod(SomeObject obj)
> > > {
> > > if(obj is typeof(FooClass))
> > > {
> > > ///
> > > }
> > > else if(obj is typeof(BarClass))
> > > {
> > > ///
> > > }
> > > etc...
> > > }
> > >
> > > What I cannot understand is where else downcasting is not preferred,
> > > and why it is "bad design", which is what I seem to think most people
> > > think it is on here. I recently read some back-posts on this topic,
> > > and Mr Robert Martin states a valid need to apply downcasting (Vehicle
> > > and Operator thread). However some people continued to state that
> > > downcasting is something to avoid, even in these situations (though I
> > > personally failed to see how you would get around it).
> > >
> > > I am still learning object oriented development, and I am sure there
> > > are other lurkers who would like to know some of the things that are
> > > not said or explained. The reason for downcasting being bad design is
> > > one of them, certainly for me.
> > >
> > > Thanks
> > >
> > > uod.
> >



Relevant Pages

  • Re: OO refactoring trial ??
    ... As long as the concrete Python classes follow the form ... other common-to-all-taunts behavior, perhaps a common constructor, or ... in both Python and Java. ... return values derive from a common base class, ...
    (comp.lang.python)
  • =?ISO-8859-1?Q?Re=3A_AS_geb=FAr=3B_bauer=3B_neighbour?=
    ... the root is restricted to Gmc. ... n., etc., find a common meaning "room, single building", and tell ... many other feminine plurals of neuters.) ... Because disturbing details have to be explained. ...
    (sci.lang)
  • =?ISO-8859-1?Q?Re=3A_AS_geb=FAr=3B_bauer=3B_neighbour?=
    ... the root is restricted to Gmc. ... n., etc., find a common meaning "room, single building", and tell ... many other feminine plurals of neuters.) ... Because disturbing details have to be explained. ...
    (sci.lang)
  • Re: polymorphism problem: common signatures with no common parent
    ... > pattern or trick I'm not seeing...but given that the base class does not ... You have a class AB with no foo methods, ... // Class containing common method names. ... class ConditionalConverterInstance: public ConditionalConverter { ...
    (comp.lang.cpp)
  • Re: AS =?UTF-8?B?Z2Viw7pyOyBiYXVlcjsgbmVpZ2hib3Vy?=
    ... seljak have in common? ... village', which is cognate with OIc 'room, hall'. ... but does reduce the possiblity of the existence of any previous root having been common to all three on independent tracks. ... can I presume a meaning of 'high town' in Old Swedish? ...
    (sci.lang)