• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie Key Value-coding and Subclasses Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie Key Value-coding and Subclasses Question


  • Subject: Re: Newbie Key Value-coding and Subclasses Question
  • From: Arturo Perez <email@hidden>
  • Date: Thu, 09 Feb 2006 12:59:11 -0500

David Avendasora wrote:

Hi David,

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 :-)

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.


IIRC, the proper WO way of doing things is to use single table inheritance for the above situation. Is that what you're doing?
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. Here's some sample pseudo-code to convey the concept.


class Part {
   public String getLabel() {...}
   public String getMaterial() {..}
}

class Finished extends Part {
   public String getUnitQuantity() {....}
}

public static main(String [] args) {
Part p = new Part();
Finished f = new Finished();

System.out.println("p unit is" + p.getUnitQuantity()); // this line won't compile. Part does not implement unitQuantity().

System.out("finished name " + f.getLabel()); // Finished inherits getLabel()

p = f;  // f isA kind of Part so this assignment works just fine.

NSKeyValueCoding.Utility.valueForKey(p, "unitQuantity"); // this line should work.

}

I think the above makes sense.

-arturo


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
  • Follow-Ups:
    • Re: Newbie Key Value-coding and Subclasses Question
      • From: David Avendasora <email@hidden>
    • Re: Newbie Key Value-coding and Subclasses Question
      • From: Chuck Hill <email@hidden>
References: 
 >Newbie Key Value-coding and Subclasses Question (From: David Avendasora <email@hidden>)
 >Re: Newbie Key Value-coding and Subclasses Question (From: Arturo Perez <email@hidden>)
 >Re: Newbie Key Value-coding and Subclasses Question (From: David Avendasora <email@hidden>)

  • Prev by Date: Re: horizontal mapping inheritance fetch trouble
  • Next by Date: Sort in memory over multiple fields
  • Previous by thread: Re: Newbie Key Value-coding and Subclasses Question
  • Next by thread: Re: Newbie Key Value-coding and Subclasses Question
  • Index(es):
    • Date
    • Thread