How do objects collaborate in a well-designed Java system, and how do you keep that collaboration from turning into a tangle?
Objects collaborate by sending messages to each other. Each object holds the ones it needs and it asks them to do their part, and none of them knows how the others work.
public class Main {
public static void main(String[] args) {
Cart cart = new Cart();
cart.add(new Book("Effective Java", 45.0));
System.out.println(cart.total());
}
}
And here is the cart and the two things it holds.
public class Cart {
private final java.util.List<Item> items = new java.util.ArrayList<>();
private final Pricing pricing;
public Cart(Pricing pricing) {
this.pricing = pricing;
}
public void add(Item item) {
items.add(item);
}
public double total() {
double sum = 0;
for (Item item : items) {
sum = sum + pricing.priceOf(item);
}
return sum;
}
}
And the pricing is an interface, so the cart does not know how the prices are worked out.
public interface Pricing {
double priceOf(Item item);
}
public class Item {
private final String title;
private final double listPrice;
public Item(String title, double listPrice) {
this.title = title;
this.listPrice = listPrice;
}
public String title() {
return title;
}
public double listPrice() {
return listPrice;
}
}
And how do you keep the collaboration from turning into a tangle? There are four rules, and they are all about who knows about whom.
the rule why it matters
------------------------------------ --------------------------------------------
each object holds one or two a class that holds ten collaborators
collaborators is doing ten jobs
the collaborators are found through so nothing is created inside and
the constructor the test can give a fake
the class depends on interfaces so the other side can change
the objects talk through small so the messages are easy to
methods follow
And the first rule is the one that catches the most. A class that holds a logger and a database and a mailer and a cache and a queue is not a class any more, it is a whole system in one file. And the fix is to give each of those jobs to its own object and to hold one of them.
And there is a second rule that is worth knowing about, and it is about the direction of the messages. A cart should ask the pricing what the price is. It should not ask the pricing for its fields and work it out itself, because then the cart knows how the pricing works.
the bad way the good way
------------------------------------ ------------------------------------
sum + pricing.discountFor(item) sum + pricing.priceOf(item)
and then adjust it here
And there is a third rule, and it is about who starts the conversation. A collection of objects that all call each other can end up with a loop, and then the order of the calls decides what happens. So the object that starts, the one that the others answer to, should be obvious in the code.
So the rule to remember is this. Each object holds one or two collaborators and asks them questions through small methods.
The last question in this topic is about what makes collaboration easy to follow.