Java Reference
In-Depth Information
LISTING 12-6 (continued)
MovieDAO movieDAO;
public List<Movie> getAllMovies() {
return movieDAO.getAllMovies();
}
}
The preceding implementation of the DAO is simplistic and can be improved upon in several ways.
Type‐Safe DAO Implementation
One way to improve upon the DAO implementation is to make the DAO interface type‐safe. This
allows a type‐safe DAO that a subinterface can implement for each entity type you want to persist.
A base DAO might look like the code in Listing 12‐7.
LISTING 12‐7: Type‐safe base DAO
package com.devchronicles.dataaccessobject;
import java.util.List;
public interface BaseDAO<E, K> {
public void create(E entity);
public Movie retrieve(K id);
public void update(E entity);
public void delete(K id);
}
The i rst type parameter, E , is used to represent the entity, whereas the K type parameter is used
K
as the key. A subinterface that would dei ne methods specii c to that entity could then extend the
BaseDAO interface.
In Listing 12‐8, you create an interface that extends the BaseDAO and dei nes a method that returns
a list of all movies.
LISTING 12‐8: Specii c movie implementation of the base DAO
package com.devchronicles.dataaccessobject;
import java.util.List;
public interface MovieDAO extends BaseDAO<Movie, Integer>{
public List<Movie> findAllMovies();
}
A concrete class would implement this interface and provide code for each method.
 
Search WWH ::




Custom Search