chapter 15 design p atterns lik e most complex structures
play

Chapter 15 Design P atterns Lik e most complex structures, - PDF document

Chapter 15 Design P atterns Lik e most complex structures, go o d computer programs are often formed b y imitating the structure of older, similar programs that ha v e already pro v en successful. The concept of


  1. Chapter 15 Design P atterns Lik e most complex structures, go o d computer programs are often formed b y imitating the structure of older, similar programs that ha v e already pro v en successful. The concept of a is an attempt to capture and formalize this pro cess of imitation. The basic design p attern idea is to c haracterize the features of a pro v en solution to a small problem, summarizing the essen tial elemen ts and omitting the unnecessary detail. A catalog of design patterns is a fascinating illustration of the m yriad w a ys that soft w are can b e structured so as to address di�eren t problems. Later, patterns can giv e insigh t in to ho w to approac h new problems that are similar to those situations describ ed b y the pattern. This c hapter will in tro duce the idea of design patterns b y describing sev eral that are found in the Ja v a library . The terminology used in describing the patterns is adapted from the b o ok Design Patterns: Elements of R eusable Obje ct-Oriente d Softwar e , b y Eric h Gamma, Ric hard Helm, Ralph Johnson and John Vlissides [Gamma 1995 ]. This w as one of the �rst b o oks to describ e the concept of design patterns and pro vide a systematic cataloging of patterns. Man y more patterns than are describ ed here can b e found in this b o ok, as w ell as in the recen t literature on design patterns. The format used in describing eac h pattern is to �rst c haracterize the problem the pattern is addressing. Then, the essen tial features of the solution are summarized. In some cases this is follo w ed b y a discussion that examines some of the con text for the problem, or con trasts alternativ e design p ossibilities. This is follo w ed b y a more detailed description of the pattern as it is manifest in the Ja v a Library . Finally , a sen tence or t w o summarizes the situations where the pattern is applicable. 15.1 Adapter Ho w do y ou use an ob ject that pro vides appropriate b eha vior but uses a di�eren t problem: in terface than is required in some situation? 259

  2. 260 CHAPTER 15. DESIGN P A TTERNS solution: De�ne an adapter class that acts as an in termediary . The adapter do es little w ork itself, but merely translates commands from one form in to the other. In ternational tra v elers frequen tly o v ercome the problem of di�ering electrical discussion: plug and v oltage standards b y using adapters for their appliances. These adapters allo w an electrical appliance that uses one t yp e of plug to b e mo di�ed so that it can b e used with a di�eren t t yp e of plug. The soft w are equiv alen t is similar. An adapter is concerned mostly with c hanges in the in terface to an ob ject, and less with the actual functionalit y b eing pro vided. Adapter Client W o rk er example: An example of adapters in the Ja v a library are the \wrapp er" classes, Bo olean , Integer , and so on. These adapt a primitiv e t yp e (suc h as b o olean or int ) so that they can b e used in situations where an Object is required. F or example, wrapp ers are necessary to store primitiv e v alues as elemen ts in a V ecto r . Another form of adapter is the class MouseAdapter used in the pin ball game describ ed in Chapter 7, as w ell as in the Solitare program presen ted in Chapter 9. Here the adapter reduces the in terface, b y implemen ting default b eha vior for metho ds that are unneeded in the curren t application. The clien t can therefore concen trate on the one metho d that is used in the program. An adapter can b e used whenev er there is the need for a c hange in in terface, but no, or v ery little, additional b eha vior b ey ond that pro vided b y the w ork er. 15.2 Comp osition problem: Ho w do y ou p ermit the creation of complex ob jects using only simple parts? solution: Pro vide a small collection of simple comp onen ts, but also allo w these comp onen ts to b e nested arbitrarily . The resulting comp osite ob jects allo w individual ob jects and comp ositions of ob jects to b e treated uniformly . F requen tly , an in teresting feature of the comp osition pattern is the merging of the is-a relation with the has-a relation. A go o d example of comp osition in the Ja v a library is the creation of design example: la y outs through the in teraction of Comp onents and Containers . There are only �v e simple t yp es of la y outs pro vided b y the standard library , and of these �v e only t w o, b order la y outs and grid la y outs, are commonly used. Eac h item in a la y out is a Comp onent . Comp osition o ccurs b ecause Containers are also Comp onents . A con tainer

  3. 15.2. COMPOSITION 261 holds its o wn la y out, whic h is again one of only a few simple v arieties. Y et the con tainer is treated as a unit in the original la y out. The structure of a comp osite ob ject is often describ ed in a tree-lik e format. Consider, for example, the la y out of the windo w sho wn in Figure 13.8 of Chapter 13. A t the application lev el there are four elemen ts to the la y out. These are a text area, a simple blank panel, and t w o panels that hold comp osite ob jects. One of these comp osite panels holds three scroll bars, while the second is holding a grid of sixteen buttons. Colo r [40,60,50] = ( h ( h ( � H h ( h ( h ( � H h ( h ( h ( � H h Colo r = � X � P � � H X � P � X � P � � H X � X � P � P � � H X ::: By nesting panels one within another, arbitrarily complex la y outs can b e created. Another example of comp osition is the class SequenceInputStream , whic h is used to catenate t w o or more input streams so that they app ear to b e a single input source (see Section 14.1.2). A SequenceInputStream InputStream (meaning it extends the is-a class InputStream ). But a SequenceInputStream also InputStream as part of its has-a in ternal state. By com bining inheritance and comp osition, the class p ermits m ultiple sequences of input sources to b e treated as a single unit. This pattern is useful whenev er it is necessary to build complex structures out of a few simple elemen ts. Note that the merging of the and relations is c haracteristic is-a has-a of the pattern (Section 15.9), although wrapp ers can b e constructed that are wr app er not comp osites.

  4. 262 CHAPTER 15. DESIGN P A TTERNS 15.3 Strategy problem: Ho w do y ou allo w the algorithm that is used to solv e a particular problem to b e easily and dynamically c hanged b y the clien t? solution: De�ne a family of algorithms with a similar in terface. Encapsulate eac h algo- rithm, and let the clien t select the algorithm to b e used in an y situation. discussion: If a complex algorithm is em b edded in a larger application, it ma y b e di�cult to extract the algorithm and replace it with another, alternativ e v ersion. If sev eral alternativ e algorithms are included in the same ob ject, b oth the complexit y and the co de of the resulting ob ject ma y b e increased unnecessarily . By separating problem and solution, it is easier for the clien t to select the solution (algorithm) appropriate for an y particular situation. example: An example of the use of the Str ate gy pattern is the creation of la y out managers in the A WT. Rather than co ding in the comp onen t library the details of ho w items are laid out on the screen, these decisions are left to the la y out manager. An in terface for La y outManager is de�ned, and �v e standard la y out managers are pro vided. The am bitious programmer is ev en allo w ed, should he or she c ho ose, to de�ne a new ob ject that satis�es the La y outManager in terface. holds La y outManager Container implemen ts inherits Applic ation GridLa y out The activities of the design comp onen t (suc h as a P anel or a Windo w ) is indep enden t of the particular la y out manager that is b eing used. This b oth simpli�es the con tainer comp onen t and p ermits a m uc h greater degree of �exibilit y in the structure of the resulting la y out than w ould b e p ossible if la y out decisions w ere an in trinsic part of the con tainer. This pattern is useful whenev er it is necessary to pro vide a set of alternativ e solutions to a problem, and the algorithms used to address the problem can b e encapsulated with a simple in terface.

Recommend


More recommend