25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8 ■ Give Advice to Your <strong>Spring</strong> Application<br />

public class SearchEngineService implements SearchEngine {<br />

private static final Logger log = LoggerFactory.getLogger(SearchEngineService.class);<br />

private DocumentDAO documentDAO;<br />

public SearchEngineService(){<br />

if(log.isDebugEnabled())<br />

log.debug("SearchEngineService created: " + this);<br />

}<br />

public DocumentDAO getDocumentDAO() {<br />

return documentDAO;<br />

}<br />

public void setDocumentDAO(DocumentDAO documentDAO) {<br />

if(log.isDebugEnabled())<br />

log.debug("Document DAO set: " + documentDAO);<br />

}<br />

this.documentDAO = documentDAO;<br />

public List findByType(Type documentType) {<br />

if(log.isDebugEnabled())<br />

log.debug("Start Params: " + documentType);<br />

List result = new ArrayList();<br />

for(Document doc : listAll()){<br />

if(doc.getType().getName().equals(documentType.getName()))<br />

result.add(doc);<br />

}<br />

}<br />

if(log.isDebugEnabled())<br />

log.debug("End Result: " + result);<br />

return result;<br />

public List listAll() {<br />

if(log.isDebugEnabled())<br />

log.debug("Start Params: ");<br />

List result = Arrays.asList(documentDAO.getAll());<br />

if(log.isDebugEnabled())<br />

log.debug("End Result: " + result);<br />

return result;<br />

}<br />

}<br />

In Listing 8-1 you are using the “if” statement because debugging is enabled and it is calling the log.debug<br />

method. But take a closer look; those “if” statements are in every method: in the constructor, in the findByType,<br />

listAll, and in the setter of the DocumentDAO. Also these statements are in different classes as well. This is called<br />

tangling and scattering code, as shown in Figure 8-1.<br />

90

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!