[Image source: this article]
That is why we have structured these tutorials to get everyone to practice this skill so that your first few (not-so-good) diagram drawing experiences happen in the tutorial, not in an interview or during your internship.
Note the following:
Question adapted from past exam paper.
Draw a Class Diagram for the code (StockItem
, Inventory
, Review
, etc.)
Consider the code below:
public interface Billable {
void bill();
}
public abstract class Item
implements Billable {
public abstract void print();
}
public class StockItem extends Item {
private Review review;
private String name;
public StockItem(
String name, Rating rating) {
this.name = name;
this.review = new Review(rating);
}
@Override
public void print() {
//...
}
@Override
public void bill() {
//...
}
}
public enum Rating {
GOOD, OK, POOR
}
public class Review {
private final Rating rating;
public Review(Rating rating) {
this.rating = rating;
}
}
import java.util.List;
public class Inventory {
private List<Item> items;
public int getItemCount() {
return items.size();
}
public void generateBill(Billable b) {
// ...
}
public void add(Item s) {
items.add(s);
}
}
(a) Draw a class diagram to represent the code. Show all attributes, methods, associations, navigabilities, visibilities, known multiplicities, and association roles. Show associations as lines.
(b) Draw an object diagram to represent the situation where the inventory has one item named spanner
and a review of POOR
rating
i.e., new Inventory().add(new StockItem("spanner", new Review(Rating.POOR)))
.
(a) Do the following exercise similar to the previous one.
Draw a Sequence Diagram for the code (PersonList
, Person
, Tag
)
Consider the code below:
class Person {
Tag tag;
String name;
Person(String personName, String tagName) {
name = personName;
tag = new Tag(tagName);
}
}
class Tag {
Tag(String value) {
// ...
}
}
class PersonList {
void addPerson(Person p) {
// ...
}
}
Draw a sequence diagram to illustrate the object interactions that happen in the code snippet below:
PersonList personList = new PersonList();
while (hasRoom) {
Person p = new Person("Adam", "friend");
personList.addPerson(p);
}
(b) How would you update the diagram if the PersonList
class was updated as follows?
class PersonList{
void addPerson(Person p){
add(p);
}
void add(Person p){
//...
}
}