• 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
Re: is protected broken, or am I?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: is protected broken, or am I?


  • Subject: Re: is protected broken, or am I?
  • From: Drew McCormack <email@hidden>
  • Date: Fri, 11 Oct 2002 08:43:20 +0200

On Friday, October 11, 2002, at 12:42 AM, matt neuburg wrote:
Because it is that other instance that has the ivar *values* I want. - Look, here's a picture of my nib file:

MyClass<----------->window
(File's Owner)

There is a bunch of code in MyClass, and there are a lot of connections set up between the MyClass instance and the window in the nib (represented by the double-headed arrow). Now I have a bunch of *other* code that governs this *other* behavior of the *same* window. The question is, where shall I put this code?

I don't want to put it into MyClass because MyClass is already huge, and also because this will mean that just about every method in MyClass will be a huge if/then statement doing one thing on one set of occasions and another thing on another set of occasions.
There are times when if/thens are difficult to avoid. These usually occur in the controller objects, which are responsible for creation. Creation of objects is difficult to make object oriented, because the compiler/runtime can't possibly know what class of object you want. You have to tell it that by branching, based on input for example, and creating the object you need. One way to reduce the amount of branching significantly, when families of classes are related by the manner in which they are created, is the abstract factory design pattern ("Design Patterns", Gamma et al.)

The point of all this is that your branching is not necessary easily avoidable: it depends why you are branching.


That is just the sort of the problem that classes are supposed to solve. Instead of if/then, you just supply a different class, and the right thing happens because whichever class the message is sent to an instance of, it contains the right code.
This is true, but I don't think just subclassing MyClass is likely to introduce the type of branching you need.


But how can I slot that code into the above picture? Basically I want that code to be where MyClass's code is in the picture - with access to all those connections to the window. But I can't do that, because MyClass's code is already there, if you see what I mean. The File's Owner proxy already represents an instance, an instance of MyClass, which owns the nib.
This is almost the perfect description of when you would use a category in objective-c. Categories allow you to extend an existing class, or override it's method, without subclassing. It helps to break large (usually controller) classes into smaller units. They are very easy to do, too. Read Apples intro to Obj-C in your developer documentation on your hard disk.

Basically it goes like this:

// MyClass.h
@interface MyClass : NSObject {
}
@end

// MyClass.m
@implementation MyClass
@end

// MyClassExtensions.h
@interface MyClass (Extensions)
@end

// MyClassExtensions.m
@implementation MyClass (Extensions) // I think the "Extensions" is needed here, but am not sure. Check docs.
@end


Drew
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
Dr. Drew McCormack
Trade Strategist (www.trade-strategist.com)
Trading simulation software for Mac OS X
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
  • Prev by Date: Re: why mouseMoved doesn't work...
  • Next by Date: Re: case-insensitive strings
  • Previous by thread: Re: is protected broken, or am I?
  • Next by thread: Re: is protected broken, or am I?
  • Index(es):
    • Date
    • Thread