2.2. Three-Tier Applications
Before we dive into writing our code, let's take a moment to talk about some architectural principles. This discussion will set the stage for this chapter and the rest of the book.
The parts of a J2EE application fall into three basic tiers:
In a well-engineered application, components in each tier are "highly cohesive" and "loosely coupled". By highly cohesive, we mean that each component should do only one thing. By loosely coupled, we mean that components across the tiers shouldn't depend on one another unnecessarily.
For example, a getCars( ) method call should return only data. The data shouldn't be preformatted into HTML. If it is, your data tier knows too much about the presentation tier. The two tiers are highly coupled.
An unfortunate side effect of this coupling is that deciding to add a new presentation technology later (like a Swing client) will force you to rewrite a portion of your persistence tier as well. If instead your persistence tier returns data and lets the presentation tier concern itself with the formatting, then you can reuse your persistence tier across multiple UIs. Loosely coupled tiers encourage reuse.
You also could argue that the persistence tier fails the highly cohesive test. It is concerned with too many thingsdata and presentation. A clean separation of concerns is what we are hoping to accomplish.
This notion of tiers guided both the JAW Motors application architecture and the layout of this book. The book starts with the presentation tier in the first few chapters, moves through the persistence tier in the middle chapters and tackles business tier issues in the latter chapters.