Assignment 2: Design Principles
Working on the Assignment
OLID
60 points (15 points per principle)
In lecture we covered the S from SOLID. In your own words, explain the other four principles, OLID. Include an example in the explanation of each principle. Prefer examples extracted from TODOOSE, either because TODOOSE follows the principle, or because TODOOSE violates it. Examples may be class diagrams, code, or both. Discuss under which conditions you’d follow the principle, and under which conditions you wouldn’t.
Technology
⚠️ In this part of the assignment you’re going to work on the TODOOSE code base. Don’t Fork that repository on GitHub! Otherwise there’s a high probability that you’ll end up pushing your answers for the whole world to see, which violates the Academic Integrity policy. Work on the version of the code base you cloned by following the steps from Assignment 0.
Let’s explore some design principles in practice.
Add an Abstraction
20 points
The JSON response produced by the getAll
action in the ItemsController
relies on the internal representation of the Item
model: the model includes the attributes identifier
and description
, so the JSON response looks like { "identifier": ___, "description": ___ }
. The translation from model fields to JSON comes for free with the JSON mapper we’re using—we didn’t have to write any code for it.
This is good for a number of reasons:
- For TODOOSE as it is, this is good enough (YAGNI).
- It keeps things simple (KISS), and we have to write less code.
- It helps us not repeat ourselves (DRY), having to say that the
identifier
attribute in the model maps to the"identifier"
field in the JSON response, and so forth. - It follows the conventions of the JSON mapper (Convention Over Configuration).
- And so forth.
But it can also be bad:
- If we add new attributes to
Item
that aren’t supposed to be part of the JSON response (for example,Item
s may now be password protected and there’s a newpassword
attribute), then we must remember to change the controller to prevent that field from appearing on the JSON response (encapsulate what varies was violated). - There’s a tight coupling between the model and the JSON response (loose coupling was violated).
- And so forth.
For this exercise you must modify the TODOOSE code base to introduce an explicit View
abstraction:
- Create a
com.jhuoose.todoose.views
package. - Create a
ItemView
class in that package. This class looks pretty much like theItem
model, including the same attributes and getters and setters. It must also include a constructor that receives anItem
and initializes the attributes accordingly. - Modify the
getAll()
action in theItemsController
so that it passes toctx.json()
a list ofItemView
s instead of a list ofItem
s.
In your submission, show what changed in the code base. Then answer the following questions:
- Why does this change solve the issue of adding a
password
attribute to the model and not having it accidentally appear in the JSON response? - Go over the list of principles on the lecture notes (including all of SOLID, not just the S that we covered in lecture). What principles were previously followed and are now violated?
- Go over the list of principles on the lecture notes (including all of SOLID, not just the S that we covered in lecture). What principles were violated and are now followed?
- If you had to choose, on which version of the code base would you prefer to work?
Remove an Abstraction
20 points
The ItemsController
is very slim (few lines of code), and you may argue that it confuses you when trying to make sense of TODOOSE. Since for now the application is so small, you may be right. Change the code base to inline the actions directly into the Server
class. This is similar to how the action is written in the first example of the Javalin webpage.
In your submission, show what changed in the code base. Then answer the following questions:
- Go over the list of principles on the lecture notes (including all of SOLID, not just the S that we covered in lecture). What principles were previously followed and are now violated?
- Go over the list of principles on the lecture notes (including all of SOLID, not just the S that we covered in lecture). What principles were violated and are now followed?
- If you had to choose, on which version of the code base would you prefer to work?
Submission
⚠️ Your assignment is submitted only when you submit the form below.
If you run into problems, send an email to assignment-submission@jhu-oose.com. Include the information about the submission: your GitHub Identifier and the Commit Identifier. Don’t include any information about the feedback—it’s anonymous.