Java 什么是适配器对象模式?

Java 什么是适配器对象模式?,java,collections,Java,Collections,我在阅读collection framework的优点时,发现一句话,“Java Collections framework使您无需编写适配器对象或转换代码来连接API。”我无法理解这一点 [链接] 我在谷歌上找到了一些适配器模式和其他东西……但我想知道“适配器对象” 有谁能解释一下……当你想让两个不同的类和不兼容的接口一起工作时,适配器模式是如何使用的。看看这个例子吧我想我有一个粗略的例子。假设您必须使用2个API—其中一个与手机相关,另一个与书籍相关。假设移动API开发人员为您提供以下API

我在阅读collection framework的优点时,发现一句话,“Java Collections framework使您无需编写适配器对象或转换代码来连接API。”我无法理解这一点

[链接]

我在谷歌上找到了一些适配器模式和其他东西……但我想知道“适配器对象”


有谁能解释一下……

当你想让两个不同的类和不兼容的接口一起工作时,适配器模式是如何使用的。看看这个例子吧

我想我有一个粗略的例子。假设您必须使用2个API—其中一个与手机相关,另一个与书籍相关。假设移动API开发人员为您提供以下API:

public class MobileList {
    private Mobile[] mobiles;
    //other fields

    public void addMobileToList(Mobile mobile) {
        //some code to add mobile
    }

    public void getMobileAtIndex(int index) {
        return mobiles[index];
    }

    //maybe other methods
}
public class BookList {
    private Book[] books;
    //other fields

    public void addBook(Book book) {
       //some code to add book
    }

    public Book[] getAllBooks() {
        return books;
    }

}
假设books API开发者为您提供了以下API:

public class MobileList {
    private Mobile[] mobiles;
    //other fields

    public void addMobileToList(Mobile mobile) {
        //some code to add mobile
    }

    public void getMobileAtIndex(int index) {
        return mobiles[index];
    }

    //maybe other methods
}
public class BookList {
    private Book[] books;
    //other fields

    public void addBook(Book book) {
       //some code to add book
    }

    public Book[] getAllBooks() {
        return books;
    }

}
现在,如果您的代码只在以下“产品”界面上工作:

interface Products {
    void add(Product product);
    Product get(int index);
}
class MobileListAdapter implements Products {
    private MobileList mobileList;

    public void add(Product mobile) {
        mobileList.addMobileToList(mobile);
    }

    public Product get(int index) {
        return mobileList.getMobileAtIndex(index);
    }
}

class BookListAdapter implements Products {
    private BookList bookList;

    public void add(Product book) {
        bookList.add(book);
    }

    public Product get(int index) {
        return bookList.getAllBooks()[index];
    }
}
您必须编写以下实现所需接口的“适配器”对象:

interface Products {
    void add(Product product);
    Product get(int index);
}
class MobileListAdapter implements Products {
    private MobileList mobileList;

    public void add(Product mobile) {
        mobileList.addMobileToList(mobile);
    }

    public Product get(int index) {
        return mobileList.getMobileAtIndex(index);
    }
}

class BookListAdapter implements Products {
    private BookList bookList;

    public void add(Product book) {
        bookList.add(book);
    }

    public Product get(int index) {
        return bookList.getAllBooks()[index];
    }
}
请注意,每个这样的
产品
API也可以有各种方法和各种方法名称。如果您的代码只希望在
产品
界面上工作,那么您必须为每个新的
产品
编写这样的“适配器”

这就是Java集合提供帮助的地方(
Java.util.List
用于此特定示例)。通过Java的
List
接口,开发人员可以简单地给出
List
List
,您可以在这些
List
上调用
get(index)
add(product)
,而不需要任何适配器类。这是因为现在
MobileList
BookList
有一组通用的方法名称和行为。我认为这就是它在文档中所说的意思

通过促进无关API之间的互操作性

本例中不相关的API为
MobileList
BookList