Question

Given is the following package/class diagram of an implementation of java.util.List:

Package/Class Diagram of java.util.List 1

1. Which design principle is (most likely) hurt by the ArrayBasedList class?

The design principle that is hurt by the ArrayBasedList class is the "Use inheritance as a Specialization" because the ArrayBasedList is (extends) a List that uses the ArrayUtilities. This principle is hurt, as inheritance should not be used for "usage" properties.

In addition, multiple inheritance is not supported by Java.

2. What would be the preferred way to model this situation in a correct way (as UML)?

The preferred way to model this situation in a correct way using UML will be to make the ArrayBasedList implement the List interface and use the ArrayUtilites.

In code this would be translated in:

package app.list;
import app.util;

public class ArrayBasedList implements List {
    
    private ArrayUtilities util = new ArrayUtilities ();
    public ArrayBasedList(){
    
    }
}

And in UML in this:

enter image description here

My solution was based on How should I have explained the difference between an Interface and an Abstract class?

What do you think about my approach?

Edit 13/01/2021: This question was part of an examination sheet in a Software related master program. I can understand that the question perhaps is a bit abstract but at the same time, after reading carefully the comment/response, I do realize that my solution approach seems to heading in the right direction.

Edit 31/01/2021: After a discussion with a Professor who teaches System Design at a Software related master program, I reliased that the optimal solution to this problem wouldn't "convert" arbitrarily the ArrayUtilities from a class to an interface. As a result, I changed the relationship between the ArrayBasedList and the ArrayUtilities as shown above.

Was it helpful?

Solution

The principle being violated here is often phrased as favor composition over inheritance. You seem to have correctly identified the principle, but not how to follow it.

ArrayBasedList should neither inherit from nor implement ArrayUtilities. Instead, it should encapsulate an implementation of ArrayUtilities. In other words, a has-a relationship instead of an is-a relationship.

Licensed under: CC-BY-SA with attribution
scroll top