16.01.2013 Views

Downloading

Downloading

Downloading

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

11.2 EScope4: Adding Zoom and Grab Options Using the Decorator Pattern 151<br />

from GraphMetrics) because they emerge from an interaction between the<br />

graph options and graph data objects and because the size of this class is yet<br />

to become too unmanageable. (If, in the future, this class did grow to be “too<br />

big” then we could consider moving these methods to a new, more narrowly<br />

defined, metrics class and keep the graph mediator class to manage the class<br />

interactions.)<br />

The GraphMediator class is shown in Section E.5.3 on page 324. It contains<br />

associations with objects from all of the concrete decorator classes and it<br />

constructs and returns composite decorators using three methods:<br />

• createDecorator<br />

• concatenateDecorators<br />

• getDecorator<br />

The createDecorator method is called whenever a new graph manipulation<br />

option is chosen, it selects the correct combination of decorator objects<br />

to chain together and chains them together in the appropriate order.<br />

The concatenateDecorator method performs the chaining and the<br />

getDecorator method returns a link to the composite decorator object. (Note<br />

that concatenateDecorator accepts a variable number of arguments which<br />

is new to Java from Java 1.5.) Listings of all three methods follow. The<br />

createDecorator method is a type of factory method which is a pattern<br />

which will be discussed in Chapter 17.<br />

public void createDecorator ()<br />

{<br />

/∗ Build the decorator chain .<br />

The elements in the chain d e f i n e what<br />

is going to be displayed .<br />

If no data or bad data then the decorator chain<br />

is only composed of a DrawCentredMessage object<br />

containing an error message.∗/<br />

if ( graphData == null || ! isGraphData)<br />

{<br />

if ( graphData != null && isDataError)<br />

warningDecorator . setMessage(<br />

”Unexpected data . See console message .”);<br />

else<br />

warningDecorator . setMessage(”No data”);<br />

//Build decorator chain with a centred message<br />

decorator = warningDecorator ;<br />

}<br />

else //Build the decorator chain based on the<br />

//current display mode<br />

{<br />

if ( graphOptions . getDisplayMode()==<br />

graphOptions .getCROSSHAIR())

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

Saved successfully!

Ooh no, something went wrong!