23.07.2012 Views

Design Patterns Explained

Design Patterns Explained

Design Patterns Explained

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Figure 7-5 The Adapter pattern: Circle "wraps" XXCircle.<br />

An example of wrapping is shown in Example 7-1.<br />

Example 7-1 Java Code Fragments: Implementing the Adapter Pattern<br />

class Circle extends Shape<br />

{ private XXCircle pxc;<br />

public Circle () { pxc= new<br />

XXCircle();<br />

}<br />

void public display() {<br />

pxc.displayIt(); } }<br />

Chapter 7 • The Adapter Pattern 1O1<br />

Using the Adapter pattern allowed me to continue using polymorphism<br />

with Shape. In other words, the client objects of Shape do<br />

not know what types of shapes are actually present. This is also an<br />

example of our new thinking of encapsulation as well— the class<br />

Shape encapsulates the specific shapes present. The Adapter pattern<br />

is most commonly used to allow for polymorphism. As we shall<br />

see in later chapters, it is often used to allow for polymor phism<br />

required by other design patterns.<br />

What we<br />

accomplished

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

Saved successfully!

Ooh no, something went wrong!