• 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: Forwarding messages to another class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Forwarding messages to another class


  • Subject: Re: Forwarding messages to another class
  • From: Quincey Morris <email@hidden>
  • Date: Sun, 07 Jun 2015 01:51:02 +0000

On Jun 6, 2015, at 17:04 , Cosmo <email@hidden> wrote:
>
> Sorry. I was inaccurate in my language. I’m actually calling these methods on the superclass, not on instances of it.

What you’re really doing here is a combination of two standard Obj-C patterns:

1. Singleton pattern. You’re using the class objects (that is, the meta-objects that represent the classes themselves) as singleton objects — as the single object that implements certain behavior.

There’s nothing absolutely wrong with this, but it’s more usual to create a single instance of a class instead of implementing the behavior as class methods. There are a few drawbacks to using the class object, the most obvious one being that class objects can’t have @property-style properties.

2. Delegation pattern. In “decoupling” the secondary (subclass) objects from the primary (base class) object, clients of your class API are sending messages to one object, which then has another object do some or all of the work.

In your case, since the API of the primary object and the API of the delegate is the same (they respond to the same methods), perhaps it would be more accurate to call this a proxy pattern rather than a delegate pattern, but the idea is much the same.

Again, there’s nothing absolutely wrong with the way you’re trying to do this, but in modern Obj-C the best way is to write the API into a protocol, and to have the primary class adopt the protocol. As far as clients of the class are concerned, the “decoupled” objects are an implementation detail they don’t care about, so the method of delegation is an implementation detail, too. There’s no need, for example, for the secondary objects to be subclasses of the primary object. For simplicity, you can choose to just make the secondary objects conform to the same protocol.

I’d avoid calling this “forwarding”, because that term has a very specific meaning in Obj-C. There is in fact a formal method forwarding mechanism, but you’ve re-invented it informally.

In terms of what’s currently going wrong, you have all the information you need to debug this. You can set breakpoints, and you can examine the result of '[self classToUseForBackend]’ in the debugger, so you should be able to see what’s going on.



_______________________________________________

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


References: 
 >Forwarding messages to another class (From: Cosmo <email@hidden>)
 >Re: Forwarding messages to another class (From: Quincey Morris <email@hidden>)
 >Re: Forwarding messages to another class (From: Cosmo <email@hidden>)
 >Re: Forwarding messages to another class (From: Graham Cox <email@hidden>)
 >Re: Forwarding messages to another class (From: Cosmo <email@hidden>)

  • Prev by Date: Re: Using CFSTR() with const char * variable
  • Next by Date: Re: why is this Swift initializer legal
  • Previous by thread: Re: Forwarding messages to another class
  • Next by thread: Re: Forwarding messages to another class
  • Index(es):
    • Date
    • Thread