开发者

Use for the adapter pattern in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. 开发者_如何学编程

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 9 years ago.

Improve this question

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?


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.


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?


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.


Adapter pattern is required in following scenario:

Say you have defined an interface I1 with method M1 and M2

C1 and C2 implements this interface I1, now for C1 while implementing M1 and M2 you have found no help from other existing classes so you need to write all logic by yourself.

Now while implementing class C2 you have come across class C3 with methods M3 and M4 that can be used to implement M1 and M2 for C2 so to utilize those M3 and M4 in class C2 you extends class C3 and use M3 and M4 of C3.

In this example C2 becomes Adapter class and C3 becomes adaptee

package com.design.patterns;

public class AdapterExample {
    public static void main(String[] args) {
        Shape line = new LineShape();
        line.draw();

        Shape text = new TextShape();
        text.draw();
    }
}

//==Start from here
interface Shape{
    public void draw();
}

class LineShape implements Shape{
    @Override
    public void draw() {
        System.out.println("write some logic and draw line");
    }   
}

//Adapter
class TextShape extends TextView implements Shape{
    @Override
    public void draw() {
        System.out.println("logic is already there in class TextView");
        drawText();
    }   
}

// Adaptee
class TextView{
    public void drawText() {
        System.out.println("Drawing Text Shape");
    }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜