Critique Design Decisions
- Criticize design decisions for a simple type hierarchy in Java made by employing the inheritance mechanism.
Exercise critique the design decisions below.
-
MoocRosterextendsJhuRoster(or the other way around)Solution
-
The advantage of having one type of roster extend the other one lies in type substitution benefits. For example, suppose
MoocRosterextendsJhuRoster. In that case, we can have an array of typeJhuRosterand store rosters ofMoocRosterthere. -
The main issue with this design is a minimal incentive for an inheritance; if
MoocRosterextendsJhuRoster(or the other way around), it must override all of its operations. There goes the incentive on code reuse. Besides, the semantic meaning of inheritance is not firmly met here either. It is not clear thatMoocRosteris aJhuRoster(or the other way around).
-
-
JhuRosterandMoocRosterare entirely different data types (or "data structures" if you like).Solution
-
The advantage of having two independent types,
JhuRosterandMoocRoster, is that we avoid the need to find a semantic "is-a" relationship. (It is not clear thatMoocRosteris aJhuRosteror the other way around.) -
The disadvantages, on the other hand, are twofold:
-
It will add complexity (and possibly duplicate code) in clients of these types (i.e., all classes that need to maintain a roster of students).
-
The two types of rosters may evolve independently and diverge from having a unified interface. That means more work for clients of these rosters as they must learn to work with two potentially different interfaces.
-
-
-
There is a
Rosterclass where bothMoocRosterandJhuRosterextend it.Solution
The final design, like the first one, benefits from the potentials of type substitution. It further offers a more justified inheritance relationship; indeed,
JhuRosteris aRosterandMoocRosteris aRoster.The problem with this design occurs when implementing the base type
Roster. We have two choices:-
Implement
Rosteras if it was aJhuRosterorMoocRoster. In this case, one of theJhuRosterorMoocRosterwill be an alias for theRosterclass. The criticism applied to the first design will be applicable here. -
Don't implement
Roster! Instead, put stubs in place of its methods. The issue with this approach is that one can still instantiate theRostereven though its methods have no implementation!Student john = new Student("John Doe", "john@email.com"); Roster myRoster = new Roster(30); myRoster.add(john); // what will happen??!
-