CSC/ECE 517 Fall 2007/wiki2 10 c4: Difference between revisions
No edit summary |
|||
Line 30: | Line 30: | ||
=== When to use Delegation=== | === When to use Delegation=== | ||
* | * Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3] | ||
* | * Delegation is used in has-a relationships where the type of object that is included may change during runtime. | ||
* | * Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6] | ||
* When code execution within objects needs to be determined dynamically. | * When code execution within objects needs to be determined dynamically. | ||
As for delegation, I tend to find this appearing when I find that a class is really implementing more than one discrete object. A new class can be extracted from the original one, which makes each one more cohesive, in that they more tightly implement the data and functions associated with one logically separate, er, thing. | As for delegation, I tend to find this appearing when I find that a class is really implementing more than one discrete object. A new class can be extracted from the original one, which makes each one more cohesive, in that they more tightly implement the data and functions associated with one logically separate, er, thing. | ||
Line 58: | Line 49: | ||
==Examples== | ==Examples== | ||
Revision as of 23:13, 22 October 2007
Introduction
Problem
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.
Inheritance
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]
Delegation
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]
Inheritance vs Delegation
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly.
When to use Inheritance
- Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]
- Inheritance is used in is-a relationships between objects. [3]
- Inheritance is used to sub categorize objects.[3]
- Inheritance should be restricted to objects of the same type.[3]
When to use Delegation
- Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]
- Delegation is used in has-a relationships where the type of object that is included may change during runtime.
- Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]
- When code execution within objects needs to be determined dynamically.
As for delegation, I tend to find this appearing when I find that a class is really implementing more than one discrete object. A new class can be extracted from the original one, which makes each one more cohesive, in that they more tightly implement the data and functions associated with one logically separate, er, thing.
[3]
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.
Examples
References
- http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html
- http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html
- http://www.tek-tips.com/viewthread.cfm?qid=372254
- http://www.python.org/ftp/python/doc/delegation.ps
- http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf
- http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html