CSC/ECE 517 Summer 2008/wiki2 8 jb: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
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.
This wiki will explore the age old debate of inheritance vs. delegation, showing the strengths and weakness of each approach, and where each approach is preferred.


==Background==
==Background==
Line 11: Line 11:
[http://en.wikipedia.org/wiki/Delegation_(programming) Delegation], sometimes referred to as [http://en.wikipedia.org/wiki/Object_composition#Aggregation aggregation], is the concept that one class may contain an instance of another class, and <i>delegate</i> some responsibility to that class. This is also referred to as the [http://en.wikipedia.org/wiki/Has-a has-a] relationship. Aggregation is closely related to [http://en.wikipedia.org/wiki/Object_composition composition]. Both aggregation and composition are used to describe one object containing another object, but composition implies ownership [http://en.wikipedia.org/wiki/Object_composition#Aggregation]. Aggregation is more general and doesn't imply any responsibilities for memory management.
[http://en.wikipedia.org/wiki/Delegation_(programming) Delegation], sometimes referred to as [http://en.wikipedia.org/wiki/Object_composition#Aggregation aggregation], is the concept that one class may contain an instance of another class, and <i>delegate</i> some responsibility to that class. This is also referred to as the [http://en.wikipedia.org/wiki/Has-a has-a] relationship. Aggregation is closely related to [http://en.wikipedia.org/wiki/Object_composition composition]. Both aggregation and composition are used to describe one object containing another object, but composition implies ownership [http://en.wikipedia.org/wiki/Object_composition#Aggregation]. Aggregation is more general and doesn't imply any responsibilities for memory management.


==Inheritance==
==The Debate==


===Pros===
<ul>
===Cons===
<li>Inheritance breaks encapsulation [http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance] </li>
<li>Inheritance increases coupling [http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance] </li>
<li>Inheritance is static and determined at compile time http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance </li>
</ul>


==Delegation==
===Pros===
===Cons===


==Links==
==Links==
Line 28: Line 27:
[http://dobbscodetalk.com/index.php?option=com_myblog&show=Composition-vs-Inheritance.html&Itemid=29 Dr. Dobbs: Composition vs. Inheritance]
[http://dobbscodetalk.com/index.php?option=com_myblog&show=Composition-vs-Inheritance.html&Itemid=29 Dr. Dobbs: Composition vs. Inheritance]
[http://www.gamearchitect.net/Articles/GameObjects1.html Good debate]
[http://www.gamearchitect.net/Articles/GameObjects1.html Good debate]
[http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance Great article covering delgation and composition]

Revision as of 02:18, 21 June 2008

This wiki will explore the age old debate of 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 discussion of each subject will help 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 aggregation, is the concept that one class may contain an instance of another class, and delegate some responsibility to that class. This is also referred to as the has-a relationship. Aggregation is closely related to composition. Both aggregation and composition are used to describe one object containing another object, but composition implies ownership [1]. Aggregation is more general and doesn't imply any responsibilities for memory management.

The Debate


Links

Wikipedia: Inheritance
Wikipedia: Delegation
Wikipedia: Composition
Replace Inheritance With Delegation design pattern
Dr. Dobbs: Composition vs. Inheritance Good debate Great article covering delgation and composition