Open6

Delegate

ykwsykws

Cocoa APIs often provide protocols that include delegate methods. When an event occurs—such as a user resizing a window—a class that’s a delegator will detect the event and call delegate methods on the class you specify as the delegate. Delegate methods can customize how an app responds to an event.

日本語訳

Cocoa APIでは、多くの場合、デリゲートメソッドを含むプロトコルが提供されます。例えばユーザーがウィンドウのサイズを変更するなどのイベントが発生した際、デリゲータ(委譲者)クラスはそのイベントを検出し、あなたがデリゲートとして指定したクラスに対してデリゲートメソッドを呼び出します。デリゲートメソッドは、アプリがイベントにどのように対応するかをカスタマイズすることができます。

ykwsykws

デリゲートで実現したいことは、イベントに対してどのように反応するかであって、イベントの発生を制御することではない

デリゲートメソッドは、アプリがイベントにどのように対応するかをカスタマイズすることができます。

ykwsykws

Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. 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.

日本語訳

デリゲーション(委任)は、プログラム内のあるオブジェクトが別のオブジェクトを代表して、または協調して行動する、シンプルで強力なパターンです。デリゲートするオブジェクトは、他のオブジェクト(デリゲート)への参照を保持し、適切なタイミングでそれにメッセージを送信します。このメッセージは、デリゲートするオブジェクトが処理しようとしている、またはすでに処理したイベントについてデリゲートに通知します。デリゲートは、そのメッセージに応答して、自身またはアプリケーション内の他のオブジェクトの外観や状態を更新したり、場合によっては、間近のイベントの処理方法に影響を与える値を返すことができます。デリゲーションの主な価値は、一つの中央のオブジェクトで複数のオブジェクトの振る舞いを簡単にカスタマイズできることにあります。

ykwsykws

言っていることは変わっていない
デリゲータである委譲者は、与えられた責務を遂行し、もしデリゲートとして委譲先を指定されたら、メッセージを送信し、処理の一部を任せる
そうすることで、デリゲータの実装時に確定しない要求に対して、デリゲート側で柔軟に実装することが可能になるため、 SDK ではカスタマイズの可能な部分をデリゲートとして提供することがよく行われていた