Adapter Pattern

The adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. Foloowing image says it all:

AdapterPatternImage1

 

UML diagram:

AdapterPatternImage2

 

 

When to Use It

  1. You want to use existing class and its interface does not match the one you need.
  2. You want to create a resuable class that cooperates with unrelated or unforeseen classes that is, class that don’t necessarily have compatible interfaces.

Consider that we have a third party library that provides print string functionality throughPrintString class. AdapterPatternProgram1

Client deals with ArrayList<String> but not with string.We have provided aPrintableList interface that expects the client input.

AdapterPatternProgram2AdapterPatternProgram3

 

Now client can use the new Adapter.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: