delegate - 概念


對於這個文件中有相當繞舌感覺的設計模式,看了實在滿頭霧水。

The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled.The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.

in Cocoa Core Competencies

委託有兩個角色,一個是「委託者」,另一個是「被委託者」。用中文講是這樣,用英文的話到底delegating object是委託者,還是受委託者?,應該要用英文原本的理解方式比較好。the delegate在英文裡有議員或代表的意思,在代議制度中,即被公民選票選出來的代表,負責參與政治。所以可以把它說是「the delegate」就是「被選中的代表(議員)」,那另外文中說到的「delegating object」,就把它當作「要拜託議員關說的選民」,這樣就很清楚了。我亂翻一下上面的一段:

要拜託議員關說的選民,會保留另一個人(物件)- 議員 的電話連絡資料(參考),然後在需要的時候打電話給他。這樣可以傳達訊息給議員,訊息包含選民他現在正急需處理的事情,或是剛剛處理到的事情。議員可能叫選民自己,或其他在應用程式中的人(物件),更改外觀或目狀態的方式來回應這些訊息,而且在某些情況他可以回傳會影響即將到來的事件如何被處理的數值。代議(委託)制度的最主要的價值,就是它可以讓你很容易的客製化在一個中心物件中的一些物件行為。

The delegating object is typically a framework object, and the delegate is typically a custom controller object. In a managed memory environment, the delegating object maintains a weak reference to its delegate; in a garbage-collected environment, the receiver maintains a strong reference to its delegate.

in Cocoa Core Competencies

所以在Cocoa中,發出委託的物件(選民),都是框架物件,就是直接回應使用者事件的那個物件。另一個文件中有說明如下:

The delegating object is often a responder object—that is, an object inheriting from NSResponder in AppKit or UIResponder in UIKit—that is responding to a user event.

in Concepts in Objective-C Programming

而議員(或代表),主要是作委託控制的,「控制」這個字眼很重要,其實它如果套用在MVC設計模式中,是屬於C(控制器)的一部份,以下是官網說明:

The delegate is an object that is delegated control of the user interface for that event, or is at least asked to interpret the event in an application-specific manner…The delegate is almost always one of your custom objects, and by definition it incorporates application-specific logic that the generic and delegating object cannot possibly know itself.

in Concepts in Objective-C Programming

這一段說明了這個機制的用途,還有委託是讓物件A不需要透過繼承物件B,就可以更動物件B的行為的機制。:

The programming mechanism of delegation gives objects a chance to coordinate their appearance and state with changes occurring elsewhere in a program, changes usually brought about by user actions. More importantly, delegation makes it possible for one object to alter the behavior of another object without the need to inherit from it.

in Concepts in Objective-C Programming

delegate的實現要使用protocol的機制(事實上objective-c語言的protocol在Java或其他語言中,是interface的概念。而objective-c的interface是用在類別宣告的…很容易搞亂~"~)。要了解delegate,首先要了解以下三個參與部份:

Protocol

類似Java語言的介面。可用@optional和@required宣告方法是否必需要被實作(預設(不寫)即是@required)

Selector

屬於動態連繫(Dymanic binding),下面有比較好的解釋,也有應用的範例情況:

The selector operator provides a way to refer to a method provided by an object, somewhat similar to a function pointer in C. It is useful because it allows you to decouple the process of calling methods on an object. For example one piece of code could provide a method, and another piece of code could apply that method to a given set of objects.

in Explanation of Cocoa @selector usage
//Test to see if an object implements a certain method:

[object respondsToSelector:@selector(methodName)]

//Store a method to later call on an object;

SEL method = @selector(methodName);
[object performSelector:method];

//Call a method on a different thread (useful for GUI work).
[object performSelectorOnMainThread:@selector(methodName)]

id

通用型的未知類別的物件參考(指標),nil可以用來判斷一個id值是否存在:

in Objective-C, object identifiers are of a distinct data type: id. This type is the general type for any kind of object regardless of class and can be used for instances of a class and for class objects themselves. The keyword nil is defined as a null object, an id with a value of 0. id, nil, and the other basic types of Objective-C are defined in the header file objc/objc.h.