Design Patterns – Can the Strategy Pattern Be Overused?

design-patternslanguage-agnosticobject-orientedstrategy

Example: A file, representing a serialized version of an object, needs to be read and deserialized. It would simple to implement this if we only cared about a single serialization format, but many such formats exist (JSON, YAML, etc.).

One way to handle this would be to create an abstract serializer type/interface with methods such as deserializeToArray, deserializeToObject, and so on, and pass this interface as a dependency in order to decouple any dependent classes from any concrete implementation.

This could be further generalized by having the serialization methods actually be implementations of another interface, which we'll call Transform. So here we're effectively using the Strategy pattern once again, this time encapsulating a family of algorithms for transforming one kind of data into another desired object or array format.

Is this perhaps an overuse of the Strategy pattern? Are there better ways to structure things here or patterns more fitting for this sort of problem? Are there disadvantages to using several nested Strategy patterns as in this example?

Best Answer

What you're talking about is programming to abstractions, which does not imply the Strategy pattern.

Programming to abstractions is always a good practice. There's virtually no additional effort involved in providing an interface or abstract base class for a concrete implementation, and many refactoring tools can now do this automatically for you.

How abstract to make it is a different question.

In your first example, you simply have an abstract type. You could program everything to use a generic Serializer or ISerializer type and then wire up a single default implementation in an IoC container like Castle or Spring. Simple. Done.

In the second instance, though, you're designing abstract functionality. You're trying to predict how this abstract type might be used in the future, and you are probably going to be wrong. The YAGNI principle applies here; unless you have some reason to believe that your application actually needs a fully-generic "transform" interface which can turn anything into anything else, and unless you've actually scoped out the requirements for it, then you're wasting your time planning for it. Maybe you should just use one of the many existing XML transformation languages if you need that kind of flexibility.

None of this really has anything to do with the strategy pattern. The strategy pattern means that not only are you coding against an abstract type, but that you are choosing which concrete type to use based on information only known at runtime. This is neither explicit nor implicit in your question.

If you only have one implementation, then implementing a strategy pattern is absolutely overkill, unless you know for sure that you will need to support additional strategies almost immediately afterward.

But that does not preclude you from using abstract types. This is generally how most OO applications are designed now, using dependency injection for code and an IoC container for configuration. I'd go so far as to say this is more productive than coding to concrete types, because (a) it's much easier to test and mock, and (b) quickly coding an interface allows you to continue your current task without much distraction, as opposed to going off and writing a whole new class because you can't continue without it.

So definitely make use of abstract types, but don't overthink things and spend a lot of time fussing about how to generalize them. It's easier to refactor later than it is to work with a bloated interface.

Related Topic