Mailing Lists: Apple Mailing Lists

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

Re: NSObject -poseAs:



On May 31, 2007, at 3:46 PM, Mike R. Manzano wrote:

You can call super on a class (static) method? Neat.

A static method in C++, Java, or C# is just a function in a namespace. A class method in Objective-C is a true class method like you'd have in Smalltalk, so self, super, etc. will all do the right thing.


Remember that Objective-C is a traditional object-oriented language: You program by sending messages to objects. Messages are always dispatched at runtime, and classes themselves are objects too.

The practical upshot of this is that certain design patterns that are used to work around the lack of dynamic dispatch in C++, Java, and C# are unnecessary when working in Objective-C, Smalltalk, Python, Ruby, or other true object-oriented languages with dynamic dispatch.

One example is the use of factory and abstract factory patterns for decoupling. In a dynamic language, it can be a code smell since the classes themselves can be treated like factories.

For example, your design sounds something like this:

  @interface Something : NSObject
  - (int)value;
  @end

  @interface SomethingFactory : NSObject {
  - (Something *)defaultSomething;
  @end

  @interface SomethingFactoryForTesting : SomethingFactory
  @end

You allocate the factory and ask it for the defaultSomething. In your test, to get a Something you can use for testing, you have SomethingFactoryForTesting pose as SomethingFactory.

You can do this much more easily, since Objective-C is a true object- oriented language, using class methods and subclassing:

  @interface Something : NSObject
  + (id)defaultSomething;
  - (int)value;
  @end

Your regular code can just ask Something for its defaultSomething. For your test, you can write a subclass:

@interface SomethingForTesting : NSObject
+ (id)defaultSomething; // returns a different singleton than its superclass
@end


Then just use that subclass in your test code instead of its parent. Everything else will still use the parent class, but your test code will use the subclass.

  -- Chris

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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
References: 
 >NSObject -poseAs: (From: "Mike R. Manzano" <email@hidden>)
 >Re: NSObject -poseAs: (From: "Clark Cox" <email@hidden>)
 >Re: NSObject -poseAs: (From: "Mike R. Manzano" <email@hidden>)
 >Re: NSObject -poseAs: (From: Jim Brownfield <email@hidden>)
 >Re: NSObject -poseAs: (From: "Mike R. Manzano" <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.