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:



UML diagram:




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.



Now client can use the new Adapter.



Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: