适配器模式

什么是适配器?

你想实现一些功能,而这些功能的一部分已经实现了,有可能是你以前写的代码,也有可能是从外面买回来的产品,对于这些现成的类,我们一般是不能通过修改代码的方式把我们需要的功能添加进去,首先是因为直接修改源代码会带来许多的错误,其次是因为买回来的产品一般不会给你源代码的,所以不能直接修改来添加功能。适配器在这种情况下就发挥作用了,就好像有一条比较粗的水管和一条比较小的水管,我们要把他们连接起来,这时候,适配器就是起到了连接的作用。

怎么实现适配器?

适配器有一个核心的接口,这个接口有所需要的所有方法,包括以实现和未实现的,然后,由于适配器模式又分为类适配器和对象适配器,在类适配器中,我们是通过继承接口和原有类的方法去实现一个新的类,并且只需实现为实现的方法;而对象适配器则是通过继承接口和在类里面实现所有的方法。

下面给出一个简单的例子,实现一个类,该类有一个实数数组的排序的方法。现在想对一个整数排序,请利用类适配器完成该实验。

两种适配的接口都为:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package adapter;

/**
 *
 * @author Administrator
 */
public interface SortTarget {
    public String iSort(int [] a);
    public String rSort(double[] a);
}

两种适配器的已实现类都相同:

package adapter;

import java.util.Arrays;

public class RealSort {

    
//    public static void main(String[] args)
    public String rSort(double[] a) {
        
        for(int i=0;i<a.length;i++){
            for(int j=i+1;j<a.length;j++){
                if (a[i]>a[j]) {
                    double temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;                    
                }
            }
        }
        StringBuilder str =new StringBuilder("");
        for (double t :a) {
//            System.out.println(t);
            str.append(Double.toString(t)+"\n");
        }
        return new String(str);
    }

}

两者的区别:

类适配器的整数排列的类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package adapter;

/**
 *
 * @author Administrator
 */
public class IntSort extends RealSort implements SortTarget{
    public String iSort(int [] a){
        double[] b=new double[a.length];
        for(int i=0;i<a.length;i++){
            b[i]=a[i];   //将int[]数组转化为double[]数组
        }
        return rSort(b);    //直接调用SortTarget中的rSort方法
    }
//        public static void main(String[] args){
//        IntSort realSort=new IntSort();
//        int[] a = {1, 5, 3, 8, 9};
//        realSort.iSort(a);
//    }
    
}

对象适配器的整数排列的类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package adapter_object;

/**
 *
 * @author Administrator
 */
public class IntSort implements SortTarget {

    private double[] b;
    RealSort real;   //定义一个 RealSort对象,这是实现对象适配器的第一步
    public String iSort(int[] a) {
        b = new double[a.length];
        for (int i = 0; i < a.length; i++) {
            b[i] = a[i];      //将int[]数组转化为double[]数组
        }
        real=new RealSort();   //实例化
        return rSort(b);
    }

    public String rSort(double[] a) {   //实现已有的方法
        return real.rSort(a);    
    }
}

例子就讲到这里,希望大家能够明白!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值