Appropriate use of bindings?

  • I'm having a slight dilemma in deciding which I should use, notifications or bindings.  Here is the background:

    I have a bunch of C++ code that uses something akin to delegates; you derive from a base class, pass that into an instance of a server class, and the server class calls the delegate at appropriate times.  I wanted to cleanly encapsulate all of this in Objective-C, so that the rest of my code can be pure Objective-C, so wrote wrappers for each base class.  Now, I pass the internally wrapped C++ instance to the server, while the rest of my code will hook up to Objective-C wrapper.

    My dilemma is how to do this hookup; I can use either notifications or bindings, but I can't decide which is better in this situation.  Bindings seem to be simpler to use, but reading about them suggests that you are expected to use the MVC design pattern, and I'm not doing MVC here. Notifications are used anywhere you want, but require more (and more careful) work to get right.  So, are bindings considered general purpose, or should they really be reserved for MVC situations (even though they can work anywhere)?

    Thanks,
    Cem Karan
  • On Tue, Oct 7, 2008 at 9:31 AM, Karan, Cem (Civ, ARL/CISD)
    <CKaran...> wrote:

    > My dilemma is how to do this hookup; I can use either notifications or bindings, but I can't decide which is better in this situation.  Bindings seem to be simpler to use, but reading about them suggests that you are expected to use the MVC design pattern, and I'm not doing MVC here. Notifications are used anywhere you want, but require more (and more careful) work to get right.  So, are bindings considered general purpose, or should they really be reserved for MVC situations (even though they can work anywhere)?

    This is hard to answer in the abstract but in an attempt to color your
    thinking...

    KVO is about making _properties_ of an object observable. For example
    did the name of person object change or the current mode of a I/O
    connection object change. It gives you the ability to observe at the
    granularity of a property of an object (including a hierarchy of
    object using key-paths).

    Additionally once you support KVO then you can leverage the controller
    layer (bindings) to wire up UI, etc. directly to properties of your
    objects (including a hierarchy of object using key-paths).

    Notifications are a generic decoupled notification system that allows
    observers to listen for specific notifications (by name) possibly from
    specific objects, etc. These typically are more about something
    happening and less about a property of an object changing (however the
    later can be considered a special case of the former).

    You get "better" granularity for "free" with KVO but with a cost of
    having one funnel point
    (observeValueForKeyPath:ofObject:change:context:) into the observer
    unless you decide to use KVB (bindings) to bind a property of the
    observed object to a property of observing object. Also support for
    key paths and dependent keys can be a win depending on what you need.

    You get "better" arbitrary notification ability using notifications
    and the ability to coalesce, etc. if you utilize your own notification
    queues. Also you get to specify the entry point (selector) into your
    observer when can result in cleaner code.

    -Shawn
  • On Tue, Oct 7, 2008 at 10:44 AM, Shawn Erickson <shawnce...> wrote:
    > On Tue, Oct 7, 2008 at 9:31 AM, Karan, Cem (Civ, ARL/CISD)
    > <CKaran...> wrote:
    >
    >> My dilemma is how to do this hookup; I can use either notifications or bindings, but I can't decide which is better in this situation.  Bindings seem to be simpler to use, but reading about them suggests that you are expected to use the MVC design pattern, and I'm not doing MVC here. Notifications are used anywhere you want, but require more (and more careful) work to get right.  So, are bindings considered general purpose, or should they really be reserved for MVC situations (even though they can work anywhere)?
    >
    > This is hard to answer in the abstract but in an attempt to color your
    > thinking...

    I should note that using neither KVO or notification is also an
    option... You could have your "bridging" objects support the concept
    of a delegate or delegates (in this case think listener). Then objects
    that want "notifications" from one of your bridge objects add
    themselves as a delegate of that object. It would then implement the
    informal protocol methods (delegate methods) that it cares about so it
    will be messaged when the bridge detects things have changed (but only
    for the subset of things they happen to care about).

    This is a more direct coupling which can be good or bad depending on
    what you are doing...

    -Shawn
  • The following article discusses five different approaches to the
    Observer Pattern in Cocoa, it may be of some use:
    http://cocoawithlove.com/2008/06/five-approaches-to-listening-observing.htm
    l


    - Dave

    On 7 Oct 2008, at 17:31, Karan, Cem (Civ, ARL/CISD) wrote:

    > I'm having a slight dilemma in deciding which I should use,
    > notifications or bindings.  Here is the background:
    >
    > I have a bunch of C++ code that uses something akin to delegates;
    > you derive from a base class, pass that into an instance of a server
    > class, and the server class calls the delegate at appropriate
    > times.  I wanted to cleanly encapsulate all of this in Objective-C,
    > so that the rest of my code can be pure Objective-C, so wrote
    > wrappers for each base class.  Now, I pass the internally wrapped C+
    > + instance to the server, while the rest of my code will hook up to
    > Objective-C wrapper.
    >
    > My dilemma is how to do this hookup; I can use either notifications
    > or bindings, but I can't decide which is better in this situation.
    > Bindings seem to be simpler to use, but reading about them suggests
    > that you are expected to use the MVC design pattern, and I'm not
    > doing MVC here. Notifications are used anywhere you want, but
    > require more (and more careful) work to get right.  So, are bindings
    > considered general purpose, or should they really be reserved for
    > MVC situations (even though they can work anywhere)?

    ------
    David Kennedy (http://www.zenopolis.com)
  • > ------------------------------
    >
    > Message: 16
    > Date: Tue, 7 Oct 2008 19:19:27 +0100
    > From: Citizen <citizen...>
    > Subject: Re: Appropriate use of bindings?
    > To: Cocoa List <cocoa-dev...>
    > Message-ID: <CA7AFC39-AFC3-495F-B0DA-E3AE848F7686...>
    > Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
    >
    > The following article discusses five different approaches to the
    > Observer Pattern in Cocoa, it may be of some use:
    > http://cocoawithlove.com/2008/06/five-approaches-to-listening-
    > observing.html

    Thanks... it still doesn't solve my problem as I have bits that can fit
    into each of the categories, and all from the same object... I guess
    I'll just go with notifications, they've got enough flexibility that I
    can change things around however I need, should I need to.

    Thanks,
    Cem Karan