Java, Programming

Factory of Factories

I am convinced that Patterns are the new Anti-Patterns. As the joke runs, the GoF patterns show you how to write better C++.

One Pattern/Anti-Pattern that drives me up the wall are Factories. Or as the might often be better known: Constructors. After all if a Factory only constructs one type of thing it is no better abstraction that just putting the construction logic in the damn constructor.

Effective Java makes a better suggestion for Java coding that constructors like inheritance should be used sparingly. Instead of creating a whole new class for constructing instances just create static factory methods inside the class with appropriate names for the kind of construction being done.

This is neater, keeps related code closer and applies in a lot more situations. My rule when evaluating code is that an implementation of Factory actually has to create more than one type of object in a way that cannot be replicated by Dependency Injection to have value.

Not much code passes that test, not even my previous attempts to please the structural aesthetic of enterprise architects. In the past I have been as guilty of pattern abuse as anyone.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s