策略模式 strategy
java.lang.Comparable#compareTo(Object)
简单的比较方法
对数组简单的排序
新建排序工具类SorterSmSimpleUtil
public class SorterSmSimpleUtil {
public void sort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int minPos = i;
for (int j = i + 1; j < arr.length; j++) {
minPos = arr[j] < arr[minPos] ? j : minPos;
}
swap(arr, i, minPos);
}
}
static void swap(int[] arr, int i, int minPos) {
int temp = arr[i];
arr[i] = arr[minPos];
arr[minPos] = temp;
}
}
测试
public static void main(String[] args) {
int[] a = {9, 2, 3, 5, 7, 1, 4};
SorterSmSimpleUtil sorterSmSimpleUtil = new SorterSmSimpleUtil();
sorterSmSimpleUtil.sort(a);
System.out.println(Arrays.toString(a));
}
}
对对象进行排序
新建对象里面添加compareTo()
public class SorterObjectMainCat {
int weight, height;
public SorterObjectMainCat(int weight, int height) {
this.weight = weight;
this.height = height;
}
@Override
public String toString() {
return "Cat{" + "weight=" + weight + ", height=" + height + '}';
}
public int compareTo(SorterObjectMainCat cat) {
if (this.weight < cat.weight) {
return -1;
} else if (this.weight > cat.weight) {
return 1;
} else {
return 0;
}
}
}
编写排序工具类SorterObjectUtil
public class SorterObjectUtil {
public void sortCat(SorterObjectMainCat[] arr) {
for (int i = 0; i < arr.length; i++) {
int minPos = i;
for (int j = i + 1; j < arr.length; j++) {
minPos = arr[j].compareTo(arr[minPos])==-1 ? j : minPos;
}
swap(arr, i, minPos);
}
}
static void swap(SorterObjectMainCat[] arr, int i, int minPos) {
SorterObjectMainCat temp = arr[i];
arr[i] = arr[minPos];
arr[minPos] = temp;
}
}
编写测试方法
public class SorterObjectMain {
public static void main(String[] args) {
SorterObjectMainCat[] catArray = {new SorterObjectMainCat(3, 3), new SorterObjectMainCat(5, 5),
new SorterObjectMainCat(1, 1)};
SorterObjectUtil sorterObjectUtil = new SorterObjectUtil();
sorterObjectUtil.sortCat(catArray);
System.out.println("catArray = " + Arrays.toString(catArray));
}
}
抽象使用参数使用Object
`* @see java.lang.Comparable#compareTo(Object)方式实现
定义接口ComparableMy
public interface ComparableMy {
int compareTo(Object o);
}
定义对象,实现ComparableMy
接口,重写compareTo()
public class SorterComparableMyCat implements ComparableMy {
int weight, height;
public SorterComparableMyCat(int weight, int height) {
this.weight = weight;
this.height = height;
}
@Override
public String toString() {
return "Cat{" + "weight=" + weight + ", height=" + height + '}';
}
@Override
public int compareTo(Object o) {
SorterComparableMyCat cat = (SorterComparableMyCat) o;
if (this.weight < cat.weight) {
return -1;
} else if (this.weight > cat.weight) {
return 1;
} else {
return 0;
}
}
}
定义排序工具类SorterComparableMyUtil
public class SorterComparableMyUtil {
public void sort(ComparableMy[] arr) {
for (int i = 0; i < arr.length; i++) {
int minPos = i;
for (int j = i + 1; j < arr.length; j++) {
minPos = arr[j].compareTo(arr[minPos]) == -1 ? j : minPos;
}
swap(arr, i, minPos);
}
}
static void swap(ComparableMy[] arr, int i, int minPos) {
ComparableMy temp = arr[i];
arr[i] = arr[minPos];
arr[minPos] = temp;
}
}
编写测试方法
public class SorterComparableMyMain {
public static void main(String[] args) {
SorterComparableMyCat[] catArrayComMy = {new SorterComparableMyCat(3, 3), new SorterComparableMyCat(5, 5),
new SorterComparableMyCat(1, 1)};
SorterComparableMyUtil sorterComparableMyUtil = new SorterComparableMyUtil();
sorterComparableMyUtil.sort(catArrayComMy);
System.out.println("catArray = " + Arrays.toString(catArrayComMy));
}
}
抽象使用参数使用泛型
定义接口ComparableMyGJ
public interface ComparableMyGJ<T> {
int compareTo2(T t);
}
定义对象,实现ComparableMyGJ
接口,重写compareTo()
public class SorterComparableMyFGJCat implements ComparableMyGJ<SorterComparableMyFGJCat> {
int weight, height;
public SorterComparableMyFGJCat(int weight, int height) {
this.weight = weight;
this.height = height;
}
@Override
public String toString() {
return "Cat{" + "weight=" + weight + ", height=" + height + '}';
}
@Override
public int compareTo2(SorterComparableMyFGJCat cat) {
if (this.weight < cat.weight) {
return -1;
} else if (this.weight > cat.weight) {
return 1;
} else {
return 0;
}
}
}
工具类SorterComparableMyGJUtil
public class SorterComparableMyGJUtil {
public void sort(ComparableMyGJ[] arr) {
for (int i = 0; i < arr.length; i++) {
int minPos = i;
for (int j = i + 1; j < arr.length; j++) {
minPos = arr[j].compareTo2(arr[minPos]) == -1 ? j : minPos;
}
swap(arr, i, minPos);
}
}
static void swap(ComparableMyGJ[] arr, int i, int minPos) {
ComparableMyGJ temp = arr[i];
arr[i] = arr[minPos];
arr[minPos] = temp;
}
}
编写测试方法
public class SorterComparableMyGJMain {
public static void main(String[] args) {
SorterComparableMyFGJCat[] catArrayComMy = {new SorterComparableMyFGJCat(3, 3),
new SorterComparableMyFGJCat(5, 5), new SorterComparableMyFGJCat(1, 1)};
SorterComparableMyGJUtil sorterComparableMy2Util = new SorterComparableMyGJUtil();
sorterComparableMy2Util.sort(catArrayComMy);
System.out.println("catArray = " + Arrays.toString(catArrayComMy));
}
}
java.util.Comparator#compare(Object, Object)策略模式
策略模式开始,此处需要根据策略来进行排序,策略也需要被作为参数进行传入 编写过程中需要遵守开闭原则
定义接口ComparatorMy
public interface ComparatorMy<T> {
int compare(T o1, T o2);
}
定义需要比较大小的对象类SorterComparatorCat
public class SorterComparatorCat {
int weight, height;
public SorterComparatorCat(int weight, int height) {
this.weight = weight;
this.height = height;
}
@Override
public String toString() {
return "SorterComparatorCat{" +
"weight=" + weight +
", height=" + height +
'}';
}
}
编写排序工具类
public class SorterComparatorUtil<T> {
public void sort(T[] arr, ComparatorMy<T> comparatorMy) {
for (int i = 0; i < arr.length; i++) {
int minPos = i;
for (int j = i + 1; j < arr.length; j++) {
minPos = comparatorMy.compare(arr[j], arr[minPos]) == -1 ? j : minPos;
}
swap(arr, i, minPos);
}
}
void swap(T[] arr, int i, int minPos) {
T temp = arr[i];
arr[i] = arr[minPos];
arr[minPos] = temp;
}
}
编写排序策略1--根据升高排序
public class CatComparatorByHeight implements ComparatorMy<SorterComparatorCat> {
@Override
public int compare(SorterComparatorCat o1, SorterComparatorCat o2) {
if (o1.height < o2.height) {
return -1;
} else if (o1.height > o2.height) {
return 1;
} else {
return 0;
}
}
}
编写排序策略2--根据体重排序
public class CatComparatorByWeight implements ComparatorMy<SorterComparatorCat> {
@Override
public int compare(SorterComparatorCat o1, SorterComparatorCat o2) {
if (o1.weight < o2.weight) {
return -1;
} else if (o1.weight > o2.weight) {
return 1;
} else {
return 0;
}
}
}
编写测试类
public class SorterComparatorMain {
public static void main(String[] args) {
SorterComparatorCat[] cats = {new SorterComparatorCat(2, 4), new SorterComparatorCat(1, 2),
new SorterComparatorCat(5, 3)};
SorterComparatorUtil<SorterComparatorCat> sorterComparatorUtil = new SorterComparatorUtil();
CatComparatorByWeight catComparatorByWeight = new CatComparatorByWeight();
sorterComparatorUtil.sort(cats, catComparatorByWeight);
System.out.println("catComparatorByWeight = " + Arrays.toString(cats));
sorterComparatorUtil.sort(cats, new CatComparatorByHeight());
//使用lambda表达式编写策略部创建策略类
sorterComparatorUtil.sort(cats, ((o1, o2) -> {
if (o1.height < o2.height) {
return -1;
} else if (o1.height > o2.height) {
return 1;
} else {
return 0;
}
}));
System.out.println("CatComparatorByHeight = " + Arrays.toString(cats));
}
}
lambda简写少编写策略类
public class SorterComparatorMain {
public static void main(String[] args) {
SorterComparatorCat[] cats = {new SorterComparatorCat(2, 4), new SorterComparatorCat(1, 2),
new SorterComparatorCat(5, 3)};
SorterComparatorUtil<SorterComparatorCat> sorterComparatorUtil = new SorterComparatorUtil();
//使用lambda表达式编写策略部创建策略类
sorterComparatorUtil.sort(cats, ((o1, o2) -> {
if (o1.height < o2.height) {
return -1;
} else if (o1.height > o2.height) {
return 1;
} else {
return 0;
}
}));
System.out.println("sorts = " + Arrays.toString(cats));
}
}
评论区