CSC/ECE 517 Summer 2008/wiki2 8 jb: Difference between revisions
Line 11: | Line 11: | ||
[http://en.wikipedia.org/wiki/Delegation_(programming) Delegation], sometimes referred to as [http://en.wikipedia.org/wiki/Object_composition#Aggregation], is the concept that one class may contain an instance of, or a reference to, another class, and <i>delegate</i> some responsibility to that class. | [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation], sometimes referred to as [http://en.wikipedia.org/wiki/Object_composition#Aggregation], is the concept that one class may contain an instance of, or a reference to, another class, and <i>delegate</i> some responsibility to that class. | ||
Aggregation can take on another connotation, specific to data modeling, closely related to [http://en.wikipedia.org/wiki/Object_composition composition]. Composition refers to one class owning, and therefore being responsible for the memory allocation and deallocation, another class. In this context, aggregation refers to a class simply having a reference to another class, but not taking ownership. | Aggregation can take on another connotation, specific to data modeling, closely related to [http://en.wikipedia.org/wiki/Object_composition composition]. Composition refers to one class owning, and therefore being responsible for the memory allocation and deallocation, another class via the [http://en.wikipedia.org/wiki/Has-a has-a] relationship. In this context, aggregation refers to a class simply having a reference to another class, but not taking ownership. | ||
For the purposes of this wiki, we will use the term delegation to mean that one class <i>delegates</i> responsibility to another class that it contains. In this context, the issue of ownership is irrelevant. | For the purposes of this wiki, we will use the term delegation to mean that one class <i>delegates</i> responsibility to another class that it contains. In this context, the issue of ownership is irrelevant. |
Revision as of 19:38, 20 June 2008
This wiki will explore the age old debate on inheritance vs. delegation, showing the strengths and weakness of each approach, and where each approach is preferred.
Background
An in-depth description Inheritance and Delegation is out of the scope of this wiki, but a brief description of each subject will be given to get us started.
Inheritance
Inheritance is one of the fundamental tenets of object oriented programming. Inheritance refers to the ability to model hierarchies classes that are related to each other through the is-a relationship. It is commonly agreed upon that inheritance done correctly must conform to the Liskov substitution principle.
Delegation
Delegation, sometimes referred to as [1], is the concept that one class may contain an instance of, or a reference to, another class, and delegate some responsibility to that class.
Aggregation can take on another connotation, specific to data modeling, closely related to composition. Composition refers to one class owning, and therefore being responsible for the memory allocation and deallocation, another class via the has-a relationship. In this context, aggregation refers to a class simply having a reference to another class, but not taking ownership.
For the purposes of this wiki, we will use the term delegation to mean that one class delegates responsibility to another class that it contains. In this context, the issue of ownership is irrelevant.
Inheritance
Pros
Cons
Delegation
Pros
Cons
Links
Wikipedia: Inheritance
Wikipedia: Delegation
Wikipedia: Composition
Replace Inheritance With Delegation design pattern
Dr. Dobbs: Composition vs. Inheritance