• 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: Subclassing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Subclassing


  • Subject: Re: Subclassing
  • From: Andy Lee <email@hidden>
  • Date: Fri, 18 Apr 2008 19:09:21 -0400


On Apr 18, 2008, at 5:56 PM, K. Darcy Otto wrote:

I am working on a program with a complex hierarchy of classes, and I want to subclass one of the objects a few steps down on that hierarchy. Do I have to create a parallel hierarchy to do this? Here is the problem:

AppController instantiates a Deduction object.
The Deduction object instantiates a DeductionLine object.
The DeductionLine object instantiates a Dependency object.

When you say "instantiates," I assume you mean each object has the next-level object as an instance variable, and the instantiating happens in the -init method? And the problem is that the DeductionLine and Dependency classes are hard-coded in the init methods that instantiate them?


Assuming I've guessed correctly...

Perhaps you could add an init method in each class that takes an instance of the sub-object as an argument. For example, DeductionLine could have this as its new designated initializer:

- (id)initWithDependency:(Dependency *)dependency
{
   if ((self = [super init]))
   {
       _dependency = [dependency retain];
   }

   return self;
}

Then it could override -init to use a plain Dependency as the default:

- (id)init
{
return [self initWithDependency:[[[Dependency alloc] init] autorelease]];
}


You could do a similar -initWithDeductionLine: method for the Deduction class.

To create a Deduction that uses an SDependency two levels down, you could do:

Dependency *sdep =
   [[[SDependency alloc] init] autorelease];
DeductionLine *dedLine =
   [[[DeductionLine alloc] initWithDependency:sdep] autorelease];
Deduction *ded =
   [[[Deduction alloc] initWithDeductionLine:dedLine] autorelease];


You could simplify things by wrapping some of the above in convenience methods that would enable you to do this...


Deduction *ded = [Deduction deductionWithDependency:[SDependency dependency]];

...or even this if it's not too implementation-revealing...

Deduction *ded = [Deduction deductionWithSDependency];


Another approach might involve using setter methods like - setDependency: etc. But it sounds like the structure of your code requires a DeductionLine to have a valid Dependency instance as soon as it is instantiated.


--Andy




I want to subclass Dependency and override a few things. Can I do this without modifying DeductionLine? As it stands, I think I'm going to have to do this:

AppController instantiates a SDeduction object (where SDeduction is a subclass of Deduction).
The SDeduction object instantiates a SDeductionLine object (where SDeductionLine is a subclass of DeductionLine).
The SDeduction object instantiates a SDependency object (where SDependency is a subclass of Dependency).


The only reason to subclass Deduction (with SDeduction) and then DeductionLine (with SDeductionLine) is to get an SDependency object. Is there an easier way? I suppose I could just copy the Dependency class files and then modify it for this particular project; but I was hoping for a different way. Thanks.
_______________________________________________


Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Subclassing (From: "K. Darcy Otto" <email@hidden>)

  • Prev by Date: Re: Capsule-Style Toolbar Controls
  • Next by Date: Re: Subclassing
  • Previous by thread: Re: Subclassing
  • Next by thread: Re: Subclassing
  • Index(es):
    • Date
    • Thread