Design Patterns

“Ah… yes, I heard about them… once. It’s only for seniors developers, right?”
Don’t be that guy.

Introduction

This is going to be a series of articles that describe the famous and yet incredibly hard to remember, at least for me anyway, Java Design Patterns. Drums in the background…

I’ve been asked about them during interviews, by only serious companies (duh!?), and actually used some during my work, but unfortunately I still don’t think that I “know” them all. And it’s disturbing for me, because patterns are fundamental, popular, and will make your life, and the life of team-mates, a lot easier.

Of course this means that you’ll have to spend more time on reading and even more time on applying patterns, but I think that it’s worth it. There is no shortcut in becoming the best at what you do. There’s only effort, pain and the following success!

Fight the laziness

Since we are all derived from class HumanBeaing, we all inherit a specific behavior called laziness. In my case, isLazy() {return true}; all the time!

Now there’s a part of me, and you probably, that is saying: “So what? We heard about them, but never actually used them AND if we haven’t so far then why start now? The past 4 years were great without them, right? I’m working at this BIG software company and didn’t need them so far. Go write about something new and exciting, you douche!”

Well… as simpler as it will be for me to write about something new, the design patterns themselves can be quite exciting and you are still very WRONG! If you haven’t had, or you passed, a chance to learn or apply design patterns then you better re-think you priorities as a developer. Let me tell you why:

First:
the company is not supposed to take special care about your own personal career development, even if they say so. And neither am I, it just so happens that I’m simply generous. It is your, not their responsibility to become better professional by acquiring new knowledge in your field. And believe me when I say there is a lot to be acquired. Having company training is one thing, but overall the main initiative towards knowledge must be your own.

Second:
The more patterns you master, the better and faster you’ll do your work making it easy for those poor souls that will be assigned to your project once you get promoted to hot-shot team lead. That’s where my real incentive comes in – I want to be surrounded by well written, understandable code.

OOP specialists are usually pattern masters as well and hopefully this article will be a step in the right direction for all of us. And with any luck you won’t look bored next time someone bring up Adapter pattern at the water dispenser. What? Is that only me? Darn…

To the point

What are design pattern? To put it simply they are ready solutions to well-known developer issues. That’s right! No need to re-invent the wheel! If you ever find you self in a coding situation dealing with issue that seem very familiar, chances are that, first – you already dealt with this issue before, and second – this issue may be resolved and documented in the form of pattern.

Originally assembled and published by infamous “Gang of Four” (GoF) in their book “Design Patterns: Elements of Reusable Object-Oriented Software” back at 1994, the design patterns became source of much passionate debate and even let to the birth of their counter parts know as anti-patterns. I won’t cover them here though. The regular patterns are enough to wrap my little head around. What followed was even more books that aimed to explain patterns even simpler. The good ones are “Head First Design Patterns” and “Design Patterns For Dummies”.

So next time you’r in a design bind because you have to manually correct hard-coded behavior across generations of inherited classes you will remember the Strategy Pattern, or when you cannot figure out how to consume this new interface that is incompatible with the old code you will remember Adapter Pattern, and so on.

Actual examples in JAVA API

Whether you realized it or not you are already using a very large portion of those patterns. No, that doesn’t mean you know them already. For example:

  • Builder pattern – java.lang.StringBuilder#append().
  • Singleton pattern – java.lang.Runtime#getRuntime().
  • Factory – java.util.Calendar#getInstance()
  • Adapter – java.io.OutputStreamWriter(OutputStream) (returns a Writer)
  • Flyweight – java.lang.Integer#valueOf(int)
  • Iterator – java.util.Iterator

and many more. For more examples go to StackOverflow.

Classification

Patterns can be divided into 3 groups: Creational, Structural, and Behavioral patterns.

Creational is about instantiation, Structural is about the blueprint, and Behavioral is about the relationship between objects.

Want to see how used the patterns actually are? Take the bellow vote and see for yourself.

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s