`

Adapter适配器模式

    博客分类:
  • J2SE
 
阅读更多
/**
 * Adapter适配器模式<br>
 * 打印数组的类
 */
public class Printer {
	/**
	 * 输出数组到标准输出,数组元素间用空格隔开,整个数组在一行显示
	 * @param array
	 */
	public void printIntArray(int[] array)
	{
		if(array != null)
		{
			for(int i=0;i<array.length;i++)
			{
				System.out.print(array[i] + " ");
			}
			System.out.println();
		}
	}
}

/**
 * 一个既能打印数组,又能对数组进行排序的类
 */
public class PrintAdapter extends Printer implements ISortNumber 
{
	private ISortNumber mySort;
	public PrintAdapter(ISortNumber sort)
	{
		super();
		this.mySort = sort;
	}
	
	public int[] sortASC(int[] intArray) {
		if(this.mySort!=null)
		{
			return this.mySort.sortASC(intArray);
		}
		else
		{
			return null;
		}
	}
	
	public static void main(String[] args)
	{
		int[] array = new int[]{7,9,4,6,2,-1,12};
		PrintAdapter adapter = new PrintAdapter(Factory.getOrderNumber(Factory.BUBBLE_SORT));
		adapter.printIntArray(adapter.sortASC(array));
	}
	
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics