Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Subclassing best practice



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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.