Where do you find abstraction in a real system, and how do you introduce it without rewriting everything at once?
You find the places where the code has to change, and you put the abstraction exactly there. And you introduce it one seam at a time, without rewriting everything.
public class Main {
public static void main(String[] args) {
Notifier notifier = new ConsoleNotifier();
notifier.notifyUser("build finished");
}
}
And here is the interface and one implementation.
public interface Notifier {
void notifyUser(String message);
}
public class ConsoleNotifier implements Notifier {
@Override
public void notifyUser(String message) {
System.out.println("[notice] " + message);
}
}
So where do you find abstractions in a real system? Here are the places that come up again and again.
the place the abstraction
------------------------------ --------------------------------------------
talking to a database a repository interface
sending a message a notifier interface
reading or writing a file a storage interface
calling another service a client interface
working out the time or money a value object, not a service
And how do you introduce one without rewriting everything? With four small steps.
the step what you do
-------------------------------- --------------------------------------------
1. find the seam the place where the code changes
2. write the interface the smallest set of methods that the
callers actually use
3. make the old class implement one line of change in the old class
it
4. change the caller to hold one line of change in each caller
the interface
And the key is step two. The interface must be exactly the set of methods that the callers use today, and not more. If you add a method that nobody calls then you have made the change harder for the next implementation.
And there is a detail worth knowing about, and it is about the order of the steps. You write the interface first and make the existing class implement it, so that nothing is broken at any point. Then you change the callers. If you changed the callers first then the code would not compile until the interface existed, and in a big code base that means a lot of broken work in between.
And there is one more thing that is worth knowing about. Sometimes the first implementation is not worth writing as a separate class at all. If the old code was doing the work inline, you can make the interface and have the old class implement it, and you have not changed any behaviour at all. The value of the abstraction is that the next implementation can now be different.
// Before: the service built the notifier itself.
public class BuildService {
private final ConsoleNotifier notifier = new ConsoleNotifier();
public void run() {
notifier.notifyUser("build finished");
}
}
// After: the notifier comes from outside.
public class BuildService {
private final Notifier notifier;
public BuildService(Notifier notifier) {
this.notifier = notifier;
}
public void run() {
notifier.notifyUser("build finished");
}
}
And there is a rule about when not to add an abstraction. If there is only ever going to be one implementation and there is no test that needs a fake, then the interface is just an extra file. So add it when the code changes, when it is tested, or when you can name the second implementation.
So the rule to remember is this. Abstract at the place where the code changes, and add only the methods the callers use.
The last question in this topic is about a real example.