• 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: Problem overriding a factory class mehtod
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem overriding a factory class mehtod


  • Subject: Re: Problem overriding a factory class mehtod
  • From: Ivan Kourtev <email@hidden>
  • Date: Thu, 2 Nov 2006 21:53:20 -0500

On Nov 2, 2006, at 7:03 PM, Julien Jalon wrote:
It seems to be a "bad implementation" of FooA. I suppose +[FooA
fooWithInt:] implementation looks like:
+ (FooA *)fooWithInt:(int)someInt
{
   return [[[FooA alloc] initWithInt:someInt] autorelease];
}
while it should be:
+ (id)fooWithInt:(int)someInt // don't strong type class creation
methods, it's generally a bad idea
{
   return [[[self alloc] initWithInt:someInt] autorelease];
}

Yeah, I know but the actual initializer is quite more complex than the example (although conceptually equivalent, I think).


FooA (the superclass) does have a designated initalizer initWithX:y:z: -- so +[FooA fooWith***] looks like this:

+(id)fooWith***
{
  // make x, y, z
  return [[[FooA alloc] initWithX:x y:y z:z] autorelease];
}

Now I can replicate the x, y, z stuff into +[FooB fooWith***] and have something like

+(id)fooWith***
{
  // make x, y, z
  return [[[FooB alloc] initWithX:x y:y z:z] autorelease];
}

which indeed works (or so it seems so far). But with this approach there is code duplication so I thought maybe I am missing a more elegant way to do this without duplicating code.

-- ivan


On 11/3/06, Ivan Kourtev <email@hidden> wrote:
I am faced with the following situation involving inheritance:

Class FooB inherits from class FooA.  Class FooA implements a class
method

+ (FooA*)fooWithInt:(int)x;

The problem seems to arise with calls such as [FooB fooWithInt:x].

If I don't implement + [FooB fooWithInt], then I just get back a FooA
object (doesn't have FooB's variables).

If I override

+ (FooB*)fooWithInt:(int)x {
   return [super fooWithInt:x];
}

then I get back a FooA object again.

So what is the "by-the-book" correct way to handle such situations?
I suppose a full reimplementation of + [FooB fooWithInt] (one that
doesn't call [super ...]) would do but then I am not able to reuse
the superclass's code.  Not a problem in my case (I own all classes)
but what if I were working with another party's library and a header
file?

-- ivan
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Re: Problem overriding a factory class mehtod
      • From: "Alan Smith" <email@hidden>
References: 
 >Problem overriding a factory class mehtod (From: Ivan Kourtev <email@hidden>)
 >Re: Problem overriding a factory class mehtod (From: "Julien Jalon" <email@hidden>)

  • Prev by Date: Re: Problem overriding a factory class mehtod
  • Next by Date: NSCell, copying, and objectValue
  • Previous by thread: Re: Problem overriding a factory class mehtod
  • Next by thread: Re: Re: Problem overriding a factory class mehtod
  • Index(es):
    • Date
    • Thread