Design Patterns

Design Patterns

Design patterns were derived from ideas put forward by Christopher Alexander, who suggested that certain common patterns of building design were inherently pleasing and effective.

The pattern is a description of the problem and the essence of its solution, so that the solution may be reused in different settings. The pattern is not a detailed specification. Rather, you can think of it as a description of accumulated wisdom and experience, a well-tried solution to a common problem.

Gamma and his co-authors break down the problem description into motivation and applicability.

The four essential elements of design patterns were defined by the ‘Gang of Four’ in their patterns book:

  1. A name that is a meaningful reference to the pattern.

  2. A description of the problem area that explains when the pattern may be applied.

  3. A solution description of the parts of the design solution, their relationships, and their responsibilities. This is not a concrete design description. It is a template for a design solution that can be instantiated in different ways. This is often expressed graphically and shows the relationships between the objects and object classes in the solution.

  4. A statement of the consequences—the results and trade-offs—of applying the pattern. This can help designers understand whether or not a pattern can be used in a particular situation.

To use patterns in your design, you need to recognize that any design problem you are facing may have an associated pattern that can be applied. Examples of such problems, documented in the ‘Gang of Four’s original patterns book, include:

  1. Tell several objects that the state of some other object has changed.

  2. Tidy up the interfaces to several related objects that have often been developed incrementally.

  3. Provide a standard way of accessing the elements in a collection, irrespective of how that collection is implemented.

  4. Allow for the possibility of extending the functionality of an existing class at run-time.

Patterns are a great idea but you need experience in software design to use them effectively. You have to recognize situations where a pattern can be applied. Inexperienced programmers, even if they have read the pattern books, will always find it hard to decide whether they can reuse a pattern or need to develop a special-purpose solution.