Subclassing best practice
Subclassing best practice
- Subject: Subclassing best practice
- From: John Stiles <email@hidden>
- Date: Wed, 14 Nov 2007 09:39:38 -0800
I have some code which subclasses NSWindow to tack on some data of my
own:
@interface MyWindow : NSWindow
{
int myStuff;
MyObject * myMoreStuff;
}
- (int) stuff;
- (MyObject*) moreStuff;
@end
@implementation MyWindow
/* etc */
@end
This is super-useful for the project I'm working on since it allows
me to keep all my data together.
However, now I have a situation where I need NSPanel to have these
members as well. So I wrapped up my @interface in a macro:
#define CreateWindowSubclass(newtype, basetype) \
@interface newtype : basetype \
{ \
int stuff; \
MyObject * moreStuff; \
} \
- (int) stuff; \
- (MyObject*) moreStuff; \
@end \
@implementation newtype \
/* etc */ \
@end
CreateWindowSubclass(MyWindow, NSWindow)
CreateWindowSubclass(MyPanel, NSPanel)
This works great. However, from a code design perspective, it seems
sloppy to me. I don't like having to rely on macros.
In other languages, I could use multiple inheritance, e.g. if this
were C++ I could do this:
struct MyWindowExtensions {
int stuff;
MyObject * moreStuff;
};
class MyWindow : public NSWindow, public MyWindowExtensions {};
class MyPanel : public NSPanel, public MyWindowExtensions {};
But ObjC doesn't support multiple inheritance.
I've run up against this problem with views as well, and I've managed
to get some mileage out of categories, but there are a lot of
situations where you just need to add members.
So what is the best design pattern for something like this? Anything
nicer than a good old fashioned #define?
_______________________________________________
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