Re: Downcasting - whats the problem?
From: Shashank (shashank_at_icmgworld.com)
Date: 05/18/04
- Next message: Object Developer: "Re: Downcasting - whats the problem?"
- Previous message: Cristiano Sadun: "Re: Why is OO Popular?"
- In reply to: Object Developer: "Re: Downcasting - whats the problem?"
- Next in thread: Mark Nicholls: "Re: Downcasting - whats the problem?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
> >
- Next message: Object Developer: "Re: Downcasting - whats the problem?"
- Previous message: Cristiano Sadun: "Re: Why is OO Popular?"
- In reply to: Object Developer: "Re: Downcasting - whats the problem?"
- Next in thread: Mark Nicholls: "Re: Downcasting - whats the problem?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|