Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Amaze your developer friends with design patterns

<strong>Java Design Patterns</strong> column kicks off with a look at three important design patterns

  • Print
  • Feedback

Page 2 of 6

What are design patterns?

In any field of endeavor, experience is always the best teacher. For example, if you're learning a foreign language, it's best to learn the way you learned your native language as a child -- by immersion -- which gives you the daily opportunity to listen to, and engage in, conversation with native speakers. That experience always proves more valuable than anything learned from a book.

Design patterns, simply put, capture the best practices of experienced object-oriented software developers. Design patterns are solutions to general software development problems. Those solutions were obtained by trial and error by numerous software developers over a substantial period of time. In the GOF book, those best practices are catalogued with 23 design patterns. The authors describe each design pattern with a name and an intent, which describes what the pattern does and what design issues or problems it addresses. Design pattern descriptions also include the applicability of a pattern, its structure, its participants (meaning classes, interfaces, objects, and so on), and the cooperation between those participants. Finally, each design pattern includes a sample implementation, known uses, and a discussion of related patterns.

Developers find design patterns important for a number of reasons. First, they give novice developers access to the best practices of more experienced developers. Second, they allow developers to think of their designs at higher levels of abstraction; for example, instead of focusing on low-level details, such as how to use inheritance, you can approach complex systems as a collection of design patterns that already make the best use of inheritance. That shift of focus to a higher level of abstraction also provides a common vocabulary when developers discuss design. Nowadays, for example, it's quite common to hear developers discuss the pros and cons of a particular design pattern in a specific context. Finally, object-oriented software is designed and implemented in an iterative fashion, where among other things, code specific to a particular problem domain is refactored into more general-use code. Thus, refactoring results in software that is more resilient to change and therefore more reusable. Because design patterns evolved from numerous refactorings, you don't have to refactor them yourself. And if you weren't aware of design patterns when you initially implemented your software, you can use those patterns as targets for your refactoring.

A few examples of design patterns in the Java SDK

The Java 2 SDK contains many design pattern implementations. Some correspond directly to those discussed in the GOF book, others vary slightly from those patterns, and some have not been formally documented anywhere. Throughout this column I will limit our discussions of Java 2 design pattern implementations to those that closely approximate the patterns discussed in the GOF book.

In this section I briefly discuss three of the simplest and most widely used design patterns implemented in the Java 2 SDK: Strategy, Composite, and Decorator. For now, to give you a feel for design patterns, how they are used, and the benefits they provide, I briefly introduce those patterns and show how you can use them. In subsequent installments of this column, I will present more in-depth discussions of those patterns, including implementation details.

  • Print
  • Feedback

Resources