适配器模式

来源:互联网 发布:笔记本触摸屏校准软件 编辑:程序博客网 时间:2024/05/21 11:02

一、适配器模式的定义

适配器模式:将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。

适配器模式的UML图:


客户使用适配器的过程:

1、客户通过目标接口调用适配器的方法对适配器发出请求。

2、适配器使用被适配者接口吧请求转换成被适配者的一个或者多个调用接口。

3、客户收到调用结果,但并未察觉这一切是适配器在起作用。



二、适配器模式的优点


1、创建适配器进行接口装好,让不兼容的接口编程兼容,让客户从实现的接口解耦。

2、允许客户使用新的库和子集合,无须改变任何代码,由适配器转化即可。

三、适配器模式的使用场景


1、当要使用一个现有的类,但是它的接口不符合需要,就使用适配器。

2、当系统的数据和行为都正确,但接口不符合,同时双方都不太容易修改的时候,应该考虑用适配器,目的使控制范围之外的一个原有对象与某个接口匹配。

3、使用第三方开发组件,组件的接口与自己程序的接口是不相同的,而我们也没必要为迎合它而改动自己的接口,可以考虑使用适配器来解决接口问题。


四、现实世界中的适配器模式

在实际开发中,经常碰到需要将数组转化成List的需求。Java里实现也比较简单,调用Arrays.asList()的方法就能实现。asList的实现原理,就是通过适配器模式,通过访问List的方式访问数组。

看下asList()的实现:

/**     * Returns a {@code List} of the objects in the specified array. The size of the     * {@code List} cannot be modified, i.e. adding and removing are unsupported, but     * the elements can be set. Setting an element modifies the underlying     * array.     *     * @param array     *            the array.     * @return a {@code List} of the elements of the specified array.     */    @SafeVarargs    public static <T> List<T> asList(T... array) {        return new ArrayList<T>(array);    }

很简单,直接返回一个ArrayList对象,注意,这个ArrayList不是我们经常用到的那个ArrayList,而是Arrays类的一个内部类,其实它就是一个适配器。看看它的实现代码:

 private static class ArrayList<E> extends AbstractList<E> implements            List<E>, Serializable, RandomAccess {        private static final long serialVersionUID = -2764017481108945198L;        private final E[] a;        ArrayList(E[] storage) {            if (storage == null) {                throw new NullPointerException("storage == null");            }            a = storage;        }        @Override        public boolean contains(Object object) {            if (object != null) {                for (E element : a) {                    if (object.equals(element)) {                        return true;                    }                }            } else {                for (E element : a) {                    if (element == null) {                        return true;                    }                }            }            return false;        }        @Override        public E get(int location) {            try {                return a[location];            } catch (ArrayIndexOutOfBoundsException e) {                throw java.util.ArrayList.throwIndexOutOfBoundsException(location, a.length);            }        }        @Override        public int indexOf(Object object) {            if (object != null) {                for (int i = 0; i < a.length; i++) {                    if (object.equals(a[i])) {                        return i;                    }                }            } else {                for (int i = 0; i < a.length; i++) {                    if (a[i] == null) {                        return i;                    }                }            }            return -1;        }        @Override        public int lastIndexOf(Object object) {            if (object != null) {                for (int i = a.length - 1; i >= 0; i--) {                    if (object.equals(a[i])) {                        return i;                    }                }            } else {                for (int i = a.length - 1; i >= 0; i--) {                    if (a[i] == null) {                        return i;                    }                }            }            return -1;        }        @Override        public E set(int location, E object) {            E result = a[location];            a[location] = object;            return result;        }        @Override        public int size() {            return a.length;        }        @Override        public Object[] toArray() {            return a.clone();        }        @Override        @SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})        public <T> T[] toArray(T[] contents) {            int size = size();            if (size > contents.length) {                Class<?> ct = contents.getClass().getComponentType();                contents = (T[]) Array.newInstance(ct, size);            }            System.arraycopy(a, 0, contents, 0, size);            if (size < contents.length) {                contents[size] = null;            }            return contents;        }    }

这个ArrayList实现List的接口,并重写List的一些方法,里面的实现委托给数组的各个方法。


0 0
原创粉丝点击