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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/aglee%40mac.com

This email sent to [EMAIL PROTECTED]

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to