0

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?

In my opinion, the design principle that is hurt by the ArrayBasedList class is the "Use inheritance as a Specialization" because the ArrayBasedList is not (extends) only an Array (ArrayUtilites) but it has (implements) the methods from List and ArrayUtilites as well.

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 and the ArrayUtilites interfaces. [Link unrelated classes with has a capabilities.]

Package/Class Diagram of java.util.List 2

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.

  • 1
    "Which design principle is (most likely) hurt by the ArrayBasedList class?" - there's literally not enough information to provide a meaningful answer. The design principle is not hurt "because the ArrayBasedList is not only an Array but it has the methods from List and ArrayUtilites as well", this, in itself, doesn't necessarily matter. Using only interface inheritance ("implements") does potentially make the code more decoupled - if you've done it for a good reason. Whether or not this is a good thing depends on if what you gain by decoupling outweighs the costs that it brings. – Filip Milovanović Jan 12 at 18:11
  • 2
    In other words, it's not about diagrams and following abstract rules without having an idea of what the components depicted in the diagrams represent, and how they are supposed to work together. Design principles, even though general in formulation, need to be understood in the context of a problem (you can't make sense of them in terms of an abstract diagram that doesn't specify what the design-relevant features of the model are). And specific designs that can be deemed right (or wrong) are problem-specific. That's the whole point of design - you're using it to tackle a specific problem. – Filip Milovanović Jan 12 at 18:17
1

The principle being violated here is often phrased as favor encapsulation 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.

  • Thanks for your prompt and interesting response. What seems difficult for me to understand, is how can the encapsulation of an implementation of ArrayUtilities described in the UML diagram. – Konstantinos Loizas Jan 12 at 18:40
  • @KonstantinosLoizas It generally wouldn't be, because it's an implementation detail. – Kevin Krumwiede Jan 12 at 18:42
  • @KonstantinosLoizas I have no idea what professors think. I just know I've made a career out of cleaning up after people with degrees. – Kevin Krumwiede Jan 12 at 18:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.