• 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: NSObject -poseAs:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSObject -poseAs:


  • Subject: Re: NSObject -poseAs:
  • From: Chris Hanson <email@hidden>
  • Date: Thu, 31 May 2007 16:08:07 -0700

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSObject -poseAs:
      • From: "Mike R. Manzano" <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>)

  • Prev by Date: Fwd: Any way to run an NSInvocation on the main thread?
  • Next by Date: Re: Any way to run an NSInvocation on the main thread?
  • Previous by thread: Re: NSObject -poseAs:
  • Next by thread: Re: NSObject -poseAs:
  • Index(es):
    • Date
    • Thread