委託 (面向對象程序設計)

面向對象程序設計中的委託是指使用另一個對象(發送者)的上下文,對一個對象(接收者)的成員(屬性方法)求值。通過把發送者對象傳遞給接收者對象,任何面向對象語言都可以做顯式的委託。如果語言特性支持成員查詢規則,則可以做隱式的委託。隱式委託是基於原型編程中行為重用的基本方法,對應於基於類編程繼承。支持委託的最知名語言是SelfJavaScript

術語委託在兩個對象之間還有別的用法。見委託 (編程)。最容易混淆的是在接收者的上下文中,與發送者成員對應的接收者成員被求值,精確地說這是轉發(即包裝者對象(wrapper object)並不把自身傳遞給被包裝對象(wrapped object))。[1][2][a]簡單地使用另一個對象,這是對象聚合委託模式軟件設計模式中實現委託的一種套路。

概述 編輯

1986年,MIT計算機科學與人工智慧實驗室亨利·利伯曼英語Henry Lieberman首創發表了基於原型編程、利用方法查詢規則的面向對象的委託等技術和概念。[3]

委託依賴於動態綁定,因為它需要一個給定的方法可以在不同的運行時上下文中被調用。這可以用於操作系統提供單獨一個類管理各個窗口。例如,當用戶點擊close box, 窗口管理器發送委託:windowShouldClose調用;如果根據窗口的上下文有未保存數據,委託會延遲關閉這個窗口。

不同於轉發,委託可以被刻畫為自身的遲綁定:[4]

... messages sent to the self (or this) variable in the parent will "come back" to the object that originally received the message.

也就是說,接收對象的一個方法定義中的self並不是靜態綁定到定義時的那個對象,而是在求值時才綁定到「最初」的對象。

有觀點認為委託優於繼承,使程序代碼更為可多、可理解。[5]有觀點認為委託和繼承是等價的,但也有認為一個是另一個特例。[6]

語言支持 編輯

使用類似於C#/Java的偽代碼的示例:

class A {
  public void foo() {
    // "this" also known under the names "current", "me" and "self" in other languages
    this.bar();
  }

  void bar() {
    Console.WriteLine("a.bar");
  }
};

class B {
  private delegate A a; // delegation link

  public B(A a) {
    this.a = a;
  }

  public void foo() {
    a.foo(); // call foo() on the a-instance
  }

  public void bar() {
    Console.WriteLine("b.bar");
  }
};

a = new A();
b = new B(a); // establish delegation between two objects
b.foo();  
b.bar();

調用b.foo()將打印b.bar, 因為在a的上下文中this引用到最初的接收者對象bthis的結果的二義性被稱作object schizophrenia英語object schizophrenia

把隱式的this翻譯為顯式參數,調用(在B中, a是個委託) a.foo()翻譯為A.foo(b),使用a類型做方法的查詢解析, 但調用委託的對象b作為this實參。

使用繼承,類似代碼為:

class A {
  void foo() {
    this.bar();
  }

  void bar() {
    print("A.bar");
  }
};

class B extends A {
  public B() {}

  void foo() {
    super.foo(); // call foo() of the superclass (A)
  }

  void bar() {
    print("B.bar");
  }
};

b = new B();

調用b.foo()的結果為B.bar. 其中this.bar()解析為子類的方法.

注釋 編輯

  1. ^ Beck 1997 uses the terms "simple delegation" for when the receiving object does not have access to the sending object, and "self delegation" for when the receiving object does have access to the sending object; in modern language these are "forwarding" and "delegation", as used in this article.

參考文獻 編輯

  1. ^ Gamma et al. 1995,"Delegation", pp. 20–21.
  2. ^ Beck 1997,"Delegation", pp. 64–69.
  3. ^ Lieberman, Henry. Using prototypical objects to implement shared behavior in object-oriented systems. Conference proceedings on Object-oriented programming systems, languages and applications 21. Portland. 1986: 214–223 [2022-04-15]. ISSN 0362-1340. doi:10.1145/960112.28718. (原始內容存檔於2021-09-04).  |journal=被忽略 (幫助); |issue=被忽略 (幫助)
  4. ^ Intersecting Classes and Prototypes. Perspectives of Systems Informatics: 5th International Andrei Ershov Memorial Conference, PSI 2003, Akademgorodok, Novosibirsk, Russia, July 9-12, 2003, Revised Papers. : 38. 
  5. ^ [1]頁面存檔備份,存於網際網路檔案館Trygve Reenskaug, Dept. of Informatics, University of Oslo, "The Case for Readable Code" (2007)
  6. ^ Stein, Lynn Andrea. Delegation is Inheritance. OOPSLA '87 Conference proceedings on Object-oriented programming systems, languages and applications: 138–146. doi:10.1145/38807.38820. 
  • Lieberman, Henry. Using prototypical objects to implement shared behavior in object-oriented systems. Conference proceedings on Object-oriented programming systems, languages and applications 21. Portland. 1986: 214–223 [2022-04-15]. CiteSeerX 10.1.1.48.69 . doi:10.1145/960112.28718. (原始內容存檔於2021-09-04).  |journal=被忽略 (幫助); |issue=被忽略 (幫助)
  • Lynn Andrea Stein, Henry Liberman, David Ungar: A shared view of sharing: The Treaty of Orlando. In: Won Kim, Frederick H. Lochovsky (Eds.): Object-Oriented Concepts, Databases, and Applications ACM Press, New York 1989, ch. 3, pp. 31–48 ISBN 0-201-14410-7 (online at Citeseer頁面存檔備份,存於網際網路檔案館))
  • Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. 1995. ISBN 978-0-201-63361-0. 
  • Malenfant, J.: On the Semantic Diversity of Delegation-Based Programming Languages, Proceedings of the OOPSLA95, New York: ACM 1995, pp. 215–230.
  • Beck, Kent. Smalltalk Best Practice Patterns. Prentice Hall. 1997. ISBN 978-0134769042. 
  • Kasper Bilsted Graversen: The nature of roles---A taxonomic analysis of roles as a language construct. Ph.D. Thesis 2006, (Online at IT University of Copenhagen)

外部連結 編輯