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

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


  • Follow-Ups:
    • Re: Subclassing best practice
      • From: "A.M." <email@hidden>
    • Re: Subclassing best practice
      • From: Uli Kusterer <email@hidden>
  • Prev by Date: Re: Setting Font Size (etc.) in Interface Builder 3.0
  • Next by Date: Bring app to foreground in 10.5
  • Previous by thread: Re: Setting Font Size (etc.) in Interface Builder 3.0
  • Next by thread: Re: Subclassing best practice
  • Index(es):
    • Date
    • Thread