11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 1.3 Design P<strong>at</strong>terns 15will call the appropri<strong>at</strong>e method for each child. If we want to add a new activitywith this organiz<strong>at</strong>ion, we have to change the code for every subclass. But this isrel<strong>at</strong>ively rare for our text compositing applic<strong>at</strong>ion. In contrast, adding a new objectinto the subclass hierarchy (which for this applic<strong>at</strong>ion is far more likely than addinga new rendering function) is easy. Adding a new subclass does not require changingany of the existing subclasses. It merely requires th<strong>at</strong> we define the behavior of eachactivity th<strong>at</strong> can be performed on the new subclass.This second design approach of burying the functional activity in the subclassesis called the Composite design p<strong>at</strong>tern. A detailed example for using the Compositedesign p<strong>at</strong>tern is presented in Section 5.3.1.1.3.4 Str<strong>at</strong>egyOur final example of a design p<strong>at</strong>tern lets us encapsul<strong>at</strong>e <strong>and</strong> make interchangeablea set of altern<strong>at</strong>ive actions th<strong>at</strong> might be performed as part of some larger activity.Again continuing our text compositing example, each output device th<strong>at</strong> we wishto render to will require its own function for doing the actual rendering. Th<strong>at</strong> is,the objects will be broken down into constituent pixels or strokes, but the actualmechanics of rendering a pixel or stroke will depend on the output device. Wedon’t want to build this rendering functionality into the object subclasses. Instead,we want to pass to the subroutine performing the rendering action a method or classth<strong>at</strong> does the appropri<strong>at</strong>e rendering details for th<strong>at</strong> output device. Th<strong>at</strong> is, we wishto h<strong>and</strong> to the object the appropri<strong>at</strong>e “str<strong>at</strong>egy” for accomplishing the details of therendering task. Thus, this approach is called the Str<strong>at</strong>egy design p<strong>at</strong>tern.The Str<strong>at</strong>egy design p<strong>at</strong>tern can be used to cre<strong>at</strong>e generalized sorting functions.The sorting function can be called with an additional parameter. This parameter isa class th<strong>at</strong> underst<strong>and</strong>s how to extract <strong>and</strong> compare the key values for records tobe sorted. In this way, the sorting function does not need to know any details ofhow its record type is implemented.One of the biggest challenges to underst<strong>and</strong>ing design p<strong>at</strong>terns is th<strong>at</strong> sometimesone is only subtly different from another. For example, you might be confusedabout the difference between the composite p<strong>at</strong>tern <strong>and</strong> the visitor p<strong>at</strong>tern.The distinction is th<strong>at</strong> the composite design p<strong>at</strong>tern is about whether to give controlof the traversal process to the nodes of the tree or to the tree itself. Both approachescan make use of the visitor design p<strong>at</strong>tern to avoid rewriting the traversal functionmany times, by encapsul<strong>at</strong>ing the activity performed <strong>at</strong> each node.But isn’t the str<strong>at</strong>egy design p<strong>at</strong>tern doing the same thing? The difference betweenthe visitor p<strong>at</strong>tern <strong>and</strong> the str<strong>at</strong>egy p<strong>at</strong>tern is more subtle. Here the differenceis primarily one of intent <strong>and</strong> focus. In both the str<strong>at</strong>egy design p<strong>at</strong>tern <strong>and</strong> the visitordesign p<strong>at</strong>tern, an activity is being passed in as a parameter. The str<strong>at</strong>egy designp<strong>at</strong>tern is focused on encapsul<strong>at</strong>ing an activity th<strong>at</strong> is part of a larger process, so

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!