Working With Design Patterns: Factory Method
Note: If you use the abstract test case pattern, you'll need to alter your Ant build scripts to ignore the abstract test classes. Otherwise, Ant will attempt to instantiate and execute the tests in the abstract test class. Typical shops follow a consistent naming convention, which makes for a simple Ant solution:<exclude name="**/Abstract*Test.java" />
Eclipse and other IDEs are smart enough to ignore these classes by default.
The factory method is a simple inheritance-based design pattern. Most programmers would implement the pattern sooner or later, even if they didn't know what a factory method was. The value of the pattern in this case is that the construct now has a standard name, and that the pattern recommends a preferred solution instead of at least a couple other variant implementations. This implementation of the factory method pattern—in the context of creating a hierarchy of unit tests—is relevant enough to a fairly common test problem that it has its own pattern name: abstract test case.
Click here for a larger image.
Figure 1: The factory method 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 3 of 3