Design Patterns: The Factory Method Pattern
The factory method pattern is a creational design pattern which has been incredibly popular practically from the start. Among Java developers it's use is nearly ubiqitous, being found in abundance in todays frameworks and libraries. It is intended for creating subclasses of a type based on the "pieces" which get sent to the factory. The objects construction is then delegated to a special static method instead of using the normal route of invoking its constructor with the 'new' keyword.
Once invoked the factory looks at the parts it has been supplied with and determines which object to construct and how to construct it, returning the object as a result. This pattern is also interesting because despite being a creational pattern it straddles the line between being a behavioral pattern as well.
Which Type Should I Use?
Lets say you are the designer of a pattern matching library which offers a multitude of different algorithms for different types of pattern matching. You might have something like a base PatternMatcher class from which you derive a BoyerMooreExactMatch class, along with a RegularExpressionMatch and EditDistanceFuzzyMatch classes.
public abstract class PatternMatcher {
public abstract Boolean match(String text);
}
public class BoyerMooreExactMatch extends PatternMatcher {
public BoyerMooreExactMatch(String pattern);
@Override
public Boolean match(String str);
}
public class RegularExpressionMatch extends PatternMatcher {
public RegularExpressionMatch(String pattern);
@Override
public Boolean match(String str);
}
public class EditDistanceFuzzyMatch extends PatternMatcher {
public EditDistanceFuzzyMatch(String pattern);
@Override
public Boolean match(String str);
}
You could leave it up to the user to know how to properly construct the needed class and any of its dependents, leaving the whole process open to errors, etc. Or, you could delegate all of that work to a factor method pattern. You might be saying "But hey, isnt that what the Facade pattern is for?" and the answer is yes, but also no. Lets take a look at the similarities and differences.
The factory pattern is actually something of an extension to the facade pattern, in that it makes use of the facade pattern to do what it does: offers a standardized, simple interface to number of different objects.
public class PatternMatcherFactory {
public static PatternMatcher create(MatchType type, String pattern) {
switch (type) {
case BOYER_MOORE:
return new BoyerMooreExactMatch(pattern);
case REGULAR_EXPRESSION:
return new RegularExpressionMatch(RECompiler.compile(new Parser().parse(pattern)));
case EDIT_DISTANCE:
return new EditDistanceFuzzyMatch(pattern);
default:
throw new IllegalArgumentException("Unknown match type");
}
}
}
-
Compiling Regular Expressions for "The VM Approach"
-
Composable Linked Digraphs: An efficient NFA Data Structure for Thompsons Construction
-
Improving the Space Efficiency of Suffix Arrays
-
Augmenting B+ Trees For Order Statistics
-
Top-Down AST Construction of Regular Expressions with Recursive Descent
-
Balanced Deletion for in-memory B+ Trees
-
Building an AST from a Regular Expression Bottom-up
-
The Aho, Sethi, Ullman Direct DFA Construction Part 2: Building the DFA from the Followpos Table
-
The Aho, Sethi, Ullman Direct DFA Construction, Part 1: Constructing the Followpos Table
-
Procedural Map Generation with Binary Space Partitioning
Leave A Comment