Refactoring Out Common Code
If you're paying attention, you've probably noticed a little repetition. In fact, some of those inner classes may be better served by separate classes that can populate your domain model. In this example, you'll refactor a little of that common code.
Your small objects may keep this book short, but they're not particularly realistic. Business objects typically have many more fields. If you try to do everything in line, you can accumulate a little too much replication. I like repetition about as much as I like paddling on flat water. In fact, my worst injuries have both come on easier rapids, or easy trails, because I wasn't not paying attention as closely as I should have been. You're likely to find the same phenomenon with tedious, repetitive code: the monotony can keep you from paying attention, and cause an uncomfortable number of minor injuries.
How do I do that?
You're simply going to break some of the code in those inner classes free, so they're easier to read and easier to reuse. You'll focus on the code that populates each object (Example 4-8).
Example 4-8. JDBCRentABike.java
public List getBikes( ) { final ArrayList results = new ArrayList( ); JdbcTemplate template = new JdbcTemplate( ); class BikesHandler implements RowCallbackHandler { public void processRow(ResultSet rs) throws SQLException { Bike bike = new Bike(rs.getString(MANUFACTURER), rs.getString(MODEL), rs.getInt(FRAME), rs.getString(SERIALNO), rs.getDouble(WEIGHT), rs.getString(STATUS)); ...