forked from fuzhengwei/itstack-demo-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01abf0c
commit 30ef712
Showing
12 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http:https://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>itstack-demo-design</artifactId> | ||
<groupId>org.itstack</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>itstack-demo-design-5-00</artifactId> | ||
|
||
|
||
</project> |
10 changes: 10 additions & 0 deletions
10
itstack-demo-design-5-00/src/main/java/org/itstack/demo/design/Singleton_00.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.itstack.demo.design; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class Singleton_00 { | ||
|
||
public static Map<String,String> cache = new ConcurrentHashMap<String, String>(); | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
itstack-demo-design-5-00/src/main/java/org/itstack/demo/design/Singleton_01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.itstack.demo.design; | ||
|
||
public class Singleton_01 { | ||
|
||
private static Singleton_01 instance; | ||
|
||
private Singleton_01() { | ||
} | ||
|
||
public static Singleton_01 getInstance(){ | ||
if (null != instance) return instance; | ||
return new Singleton_01(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
itstack-demo-design-5-00/src/main/java/org/itstack/demo/design/Singleton_02.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.itstack.demo.design; | ||
|
||
public class Singleton_02 { | ||
|
||
private static Singleton_02 instance; | ||
|
||
private Singleton_02() { | ||
} | ||
|
||
public static synchronized Singleton_02 getInstance(){ | ||
if (null != instance) return instance; | ||
return new Singleton_02(); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
itstack-demo-design-5-00/src/main/java/org/itstack/demo/design/Singleton_03.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.itstack.demo.design; | ||
|
||
public class Singleton_03 { | ||
|
||
private static Singleton_03 instance = new Singleton_03(); | ||
|
||
private Singleton_03() { | ||
} | ||
|
||
public static Singleton_03 getInstance() { | ||
return instance; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
itstack-demo-design-5-00/src/main/java/org/itstack/demo/design/Singleton_04.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.itstack.demo.design; | ||
|
||
public class Singleton_04 { | ||
|
||
private static class SingletonHolder { | ||
private static Singleton_04 instance = new Singleton_04(); | ||
} | ||
|
||
private Singleton_04() { | ||
} | ||
|
||
public static Singleton_04 getInstance() { | ||
return SingletonHolder.instance; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
itstack-demo-design-5-00/src/main/java/org/itstack/demo/design/Singleton_05.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.itstack.demo.design; | ||
|
||
public class Singleton_05 { | ||
|
||
private static Singleton_05 instance; | ||
|
||
private Singleton_05() { | ||
} | ||
|
||
public static Singleton_05 getInstance(){ | ||
if(null != instance) return instance; | ||
synchronized (Singleton_05.class){ | ||
if (null == instance){ | ||
instance = new Singleton_05(); | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
itstack-demo-design-5-00/src/main/java/org/itstack/demo/design/Singleton_06.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.itstack.demo.design; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public class Singleton_06 { | ||
|
||
private static final AtomicReference<Singleton_06> INSTANCE = new AtomicReference<Singleton_06>(); | ||
|
||
private static Singleton_06 instance; | ||
|
||
private Singleton_06() { | ||
} | ||
|
||
public static final Singleton_06 getInstance() { | ||
for (; ; ) { | ||
Singleton_06 instance = INSTANCE.get(); | ||
if (null != instance) return instance; | ||
INSTANCE.compareAndSet(null, new Singleton_06()); | ||
return INSTANCE.get(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(Singleton_06.getInstance()); // org.itstack.demo.design.Singleton_06@2b193f2d | ||
System.out.println(Singleton_06.getInstance()); // org.itstack.demo.design.Singleton_06@2b193f2d | ||
} | ||
|
||
|
||
} |
10 changes: 10 additions & 0 deletions
10
itstack-demo-design-5-00/src/main/java/org/itstack/demo/design/Singleton_07.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.itstack.demo.design; | ||
|
||
public enum Singleton_07 { | ||
|
||
INSTANCE; | ||
public void test(){ | ||
System.out.println("hi~"); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
itstack-demo-design-5-00/src/test/java/org/itstack/demo/design/test/ApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.itstack.demo.design.test; | ||
|
||
import org.itstack.demo.design.Singleton_00; | ||
import org.itstack.demo.design.Singleton_07; | ||
import org.junit.Test; | ||
|
||
public class ApiTest { | ||
|
||
@Test | ||
public void test() { | ||
Singleton_07.INSTANCE.test(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters