views:

103

answers:

4

I've been learning the adapter and facade design patterns for an upcoming junior development role i'm applying for, as i'm expecting to be asked what i know about the pattern and times i've used it. While the pattern itself seems straight forward - i'm struggling to think of a practical use i could use it for in a personal project.

So can anyone suggest an idea for a use for it within a small personal project?

Also does the pattern appear anywhere within the Java API?

+3  A: 

Not sure if this counts as an adapter (maybe it does a little more than a pure interface conversion, what with translating bytes to characters), but how about java.io.InputStreamReader, which turns an InputStream into a Reader?

And maybe java.util.concurrent.ExecutorService is a facade that hides the detailed interactions between Threads, and Queues, and Runnables from the user?

Thilo
Javadoc says, that `InputStreamReader` is a *Bridge* but I do not agree because the bridge pattern would require `InputStreamReader` to be abstract, while it is concrete. So yes, to me it adapts a real InputStream to the Reader interface.
Andreas_D
+2  A: 

For uses in Java API of design patterns, look at this question.

Quoting Wikipedia:

In computer programming, 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.

So you will use it quite rarely. It is just adapting two incompatible interfaces.

And remember when you don't need a design pattern, just don't use it. Only because it is written in the big books, it doesn't mean it serves all your needs and you must follow it blindly.

Petar Minchev
+1  A: 

Head First Design patterns starts the chapter about the Adapter pattern with these words:

In this chapter we're going to attempt such impossible feats as putting a square peg into a round hole.

The purpose is to make [some objects] interfaces look like something they're not. I doubt that you use this pattern while you design an application. It's useful if you have an existing application and you need to make it work with 3rd party libraries or tools.

There is one Adapter in the Java API that we all know, although it's quite hidden. You get is with a fectory method from Arrays:

Arrays.asList(T... a)

The method returns an instance of Arrays.ArrayList and this object adapts the array to the List interface.

Andreas_D
A: 

For a real-world example of the adapter pattern, check out my answer to this stack overflow question.

mikemanne