Inheritance vs Delegation:
- Inheritance: Allow one class (Child class) inherits attributes and behaviors from another class (Parent class), promoting code reusability.
- Delegation: One class delegates duties to another class which reduces coupling.
- Create a subclass of SortedIntList with inheritance that counts how many elements have been added to the list.
- Create a class to extend SortedIntList with delegation to count how many elements have been added to the list.
- Discuss questions regarding the tradeoffs and limitations between inheritance and delegation with a TA (no written answer needed).
Fork and clone the repo from https://github.com/CMU-17-214/f23-lab05
The repo has both Java and Typescript codes so feel free to implement in either of the languages you feel comfortable. Please try out the other language in your own time to gain an understanding of inheritance & delegation in both languages.
You will examine the strengths and weaknesses of inheritance and delegation by using both techniques to add a feature to a SortedIntList
class. The SortedIntList
class is one of a family of integer lists. It is very similar to the AbstractIntList
, except it stores its elements in ascending order.
We want to instrument a SortedIntList
to count how many elements have been added since it was created. (This is not the same as its current size, which is reduced when an element is removed). To provide this functionality getTotalAdded
, you should count the number of attempted insertions and also provide an access method to get this count. Your solutions should use inheritance and delegation so you can reuse, but not modify, the original SortedIntList
implementation. Please refer to the appendix for the UML diagram.
We have provided an empty InheritanceSortedIntList
class where you should implement an inheritance-based solution.
Hints:
InheritanceSortedIntList
should extendSortedIntList
.- The
SortedIntList
contains two methods that add elements,add
andaddAll
.- You should override both of these methods to track how many elements have been added.
- Make sure you check that
getTotalAdded
works with bothadd
andaddAll
.
After you have implemented the InheritanceSortedIntList
class, test the instrumentation you just added using tests in InheritanceSortedIntListTest
class.
We have provided an empty DelegationSortedIntList
class where you should implement your delegation-based solution.
Hints:
- Ensure your class implements the
IntegerList
interface.- Create a private
SortedIntList
instance for delegation.- Delegate list operations to the
SortedIntList
instance.- Make sure you check that
getTotalAdded
works with bothadd
andaddAll
.- After you have implemented the
DelegationSortedIntList
class, test the instrumentation you just added using tests inDelegationSortedIntListTest
class.
Run the tests and make sure your instrumentation passes all the tests.
(You might want to use the printList
helper we provided you and read the documentation for addAll
in the AbstractIntList
class.)
Evaluate your two implementations for the given problem and answer the following questions:
- Which is more dependent on the implementation details of the
SortedIntList
, delegation or inheritance? - If the
add
method inSortedIntList
is significantly modified or its behavior changes, which implementation is more likely to break? - What issues does using delegation solve that might have been problematic with inheritance?
- Based on the provided implementations, when would it be more appropriate to use inheritance and when to use delegation?
Take a look at the UML diagram to see what each function should implement.