Re: Subclassing
Re: Subclassing
- Subject: Re: Subclassing
- From: Manfred Schwind <email@hidden>
- Date: Sat, 19 Apr 2008 15:13:39 +0200
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.
I want to subclass Dependency and override a few things. Can I do
this without modifying DeductionLine?
If I understand it correctly, you already have a project that uses
these classes, and now you have a second project that also wants to
use the same code, but just for Dependency you want to use a specific
subclass of it?
In this case I would consider to create a "factory" to create the
Dependency object. A factory is a method that creates an instance of a
class.
Then you can define that this factory method has to be implemented for
every project separately (therefore I would define it as a category in
Obj-C). The factory then can return any subclass of Dependency as you
like for that particular project.
But the original project has to be modified to use the factory also.
@interface Dependency (Factory)
- (Dependency *)createDependency; // this is the factory
@end
The first project can provide an implementation of createDependency
that just creates a Dependency object:
@implementation Dependency (Factory)
- (Dependency *)createDependency {
return [[[Dependency alloc] init] autorelease];
}
@end
The second project can provide an implementation of createDependency
that creates an instance of a subclass of Dependency:
@interface SubDependency : Dependency {
}
...
@end
@implementation Dependency (Factory)
- (Dependency *)createDependency {
return [[[SubDependency alloc] init] autorelease];
}
@end
The code for your DeductionLine class has to be modified to always use
that factory whenever it has to instantiate a Dependency object, of
course.
Regards,
Mani
--
http://www.mani.de
iVolume - Loudness adjustment for iTunes.
LittleSecrets - The encrypted notepad.
_______________________________________________
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>) |