Object-Oriented Design
Encapsulation, cohesion, and composition in the chatbot
11.1 Learning Objectives
By the end of this chapter, you should be able to:
- distribute responsibilities across cohesive classes;
- protect invariants with encapsulation;
- choose appropriate access modifiers;
- use composition to collaborate without inheritance;
- identify unnecessary coupling.
11.2 From a Large Class to Clear Responsibilities

Figure 11.1. Separated responsibilities collaborate through composition.
The previous version concentrates input, rules, and history in a single place. An initial decomposition can create:
History: records and queries interactions;CommandProcessor: chooses responses;ConsoleApplication: coordinates the terminal and shutdown.
A class is cohesive when its data and methods work toward the same goal. Coupling is the dependency between parts; the goal is not to eliminate it completely, but to depend on small, stable contracts.
11.3 Encapsulation and Access
| Modifier | Primary access |
|---|---|
private | only the class itself |
| no modifier (package-private) | classes in the same package |
protected | same package and subclasses |
public | any class with access to the type |
Start with the narrowest visibility needed. Making a field private is not enough: the class must also provide operations that preserve its valid state.
11.4 Composition
import java.util.ArrayList;
import java.util.List;
final class History {
private final List<String> lines = new ArrayList<>();
void recordInteraction(String input, String response) {
if (input == null || input.isBlank()) {
throw new IllegalArgumentException("Input is required.");
}
lines.add("USER: " + input.strip());
lines.add("BOT: " + response);
}
List<String> entries() {
return List.copyOf(lines);
}
int totalInteractions() {
return lines.size() / 2;
}
}
final class CommandProcessor {
String respond(String command) {
return switch (command) {
case "hi" -> "Hello!";
case "help" -> "Commands: hi, help, and tip.";
case "tip" -> "Prefer composition for has-a relationships.";
default -> "I didn't understand.";
};
}
}
public class Chapter11Design {
public static void main(String[] args) {
History history = new History();
CommandProcessor processor = new CommandProcessor();
String input = "tip";
String response = processor.respond(input);
history.recordInteraction(input, response);
System.out.println(response);
System.out.println(history.entries());
}
}
History does not decide responses. CommandProcessor does not write to the console or know the internal structure of the history. The application composes the objects.
11.5 static and final
Use static for behavior or state that belongs to the class rather than a specific instance. Avoid turning it into a shortcut for mutable global state.
Use final to communicate that:
- a reference will not be reassigned;
- a method cannot be overridden;
- a class was not designed for inheritance.
11.6 Design Review
Ask yourself:
- does each class change for one main reason?
- does each rule appear in only one place?
- are internal collections protected?
- could you swap out the console interface without rewriting the domain logic?
- do the names reflect the language of the problem?
The complete example is in examples/src/Chapter11Design.java.
11.7 Verifiable Practice
Extract a History class from version 10. The application must continue producing the same responses.
Test cases:
- an empty history returns an empty list;
- one interaction produces exactly two lines;
- modifying the returned list does not modify the internal state;
- empty input is rejected.
Modeling rule
Use composition for “has-a” relationships: the chatbot has a history. Reserve inheritance for genuine specialization relationships.