|
|
|||||
Felipe Gaucho's BlogPatterns ArchivesSimple example of Dependency InjectionPosted by felipegaucho on November 06, 2007 at 10:41 AM | Permalink | Comments (1)Footprint Project is gaining momentum since its stable release, with a growing number of JUGs and other groups using the library to produce signed certificates for events and courses. Some of these early adopters are asking me how to modify the way Footprint publish the certificates, and also if there is a set of configuration they can use in order to avoid checkout the project from the SVN repository and do it programmatically. Well, you actually cannot escape from programming, but instead of digging details of the Footprint library, you can use Dependency Injection to minimize your effort. How Footprint publishes the signed PDF files?First of all, a brief explanation on how Footprint generates documents - shown in the diagram below:
Using Constructor Injection to modify the behavior of the ExporterThe core interface of Footprint is the Exporter, which describe the following set of methods:
Following the Dependency Injection pattern proposed by Martin Fowler, the Footprint uses Construction Injection to allow different implementations of the Exporter interface. In order to produce different documents - other than signed PDF files - or to change the way the documents are generated, you must do:
public class MyRtfExporter implements net.java.dev.footprint.exporter.Exporter
FootprintPublisher rtfPublisher = new JdbcPublisher(new MyRtfExporter(), config, logger);
That's it. Dependency Injection is an overhyped term used to promote the idea of an advanced usage of Design Patterns, but simply posting: dependency injection is a realization of an interface passed as parameter of a constructor or a method.. The so called inversion of control means you are creating a program based on abstract concepts - the interfaces - but the concrete way your program will produce its results will come from outside the code: it will be injected. Your code will not control the way the interfaces will be realized, the user of your code will do that ;) Final tips:
| |||||
|
|