2012年10月10日水曜日

Delegationの基礎

Delegation -- Your First Mac App.(2012-02-16版)より
Delegation is a design pattern in which one object sends messages to another object—specified as its delegate—to ask for input or to notify it that an event is occurring. Delegation is often used as an alternative to class inheritance to extend the functionality of reusable objects. For example, before a window changes size, it asks its delegate whether the new size is ok. The delegate replies to the window, telling it that the suggested size is acceptable or suggesting a better size. (For more details on window resizing, see the windowWillResize:toSize: message.)
 デレゲーション(和訳:委任、代理)とは、1つのデザインパターンである。イベントが起こった時にあるオブジェクトが他のオブジェクト(デレゲーションとして)にメッセージを送り、入力やイベントの通知を行う。デレゲーションは、再利用可能なオブジェクトの機能的な拡張を、クラスの継承の代わりに行うときに用いられる方法である。例えば、ウインドウサイズの変更(訳中:というイベント)がおこる前にそのデレゲートに対し新たなサイズの妥当性を問い合わせる。デレゲートはそのウインドウに対し許容範囲であるか、より適当なサイズを答える。

Delegate methods are typically grouped into a protocol. A protocol is basically just a list of methods. The delegate protocol specifies all the messages an object might send to its delegate. If a class conforms to (or adopts ) a protocol, it guarantees that it implements the required methods of a protocol. (Protocols may also include optional methods).

 デレゲートメソッドは典型的にはプロトコルに分類される。プロトコルは基本的には「ただ」のメソッドのリストである。デレゲートプロトコルはあるオブジェクトがそのデレゲートにおくるはずのすべてのメッセージを規定する。あるクラスがプロトコルを承認/採用しているとは、 そのクラスがあるプロトコルの(要求された)メソッドが実装されていることを保証していることを指す。

In this application, the application object tells its delegate that the main startup routines have finished by sending it the applicationDidFinishLaunching: message. The delegate is then able to perform additional tasks if it wants.

実例のアプリケーションではアプリケーションオブジェクトがそのデレゲートに対しメインスタートアップルーチンが終わったことを、applicationDidFinishLaunching:メッセージにて送信する。そしてデレゲートは必要であれば追加的なタスクを実行することができる(プログラマーがコードを追加することで)。

[まとめ]デレゲートとは、
・to ask for input or to notify it that an event is occurring
・an alternative to class inheritance to extend the functionality of reusable objects
・ typically grouped into a protocol

0 件のコメント:

コメントを投稿