Re: Newbie Key Value-coding and Subclasses Question
Re: Newbie Key Value-coding and Subclasses Question
- Subject: Re: Newbie Key Value-coding and Subclasses Question
- From: David Avendasora <email@hidden>
- Date: Fri, 10 Feb 2006 06:57:49 +0000
Hi Arturo,
I have a reputation as being a bit ummm *strict* in my
interpretation of what the proper O-O way is of doing things. It
has served me well with code and less so with ppl :-)
No worries! This is my first OO programming project and being
*somewhat* over the age of 14, I'm having a slow time reconciling it
with my *strict* Relational Database design background. I'm trying to
be OO, but sometimes I wake up in the night screaming "It's not
normalized, it's not normalized!!!" Be strict. It helps me.
Arturo,
On question 1, I am currently testing for what type of Class it is
by reading my PartType field using:
if (((String) valueForKeyPath("part.partType.partTypeName")).equals
("Finished Part")) {...}
and this works just fine, but my understanding was that instanceof
is a better OO way of doing things - especially since the string
partTypeName is a value from a DB field which could potentially
change.
The problem isn't so much with doing the above or using
instanceof. The problem is that once your code becomes aware of
object-types then you're on a slow sure path to perdition. You
might want to take a look at the book HeadsFirst Design Patterns
(the typical GoF reference is too heavyweight for most developers)
for reasons to avoid instanceof et al.
I have bought it and am currently about a third of the way through. I
understand the write to an interface and not to the implementation
concept, but I am having a hard time understanding exactly how to
apply it in this situation.
All I'm doing here is copying some default data from a Part object to
a Label object. Depending upon what type of Part (Raw, Intermediate
or Finished) they have different subsets of information. All three
have some things like partDescription but only a Finished has
unitQuantity so I have to figure out what type of Part it is first to
know what information I can get from this part. I suppose I could
create an interface of "Labelable" and require all Part types to have
the fields I'm pulling so I wouldn't have to worry about what type of
Part it is, but then I'm solidifying some UI convenience as a
requirement in the code.
At this point I'm sure you are going to as "Why are you copying the
data? Just use the relationship to grab the information from the
associated Part." The problem is that a Label can potentially use
different information, I'm just copying this info across as the
default. The user can change it to something else, although they
usually won't.
IIRC, the proper WO way of doing things is to use single table
inheritance for the above situation. Is that what you're doing?
(sheepishly) No, I'm using Vertical Inheritance, even after reading
all the admonishment against it in the WO documentation. See the bit
about my dreams above.
On the second question, The problem is that "Label" is not related
to "Finished", but to it's superclass "Part". Once I know that the
"Part" I'm dealing with is a "Finished" (subject of question 1)
then how do I call a method from the "Finished" subclass using key
value-coding? valueForKeyPath("part.unitQuantity") does not work
because unitQuantity is not a attribute of "Part" it is an
attribute of "Finished".
If your object inheritance hierarchy is correct then you should be
able to do valueForKeyPath("part.unitQuantity"). Let's say you
have Part as the base of your inheritance hierarchy. If Finished
extends Part then you are done.
AHHG. It's working perfectly. I *swear* I tried to do this first and
it didn't work.
This actually makes me feel MUCH better because what I wanted to do
first was the right thing (if you ignore the whole "testing for
object-type" and "vertical inheritance" things). I thought that this
way just seemed right, and it would have been the best way, but when
it didn't work (or so I thought), I was stumped and started trying
other things. I must have had some other bug in my code.
Thanks for the help!
Dave
Dave
On Feb 9, 2006, at 4:17 PM, Arturo Perez wrote:
David Avendasora wrote:
Hi.
I have a relationship from Label to Part (toOne)
Part has three subclasses: Raw, Intermediate and Finished. All
of these (Part, Raw, Intermediate and Finished) are all
EOGenericRecord classes right now.
I'm running into two problems
1) How do I perform an instanceof to determine what subclass of
Part takeStoredValueForKey("part") returns when run on a Label
object? Do I have to generate the custom classes first for Part
and it's subclasses?
2) How do I call subclass-specific methods with key value-coding
when the relationship is to the superclass? I have no problem
executing superclass-specific methods using valueForKeyPath
("part.partDescription"), but I want something along the lines
of valueForKeyPath("finished.unitQuantity") (unitQuantity is an
attribute of the Finished subclass of Part)
Well, instanceof in an OO design is generally considered bad
practice. You may want to reconsider if your design requires
it. If you really do need to know what something is I think you
can do something like getting the EODescription/
EOClassDescription of the EOGenericRecord and one of the
attributes is the name you've given it.
What's the problem you're running into with the second thing? If
you have a relationship to something and you do what you've
written it should just work.
-arturo
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden