阅读 java.util.Arrays#sort 代码,学习设计模式。
1、阅读标准类库代码 java.util.Arrays#sort(T[], java.util.Comparator<? super T>) 。
2、分析其中涉及的设计模式,指出此种设计模式定义的各个角色在代码中的体现。
策略模式
上下文角色(Context):用来操作策略的上下文环境,屏蔽高层模块(客户端)
对策略,算法的直接访问,封装可能存在的变化;
抽象策略角色(Strategy):规定策略或算法的行为;
具体策略角色(ConcreteStrategy):具体的策略或算法实现;
作者:Whyn
链接:https://www.jianshu.com/p/3bcf55cf83d3
来源:简书
3、编写代码,对给定的成绩数组按照最终总成绩递减排序。
public abstract class Strategy {
public static final String DESC = "desc"; // 降序
public static final String ASC = "asc"; // 升序
public void sort(int a[], String order) {
if (a == null) {
throw new IllegalArgumentException("array can not be null");
}
if (order.equals(ASC)) {
asc(a);
} else if (order.equals(DESC)) {
desc(a);
}
}
protected abstract void desc(int[] a);
protected abstract void asc(int[] a);
}
```cpp
public class BubbleStrategy extends Strategy{
@Override
protected void desc(int[] a) {
int d = a.length;
boolean isChange = true;
for (int i = 0; i < d - 1 && isChange; i++) {
isChange = false;
for (int j = 0; j < d - 1 - i; j++) {
if (a[j] > a[j + 1]) {
a[j] = a[j] ^ a[j + 1];
a[j + 1] = a[j] ^ a[j + 1];
a[j] = a[j] ^ a[j + 1];
isChange = true;
}
}
}
}
@Override
protected void asc(int[] a) {
int d = a.length;
boolean isChange = true;
for (int i = 0; i < d - 1 && isChange; i++) {
isChange = false;
for (int j = 0; j < d - 1 - i; j++) {
if (a[j] < a[j + 1]) {
a[j] = a[j] ^ a[j + 1];
a[j + 1] = a[j] ^ a[j + 1];
a[j] = a[j] ^ a[j + 1];
isChange = true;
}
}
}
}
}
public class QuickStrategy extends Strategy{
public static void quickSort(int[] arr,int low,int high){
int i,j,temp;
i=low;//低端下标
j=high;//高端下标
temp=arr[i];//取第一个元素为标准元素。
while(i<j){//递归出口是 low>=high
while(i<j&&temp<=arr[j]) //在数组的后端扫描
j--;//移动后j再减了一个,即在temp前一个咯
if(i<j){
arr[i]=arr[j];
i++;
}
while(i<j&&arr[i]<temp) //在数组的左端扫描
i++;
if(i<j){
arr[j]=arr[i];
j--;
}
}//while完,即第一盘排序
arr[i]=temp;//把temp值放到它该在的位置。
// System.out.println("第 次排序----->"+Arrays.toString(arr));
if(low<i)
quickSort(arr,low,i-1);//对左端子数组递归
if(i<high)
quickSort(arr,j+1,high);//对右端子数组递归
}
public static void quickSort2(int[] arr,int high,int low) {
int i, j, temp;
i = high;//高端下标
j = low;//低端下标
temp = arr[i];//取第一个元素为标准元素。
while (i < j) {//递归出口是 low>=high
while (i < j && temp > arr[j])//后端比temp小,符合降序,不管它,low下标前移
j--;//while完后指比temp大的那个
if (i < j) {
arr[i] = arr[j];
i++;
}
while (i < j && temp < arr[i])
i++;
if (i < j) {
arr[j] = arr[i];
j--;
}
}//while完,即第一盘排序
arr[i] = temp;//把temp值放到它该在的位置。
if (high < i) //注意,下标值
quickSort2(arr, high, i - 1);//对左端子数组递归
if (i < low) //注意,下标值
quickSort2(arr, i + 1, low);//对右端子数组递归 ;对比上面例子,其实此时i和j是同一下标!!!!!!!!!!!!!
}
@Override
protected void desc ( int[] a){
int len = a.length;
quickSort(a, 0, len - 1);
}
@Override
protected void asc(int[] a) {
int len=a.length;
quickSort2(a,0,len-1);
}
}
public class Context {
private Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void sortOrderByDesc(int[] array) {
strategy.sort(array, Strategy.DESC);
}
public void sortOrderByAsc(int[] array) {
strategy.sort(array, Strategy.ASC);
}
}
public class Client {
public static void main(String[] args) {
int array[] = {2, 19, 5, 90, 3, 54, 7, 6, 8, 52, 11, 68, 33, 3, 5};
System.out.println("原序列:");
printArray(array);
Context context = new Context(new BubbleStrategy());
context.sortOrderByDesc(array);
System.out.println("冒泡降序:");
printArray(array);
context.sortOrderByAsc(array);
System.out.println("冒泡升序:");
printArray(array);
context = new Context(new QuickStrategy());
context.sortOrderByDesc(array);
System.out.println("快排降序:");
printArray(array);
context.sortOrderByAsc(array);
System.out.println("快排升序:");
printArray(array);
}
private static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "\t");
}
System.out.println();
}
}