Why Study Design Patterns?

Now that you have an idea about what design patterns are, you may still be wondering, “Why study them?” There are several reasons that are obvious and some that are not so obvious.

The most commonly stated reasons for studying patterns are because patterns allow us to:

  • Reuse solutions— By reusing already established designs, I get a head start on my problems and avoid gotchas. I get the benefit of learning from the experience of others. I do not have to reinvent solutions for commonly recurring problems.

  • Establish common terminology— Communication and teamwork require a common base of vocabulary and a common viewpoint of the problem. Design patterns provide a common point of reference during the analysis and design phase of a project.

However, there is a third reason to study design patterns:

Patterns give you a higher-level perspective on the problem and on the process of design and object orientation. This frees you from the tyranny of dealing with the details too early.

By the end of this book, I hope you will agree that this is one of the greatest reasons to study design patterns. It will shift your mindset and make you a more powerful analyst.

To illustrate this advantage, I want to relate a conversation between two carpenters about how to build the drawers for some cabinets.[7]

[7] This section is inspired by a talk given by Ralph Johnson and is adapted by the authors.

Consider two carpenters discussing how to build the drawers for some cabinets.

Carpenter 1: How do you think we should build these drawers?

Carpenter 2: Well, I think we should make the joint by cutting straight down into the wood, and then cut back up 45 degrees, and then going straight back down, and then back up the other way 45 degrees, and then going straight back down, and then …

Now, your job is to figure out what they are talking about!

Isn't that a confusing description? What is Carpenter 2 prescribing? The details certainly get in the way! Let's try to draw out his description.

Carpenter 2 Says … Which Looks Like …
“Well, I think we should make the joint by cutting straight down into the wood, and then cut back up 45 degrees …”

“… then going straight back down, and then back up the other way 45 degrees, and then going straight back down, and then …”

“… until you end up with a dovetail joint. That is what I was describing!”


Doesn't this sound like code reviews you have heard? The one where the programmer describes the code in terms such as,

And then, I use a WHILE LOOP here to do … followed by a series of IF statements to do … and here I use a SWITCH to handle …

You get a description of the details of the code, but you have no idea what the program is doing and why it is doing it!

Of course, no self-respecting carpenter would talk like this. What would really happen is something like:

Carpenter 1: Should we use a dovetail joint or a miter joint?

Already we see a qualitative difference. The carpenters are discussing differences in the quality of solutions to a problem; their discussion is at a higher level, a more abstract level. They avoid getting bogged down in the details of a particular solution.

When the carpenter speaks of a miter joint, he or she has the following characteristics of the solution in mind:

  • It is a simpler solution— A miter joint is a simple joint to make. You cut the edges of the joining pieces at 45 degrees, abut them, and then nail or glue them together (see Figure 5-2).

    Figure 5-2. A miter joint

  • It is lightweight— A miter joint is weaker than a dovetail. It cannot hold together under great stress.

  • It is inconspicuous— The miter joint's single cut is much less noticeable than the dovetail joint's multiple cuts.

When the carpenter speaks of a dovetail joint (which we described how to make on page 81), he or she has other characteristics of the solution in mind. These characteristics may not be obvious to a layman, but would be clearly understood by any carpenter.

  • It is a more complex solution— It is more involved to make a dovetail joint. Thus, it is more expensive.

  • It is impervious to temperature and humidity— As these change, the wood expands or contracts. However, the dovetail joint will remain solid.

  • It is independent of the fastening system— In fact, dovetail joints do not even depend upon glue to work.

  • It is a more aesthetically pleasing joint— It is beautiful to look at when made well.

In other words, the dovetail joint is a strong, dependable, beautiful joint that is complex (and therefore expensive) to make.

So, when Carpenter 1 asked,

Should we use a dovetail joint or a miter joint?

the real question that was being asked was,

Should we use a joint that is expensive to make but is both beautiful and durable, or should we just make a quick and dirty joint that will last at least as long until the check clears?

We might say the carpenters' discussion really occurs at two levels: the surface level of their words, and the real conversation, which occurs at a higher level (a meta-level) that is hidden from the layman and which is much richer in meaning. This higher level is the level of “carpenter patterns” and reflects the real design issues for the carpenters.

In the first case, Carpenter 2 obscures the real issues by discussing the details of the implementations of the joints. In the second case, Carpenter 1 wants to decide which joint to use based on costs and quality of the joint.

Who is more efficient? Who would you rather work with?

This is one thing I mean when I say that patterns can help raise the level of your thinking. You will learn later in the book that when you raise your level of thinking like this, new design methods become available. This is where the real power of patterns lies.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.155.130