23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

(4,3,5,2,9)<br />

5<br />

get(2)<br />

(4,3,5,2,9)<br />

set(3,8)<br />

2<br />

(4,3,5,8,9)<br />

6.1.2 The Adapter Pattern<br />

Classes are often written to provide similar functionality to other classes. The<br />

adapter design pattern applies to any context where we want to modify an exist<strong>in</strong>g<br />

class so that its methods match those of a related, but different, class or <strong>in</strong>terface.<br />

One general way for apply<strong>in</strong>g the adapter pattern is to def<strong>in</strong>e the new class <strong>in</strong> such a<br />

way that it conta<strong>in</strong>s an <strong>in</strong>stance of the old class as a hidden field, <strong>and</strong> implement<br />

each method of the new class us<strong>in</strong>g methods of this hidden <strong>in</strong>stance variable. The<br />

result of apply<strong>in</strong>g the adapter pattern is that a new class that performs almost the<br />

same functions as a previous class, but <strong>in</strong> a more convenient way, has been created.<br />

With respect to our discussion of the array list ADT, we note that this ADT is<br />

sufficient to def<strong>in</strong>e an adapter class for the deque ADT, as shown <strong>in</strong> Table 6.1. (See<br />

also Exercise C-6.8.)<br />

Table 6.1:<br />

array list.<br />

Realization of a deque by means of an<br />

Deque Method<br />

Realization with Array-List Methods<br />

size(), isEmpty()<br />

size(), isEmpty()<br />

getFirst()<br />

get(0)<br />

getLast()<br />

316

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

Saved successfully!

Ooh no, something went wrong!