Expert Spring MVC and Web Flow

(Dana P.) #1

Service Interface


As mentioned before, Spring MVC applications are typically layered with the service layer
encapsulating the actual use cases. Therefore, we start by creating the service layer interface,
helping us keep the business logic out of the web layer. Because the Spring Framework does
such a good job at managing plain old Java objects (POJOs) and beans, we’re not afraid of cre-
ating many well-defined and orthogonal Java objects that together form the entire system.
Creating the interface first also allows development work to begin on the web layer,
before the actual interface implementation is complete.

Use Case #1


The first use case doesn’t specify any type of uniqueness to the special deals. That is, every
user will see the same special deals when they view the home page. For now, the most simple
thing to do is create a getSpecialDeals()method on the service interface (Listing 4-6) return-
ing a list of SpecialDealobjects.
The SpecialDealclass (Listing 4-2) is defined by the use case to include three parameters:
the departure airport, the arrival airport, and the total cost. The airports will be instances of
the Airportclass (see Listing 4-1), so that we can encapsulate both the name and airport code.

Listing 4-1.Airport Class

public class Airport {

private String name;
private String airportCode;

public Airport(String name, String airportCode) {
this.name = name;
this.airportCode = airportCode;
}

public String getAirportCode() {
return airportCode;
}

public String getName() {
return name;
}

public String toString() {
return name + " (" + airportCode + ")";
}

}
Like the Airportclass, we have made the SpecialDealclass (Listing 4-2) immutable, to
make it easier and safer to use. As it stands now, the only use of this class is to return read-only
data, so in this case it is justified.

42 CHAPTER 4 ■JUMP INTO SPRING MVC

Free download pdf