parent
a32aea265e
commit
592e2e2bd4
@ -0,0 +1,23 @@
|
||||
package com.demo.tank.course5.singleton;
|
||||
|
||||
/*
|
||||
* 同1
|
||||
* */
|
||||
public class Singleton2 {
|
||||
private static final Singleton2 INSTANCE;
|
||||
static {
|
||||
INSTANCE = new Singleton2();
|
||||
}
|
||||
|
||||
private Singleton2() {
|
||||
}
|
||||
|
||||
public static Singleton2 getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Singleton2 singleton = Singleton2.getInstance();
|
||||
System.out.println(singleton);
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.demo.tank.course5.singleton;
|
||||
|
||||
/*
|
||||
* 懒汉式 lazy loading
|
||||
* 虽然达到了按需初始化的目的, 同步代码块
|
||||
* */
|
||||
public class Singleton6 {
|
||||
private static volatile Singleton6 INSTANCE;
|
||||
|
||||
private Singleton6() {
|
||||
}
|
||||
|
||||
public static synchronized Singleton6 getInstance() {
|
||||
synchronized (Singleton6.class){
|
||||
//双重检查
|
||||
if(INSTANCE == null) {
|
||||
INSTANCE = new Singleton6();
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Singleton6 singleton = Singleton6.getInstance();
|
||||
for (int i=0; i< 100; i++){
|
||||
new Thread(() -> System.out.println(singleton.hashCode())).start();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.demo.tank.course5.strategy;
|
||||
|
||||
public class Cat implements Comparable<Cat>{
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.demo.tank.course5.strategy;
|
||||
|
||||
public class CatHeightComparator implements Comparator<Cat>{
|
||||
@Override
|
||||
public int compareTo(Cat t1, Cat t2) {
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.demo.tank.course5.strategy;
|
||||
|
||||
public class CatWeightComparator implements Comparator<Cat>{
|
||||
@Override
|
||||
public int compareTo(Cat t1, Cat t2) {
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.demo.tank.course5.strategy;
|
||||
|
||||
public interface Comparator<T> {
|
||||
int compareTo(T t1, T t2);
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.demo.tank.course5.strategy;
|
||||
|
||||
public class Dog implements Comparable<Dog>{
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.demo.tank.course5.strategy;
|
||||
|
||||
public class DogComparator implements Comparator<Dog>{
|
||||
@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;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.demo.tank.course5.strategy;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Sorter<T> {
|
||||
public void sort(T[] arr, Comparator<T> 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<Dog> sorter = new Sorter<>();
|
||||
sorter.sort(dogs, new DogComparator());
|
||||
System.out.println(Arrays.toString(dogs));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue