首页 > 其他 > 详细

适配器模式

时间:2018-01-16 21:52:31      阅读:250      评论:0      收藏:0      [点我收藏+]
 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 }

 

适配器模式

原文:https://www.cnblogs.com/celine/p/8298062.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!