12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

16 Chap. 1 <strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Algorithm</strong>sany of the existing subclasses. It merely requires that we define the behavior of eachactivity that can be performed on that subclass.This second design approach of burying the functional activity in the subclassesis called the Composite design pattern. A detailed example for using the Compositedesign pattern is presented in Section 5.3.1.1.3.4 StrategyOur final example of a design pattern lets us encapsulate <strong>and</strong> make interchangeablea set of alternative actions that might be performed as part of some larger activity.Again continuing our text compositing example, each output device that we wish<strong>to</strong> render <strong>to</strong> will require its own function for doing the actual rendering. That is,the objects will be broken down in<strong>to</strong> constituent pixels or strokes, but the actualmechanics of rendering a pixel or stroke will depend on the output device. Wedon’t want <strong>to</strong> build this rendering functionality in<strong>to</strong> the object subclasses. Instead,we want <strong>to</strong> pass <strong>to</strong> the subroutine performing the rendering action a method or classthat does the appropriate rendering details for that output device. That is, we wish<strong>to</strong> h<strong>and</strong> <strong>to</strong> the object the appropriate “strategy” for accomplishing the details of therendering task. Thus, we call this approach the Strategy design pattern.The Strategy design pattern will be discussed further in Chapter 7. There, asorting function is given a class (called a compara<strong>to</strong>r) that underst<strong>and</strong>s how <strong>to</strong> extract<strong>and</strong> compare the key values for records <strong>to</strong> be sorted. In this way, the sortingfunction does not need <strong>to</strong> know any details of how its record type is implemented.One of the biggest challenges <strong>to</strong> underst<strong>and</strong>ing design patterns is that manyof them appear <strong>to</strong> be pretty much the same. For example, you might be confusedabout the difference between the composite pattern <strong>and</strong> the visi<strong>to</strong>r pattern. Thedistinction is that the composite design pattern is about whether <strong>to</strong> give control ofthe traversal process <strong>to</strong> the nodes of the tree or <strong>to</strong> the tree itself. Both approachescan make use of the visi<strong>to</strong>r design pattern <strong>to</strong> avoid rewriting the traversal functionmany times, by encapsulating the activity performed at each node.But isn’t the strategy design pattern doing the same thing? The difference betweenthe visi<strong>to</strong>r pattern <strong>and</strong> the strategy pattern is more subtle. Here the differenceis primarily one of intent <strong>and</strong> focus. In both the strategy design pattern <strong>and</strong> the visi<strong>to</strong>rdesign pattern, an activity is being passed in as a parameter. The strategy designpattern is focused on encapsulating an activity that is part of a larger process, sothat different ways of performing that activity can be substituted. The visi<strong>to</strong>r designpattern is focused on encapsulating an activity that will be performed on allmembers of a collection so that completely different activities can be substitutedwithin a generic method that accesses all of the collection members.

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

Saved successfully!

Ooh no, something went wrong!