What is the God Object antipattern, and how would you break up a class that has become one?
A God Object is a class that knows too much and does too much. It ends up holding every piece of data in the system and every method that acts on that data, so that everything goes through it.
public class Main {
public static void main(String[] args) {
App app = new App(new Logger(), new Store());
app.run();
}
}
And here is what a God Object looks like.
public class App {
private final java.util.List<User> users = new java.util.ArrayList<>();
private final java.util.List<Order> orders = new java.util.ArrayList<>();
private final Logger logger;
private final Store store;
public App(Logger logger, Store store) {
this.logger = logger;
this.store = store;
}
// It has a method for everything.
public void registerUser(String email) { }
public User findUser(String email) { return null; }
public void placeOrder(String userId, String item) { }
public void cancelOrder(String orderId) { }
public void printReport() { }
public void sendEmails() { }
public void backup() { }
public void log(String message) { }
}
And the four problems it causes.
the problem what goes wrong
----------------------------------- --------------------------------------------
everything depends on it one change breaks everything
it has no single job you cannot name what it does
it has all the data everybody shares one mutable bag
it is impossible to test you have to build the whole thing
And how do you break it up? You look at the methods and you group them by the data they act on. That gives you the classes that were hiding inside it.
// The methods about users become one class.
public class UserService {
private final java.util.List<User> users = new java.util.ArrayList<>();
public void register(String email) { }
public User find(String email) { return null; }
}
// The methods about orders become another.
public class OrderService {
private final java.util.List<Order> orders = new java.util.ArrayList<>();
public void place(String userId, String item) { }
public void cancel(String orderId) { }
}
// The reporting methods become a third.
public class ReportService {
public void printReport() { }
public void sendEmails() { }
}
And the god object becomes a coordinator that holds the smaller ones and passes the work on.
public class App {
private final UserService users;
private final OrderService orders;
private final ReportService reports;
public App(UserService users, OrderService orders, ReportService reports) {
this.users = users;
this.orders = orders;
this.reports = reports;
}
public void run() {
users.register("[email protected]");
orders.place("u1", "book");
reports.printReport();
}
}
And there are two things worth knowing about how to do this without breaking everything. The first one is that you do it one group at a time, and you keep the god object's old methods working until the callers have moved over.
the step what you do
-------------------------------- --------------------------------------------
1. move one group of methods into a new class
2. leave the old methods in they call the new class
3. change the callers one at a time
4. delete the old methods once nothing calls them
And the second one is that the data moves too. If the god object held the list of users then the new user service holds it, and the god object does not hold it as well. If they both hold it then you have two copies and the bug is worse than before.
So the rule to remember is this. Group the methods by the data they use, and move the data with them.
The last question in this topic is about the symptoms.