An Aside on Design Patterns

There's an area of increasing importance in OOP technology, known as design patterns. A design pattern is a set of steps for doing something, like a recipe is a set of steps for cooking something.

There is a key book on the topic called Design Patterns—Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (Addison Wesley, 1994: ISBN 0-201-63361-2). The book would be a lot more valuable if it were easier to read and gave examples that ordinary programmers could relate to. In spite of its dry, excessively academic style, it's an important text.

As the authors explain, design patterns describe simple, repeatable solutions to specific problems in object-oriented software design. They capture solutions that have been refined over time; hence, they aren't typically the first code that comes to mind unless you know about them. They are code idioms writ large. They are not unusual or amazing, or tied to any one language. Giving names to the common idioms and describing them, helps reuse. Some common idioms/design patterns are shown in Table 14-1.

Table 14-1. Design patterns overview

Design pattern

Purpose and use

Abstract Factory

Supplies an interface to create any of several related objects without specifying their concrete classes. The Factory figures out the precise class that is needed and constructs one of those for you.

Adapter

Converts the interface of a class into another interface that the client can use directly. Adapter lets classes work together that couldn't otherwise. Think “hose to sprinkler interface adapter.”

Observer

Defines a many-to-one dependency between objects, so that when the observed object changes state, all the Observers are notified and can act accordingly. Think “monitoring the progress of something coming in over the network.”

Singleton

Ensure that only one object is instantiated from some class, and provide a global point of access to it.

Command

Encapsulate a request as an object, rather than a method call. That lets you parameterize servers to handle different requests, and support requests that can be undone.

The recommended book describes a couple of dozen design patterns, and repays further study.

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

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