单例和策略设计模式

master
kn5886348135 3 years ago
parent 27b0107b7a
commit 70f9cb6801

@ -0,0 +1,4 @@
package com.example.tankbattle.singleton;
public class Singleton {
}

@ -0,0 +1,29 @@
package com.example.tankbattle.singleton;
/**
* 饿
* JVM线
* 使
* :
*
*/
public class Singleton01 {
private static Singleton01 INSTANCE = new Singleton01();
private Singleton01() {
}
public static Singleton01 getInstance() {
return INSTANCE;
}
public void method(){
System.out.println("method");
}
public static void main(String[] args) {
Singleton01 singleton01 = new Singleton01();
Singleton01 singleton02 = new Singleton01();
System.out.println(singleton01 == singleton02);
}
}

@ -0,0 +1,29 @@
package com.example.tankbattle.singleton;
/**
* Singleton01static
*/
public class Singleton02 {
private static Singleton02 INSTANCE;
static {
INSTANCE = new Singleton02();
}
private Singleton02() {
}
public static Singleton02 getInstance() {
return INSTANCE;
}
public void method(){
System.out.println("method");
}
public static void main(String[] args) {
Singleton02 singleton01 = new Singleton02();
Singleton02 singleton02 = new Singleton02();
System.out.println(singleton01 == singleton02);
}
}

@ -0,0 +1,35 @@
package com.example.tankbattle.singleton;
/**
* lazy loading
*
* 线
*/
public class Singleton03 {
private static Singleton03 INSTANCE;
private Singleton03() {
}
public static Singleton03 getInstance() {
if (INSTANCE == null) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton03();
}
return INSTANCE;
}
public void method(){
System.out.println("method");
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(() -> System.out.println(Singleton03.getInstance())).start();
}
}
}

@ -0,0 +1,36 @@
package com.example.tankbattle.singleton;
/**
* lazy loading
*
* 线
* synchronized
*/
public class Singleton04 {
private static Singleton04 INSTANCE;
private Singleton04() {
}
public static synchronized Singleton04 getInstance() {
if (INSTANCE == null) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton04();
}
return INSTANCE;
}
public void method(){
System.out.println("method");
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(() -> System.out.println(Singleton04.getInstance())).start();
}
}
}

@ -0,0 +1,39 @@
package com.example.tankbattle.singleton;
/**
* lazy loading
*
* 线
* synchronized线
*/
public class Singleton05 {
private static Singleton05 INSTANCE;
private Singleton05() {
}
public static Singleton05 getInstance() {
if (INSTANCE == null) {
// 妄图通过减小同步代码块的方式提高效率,然而不可行。
synchronized (Singleton05.class) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton05();
}
}
return INSTANCE;
}
public void method(){
System.out.println("method");
}
public static void main(String[] args) {
for (int i = 0; i < 10000; i++) {
new Thread(() -> System.out.println(Singleton05.getInstance())).start();
}
}
}

@ -0,0 +1,48 @@
package com.example.tankbattle.singleton;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* INSTANCEvolatile
*/
public class Singleton06 {
private static Singleton06 INSTANCE;
private Singleton06() {
}
public static synchronized Singleton06 getInstance() {
if (INSTANCE == null) {
// 双重检查
synchronized (Singleton06.class) {
if (INSTANCE == null) {
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton06();
}
}
}
return INSTANCE;
}
public void method(){
System.out.println("method");
}
public static void main(String[] args) {
Map<Integer, String> map = new ConcurrentHashMap<>(50000);
for (int i = 0; i < 50000; i++) {
new Thread(() -> map.put(Singleton06.getInstance().hashCode(), "11111")).start();
}
Set<Integer> keys = map.keySet();
for (Integer key : keys) {
System.out.println(key);
}
}
}

@ -0,0 +1,29 @@
package com.example.tankbattle.singleton;
/**
*
* JVM
*
*/
public class Singleton07 {
private Singleton07() {
}
private static class Singleton07Holder {
private static Singleton07 INSTANCE = new Singleton07();
}
public static Singleton07 getInstance() {
return Singleton07Holder.INSTANCE;
}
public void method(){
System.out.println("method");
}
public static void main(String[] args) {
for (int i = 0; i < 10000; i++) {
new Thread(() -> System.out.println(Singleton07.getInstance())).start();
}
}
}

@ -0,0 +1,16 @@
package com.example.tankbattle.singleton;
/**
* 线
*/
public enum Singleton08 {
INSTANCE;
public void method(){
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(() -> System.out.println(Singleton08.INSTANCE.hashCode())).start();
}
}
}

@ -0,0 +1,52 @@
package com.example.tankbattle.strategy;
public class Cat implements CustomComparable<Cat> {
private String id;
private String name;
private int weight;
private int height;
public Cat(int weight, int height) {
this.weight = weight;
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int compareToa(Cat cat) {
if (this.weight < cat.weight) {
return -1;
} else if (this.weight > cat.weight) {
return 1;
} else {
return 0;
}
}
@Override
public int compareTo(Cat cat) {
if (this.weight < cat.weight) {
return -1;
} else if (this.weight > cat.weight) {
return 1;
} else {
return 0;
}
}
}

@ -0,0 +1,5 @@
package com.example.tankbattle.strategy;
public interface CustomComparable<T> {
int compareTo(T o);
}

@ -0,0 +1,5 @@
package com.example.tankbattle.strategy;
public interface CustomComparator<T> {
int compare(T o1, T o2);
}

@ -0,0 +1,36 @@
package com.example.tankbattle.strategy;
public class Dog implements CustomComparable<Dog> {
private int food;
public int getFood() {
return food;
}
public void setFood(int food) {
this.food = food;
}
public Dog(int food) {
this.food = food;
}
@Override
public int compareTo(Dog dog) {
if (this.food < dog.food) {
return -1;
} else if (this.food < dog.food) {
return 1;
} else {
return 0;
}
}
@Override
public String toString() {
return "Dog{" +
"food=" + food +
'}';
}
}

@ -0,0 +1,15 @@
package com.example.tankbattle.strategy;
public class DogComparator implements CustomComparator<Dog> {
@Override
public int compare(Dog dog1, Dog dog2) {
if (dog1.getFood() < dog2.getFood()) {
return -1;
} else if (dog1.getFood() > dog2.getFood()) {
return 1;
} else {
return 0;
}
}
}

@ -0,0 +1,27 @@
package com.example.tankbattle.strategy;
import java.util.Arrays;
public class Main {
private static int[] arr = {9, 6, 7, 7, 6, 5, 5, 4, 4};
private static Sorter sorter = new Sorter();
public static void main(String[] args) {
Cat[] cats = {new Cat(1, 1), new Cat(3, 3), new Cat(5, 5)};
Dog[] dogs = {new Dog(5), new Dog(3), new Dog(1)};
sorter.sort(dogs, new CustomComparator<Dog>() {
@Override
public int compare(Dog dog1, Dog dog2) {
if (dog1.getFood() < dog2.getFood()) {
return -1;
} else if (dog1.getFood() > dog2.getFood()) {
return 1;
} else {
return 0;
}
}
});
System.out.println(Arrays.asList(dogs));
}
}

@ -0,0 +1,22 @@
package com.example.tankbattle.strategy;
public class Sorter<T> {
public void sort(T[] arr, CustomComparator<T> comparator) {
int length = arr.length;
if (arr == null || length < 2) {
return;
}
T tmp;
for (int i = length - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (comparator.compare(arr[j + 1], (arr[j])) < 0) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
}
Loading…
Cancel
Save