Working With Design Patterns: Abstract Factory
Each of these ReportComponent implementations is also trivial to test! Further, they provide ultimate flexibility as the kinds of reports grow. The HtmlReportSummary implementation, for example, could be used for any sort of HTML report.
Is this an appropriate use of the abstract factory pattern, and a better choice than builder? That's for you to decide. In this case, perhaps. It fulfills the interest of hiding the type information from a client. The client still must pass in an indicator that specifies whether or not the report is a platform or web report. That information could have as easily been buried in a properties file read by the abstract factory itself. The builder, on the other hand, is designed to highlight step-by-step construction, but that's not really necessary for this use.
As with all design patterns, abstract factory has some deep similarities to another pattern. The challenge is applying the pattern that best fits your needs. In many cases, you'll find yourself building a hybrid implementation.
Figure 1: The abstract factory pattern.
About the Author
Jeff Langr is a veteran software developer with over a quarter century of professional software development experience. He's written two books, including Agile Java: Crafting Code With Test-Driven Development (Prentice Hall) in 2005. Jeff contributed to Uncle Bob Martin's new book, Clean Code (Prentice Hall, August 2008). Jeff has written over 80 articles on software development, with over thirty-five appearing at Developer.com. You can find out more about Jeff at his site, http://langrsoft.com, or you can contact him via email at jeff at langrsoft dot com.
Page 4 of 4