适配器模式

 1 package com.jdk7.chapter2.adapter;
 2 /**
 3  * AdapterPrint适配器既继承了PrintIntArray的打印数组的功能,又实现了排序接口的排序功能SortNumber,
 4  * 把不相关的功能集合到一起
 5  * @author Administrator
 6  *
 7  */
 8 
 9 //需要使用不相关类的方法则继承该类即可继承该类的功能
10 public class AdapterPrint extends PrintIntArray implements SortNumber{
11     //将接口作为适配器的私有对象类型
12     private SortNumber sort;
13     //将适配器构造函数中对象类型的复件引用传给适配器中的私有对象变量
14     public AdapterPrint(SortNumber sort1){
15         this.sort = sort1;
16     }
17     @Override
18     public int[] sortASCNumber(int[] intArray) {
19         if(this.sort!=null){
20             return this.sort.sortASCNumber(intArray);
21         }else{
22             return null;
23         }
24     }
25     
26     public static void main(String[] args) {
27         int[] array = new int[] {5,8,7,6,1,4,3,2};
      //使用工厂模式创建排序接口对象
28 AdapterPrint adapter = new AdapterPrint(Factory.getSortNumber(Factory.BUBBLE_SORT)); 29 adapter.printIntArray(adapter.sortASCNumber(array)); 30 } 31 }

 

posted @ 2018-01-16 18:18  celineluo  阅读(103)  评论(0编辑  收藏  举报