diff --git a/src/singleton/Singleton6.java b/src/singleton/Singleton6.java index c864a53..4dfd85e 100644 --- a/src/singleton/Singleton6.java +++ b/src/singleton/Singleton6.java @@ -5,7 +5,7 @@ package singleton; * 虽然达到了按需初始化的目的, 同步代码块 * */ public class Singleton6 { - private static Singleton6 INSTANCE; + private static volatile Singleton6 INSTANCE; private Singleton6() { } diff --git a/src/strategy/Cat.java b/src/strategy/Cat.java new file mode 100644 index 0000000..b61b379 --- /dev/null +++ b/src/strategy/Cat.java @@ -0,0 +1,26 @@ +package strategy; + +public class Cat implements Comparable{ + private int weight; + private int height; + + public Cat(int weight, int height) { + this.weight = weight; + this.height = height; + } + + @Override + public String toString() { + return "Cat{" + + "weight=" + weight + + ", height=" + height + + '}'; + } + + @Override + public int compareTo(Cat c) { + if(this.weight < c.weight) return -1; + else if(this.weight > c.weight) return 1; + else return 0; + } +} diff --git a/src/strategy/CatHeightComparator.java b/src/strategy/CatHeightComparator.java new file mode 100644 index 0000000..dd2cf5e --- /dev/null +++ b/src/strategy/CatHeightComparator.java @@ -0,0 +1,8 @@ +package strategy; + +public class CatHeightComparator implements Comparator{ + @Override + public int compareTo(Cat t1, Cat t2) { + return 0; + } +} diff --git a/src/strategy/CatWeightComparator.java b/src/strategy/CatWeightComparator.java new file mode 100644 index 0000000..2095afe --- /dev/null +++ b/src/strategy/CatWeightComparator.java @@ -0,0 +1,8 @@ +package strategy; + +public class CatWeightComparator implements Comparator{ + @Override + public int compareTo(Cat t1, Cat t2) { + return 0; + } +} diff --git a/src/strategy/Comparator.java b/src/strategy/Comparator.java new file mode 100644 index 0000000..ef12012 --- /dev/null +++ b/src/strategy/Comparator.java @@ -0,0 +1,5 @@ +package strategy; + +public interface Comparator { + int compareTo(T t1, T t2); +} diff --git a/src/strategy/Dog.java b/src/strategy/Dog.java new file mode 100644 index 0000000..e0d10e8 --- /dev/null +++ b/src/strategy/Dog.java @@ -0,0 +1,23 @@ +package strategy; + +public class Dog implements Comparable{ + int food; + + public Dog(int food) { + this.food = food; + } + + @Override + public String toString() { + return "Dog{" + + "food=" + food + + '}'; + } + + @Override + public int compareTo(Dog d) { + if(this.food < d.food) return -1; + else if(this.food > d.food) return 1; + else return 0; + } +} diff --git a/src/strategy/DogComparator.java b/src/strategy/DogComparator.java new file mode 100644 index 0000000..8df7845 --- /dev/null +++ b/src/strategy/DogComparator.java @@ -0,0 +1,10 @@ +package strategy; + +public class DogComparator implements Comparator{ + @Override + public int compareTo(Dog d1, Dog d2) { + if(d1.food < d2.food) return -1; + else if(d1.food > d2.food) return 1; + else return 0; + } +} diff --git a/src/strategy/Sorter.java b/src/strategy/Sorter.java new file mode 100644 index 0000000..3fc1a05 --- /dev/null +++ b/src/strategy/Sorter.java @@ -0,0 +1,29 @@ +package strategy; + +import java.util.Arrays; + +public class Sorter { + public void sort(T[] arr, Comparator comparator){ + for (int i = 0; i < arr.length - 1; i++) { + int minPos = i; + for (int j = i+1; j < arr.length; j++) { + minPos = comparator.compareTo(arr[j], arr[minPos]) == -1 ? j : minPos; + } + swap(arr, i, minPos); + } + } + + private void swap(T[] arr, int i, int minPos) { + T temp = arr[i]; + arr[i] = arr[minPos]; + arr[minPos] = temp; + } + + public static void main(String[] args) { + Dog[] dogs = new Dog[]{new Dog(5), new Dog(1), new Dog(6)}; + Cat[] cats = new Cat[]{new Cat(6,2), new Cat(3,4), new Cat(5,5)}; + Sorter sorter = new Sorter<>(); + sorter.sort(dogs, new DogComparator()); + System.out.println(Arrays.toString(dogs)); + } +}