Nov. 30, 2006, 10:56 a.m.

Sometimes Things Don't Work for You

After a conversation with a co-worker yesterday about some code that I'd written, I have figured out a need for some new design patterns.

You will occasionally find a piece of code that looks something like what you want, but it doesn't fit your needs exactly. This is a quick guide to help you when you run into such a scenario.

The good news is that I have determined not just one, but two design patterns to ensure that you no longer need to use things that don't solve your problems. Which pattern you use depends on a number of factors. Lucky for you, that number is one: access to modify the original code.

Change It

Change It I call the first pattern the Change It design pattern. For this pattern, you need to have access to edit the original code.

Here's how this pattern works: Edit the code to solve your problem.

Ideally, the unit tests on the code will also help you make sure you haven't broken the existing functionality when you modified it to meet your requirements.

Use Something Else

Use Something Else The alternative is what I call the Use Something Else design pattern.

To determine whether you need to use this design pattern, you basically assess the API for its applicability to your problem, and if it fails to do what you want, and you can't apply the Change It pattern, then you will need to Use Something Else.

The diagram describing how this pattern works can be found to the right, but the basic concept is to write something that meets your requirements when you can't find anything that does.

I offer these design patterns to the world for free in hopes that they'll save someone from spending a half an hour trying to explain that an API that was created before new requirements came along that don't fit into it can be modified to meet the new requirements. Remember to use them wisely.

Like all design patterns, they may not fit every problem, so don't try to use them where they don't fit just because they're new and exciting.

blog comments powered by Disqus