File tree Expand file tree Collapse file tree
main/java/com/javalearning/singleton
test/java/com/javalearning Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # kdiff3 ignore
2+ * .orig
3+
4+ # maven ignore
5+ target /
6+
7+ # eclipse ignore
8+ .settings /
9+ .project
10+ .classpath
11+
12+ # idea ignore
13+ .idea /
14+ * .ipr
15+ * .iml
16+ * .iws
17+
18+ # temp ignore
19+ * .log
20+ * .cache
21+ * .diff
22+ * .patch
23+ * .tmp
24+
25+ # system ignore
26+ .DS_Store
27+ Thumbs.db
28+
29+ # package ignore (optional)
30+ # *.jar
31+ # *.war
32+ # *.zip
33+ # *.tar
34+ # *.tar.gz
Original file line number Diff line number Diff line change 1+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3+ <modelVersion >4.0.0</modelVersion >
4+
5+ <parent >
6+ <groupId >com.javalearning</groupId >
7+ <artifactId >javalearning</artifactId >
8+ <version >1.0-SNAPSHOT</version >
9+ </parent >
10+
11+ <artifactId >designPatterns</artifactId >
12+ <packaging >jar</packaging >
13+
14+ <name >designPatterns</name >
15+ <url >http://maven.apache.org</url >
16+
17+ <properties >
18+ <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
19+ </properties >
20+
21+ <dependencies >
22+ <dependency >
23+ <groupId >junit</groupId >
24+ <artifactId >junit</artifactId >
25+ </dependency >
26+ </dependencies >
27+ </project >
Original file line number Diff line number Diff line change 1+ package com .javalearning .singleton ;
2+
3+ /**
4+ * Created by renqun.yuan on 2015/10/14.
5+ */
6+ public class NotRightWorkSingleton {
7+ private static NotRightWorkSingleton instance = null ;
8+
9+ private NotRightWorkSingleton () {
10+ }
11+
12+ /**
13+ * In multiThread, it can't work
14+ *
15+ * @return
16+ */
17+ public static NotRightWorkSingleton getInstance () {
18+ if (instance == null ) {
19+ instance = new NotRightWorkSingleton ();
20+ }
21+ return instance ;
22+ }
23+
24+ /**
25+ * 'new' may not a atomic operation,
26+ * instruct can be reorder
27+ * volatile used can be will work, see: SingletonLazyMode.java
28+ *
29+ * @return
30+ */
31+ public static NotRightWorkSingleton getInstance1 () {
32+ if (instance == null ) {
33+ synchronized (SingletonLazyMode .class ) {
34+ if (instance == null ) {
35+ instance = new NotRightWorkSingleton ();
36+ }
37+ }
38+ }
39+ return instance ;
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ package com .javalearning .singleton ;
2+
3+ /**
4+ * Created by renqun.yuan on 2015/10/14.
5+ */
6+ public class SingletonEagerMode {
7+ // when the class is loaded, the instance is initialized
8+ private static final SingletonEagerMode instance = new SingletonEagerMode ();
9+
10+ private SingletonEagerMode () {
11+ }
12+
13+ public static SingletonEagerMode getInstance () {
14+ return instance ;
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package com .javalearning .singleton ;
2+
3+ /**
4+ * Created by renqun.yuan on 2015/10/14.
5+ */
6+ public class SingletonEagerMode1 {
7+ // when the getInstance method is invoked, the instance is initialized
8+ private static class SingletonInstanceHolder {
9+ private static final SingletonEagerMode1 INSTANCE = new SingletonEagerMode1 ();
10+ }
11+
12+ private SingletonEagerMode1 () {
13+ }
14+
15+ public static SingletonEagerMode1 getInstance () {
16+ return SingletonInstanceHolder .INSTANCE ;
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package com .javalearning .singleton ;
2+
3+ /**
4+ * Created by renqun.yuan on 2015/10/14.
5+ */
6+ public enum SingletonEnumMode {
7+ INSTANCE ;
8+ }
Original file line number Diff line number Diff line change 1+ package com .javalearning .singleton ;
2+
3+ /**
4+ * Created by renqun.yuan on 2015/10/14.
5+ */
6+ public class SingletonLazyMode {
7+ private volatile static SingletonLazyMode instance = null ;
8+
9+ private SingletonLazyMode () {
10+ }
11+
12+ // double-check
13+ public static SingletonLazyMode getInstance () {
14+ if (instance == null ) {
15+ synchronized (SingletonLazyMode .class ) {
16+ if (instance == null ) {
17+ instance = new SingletonLazyMode ();
18+ }
19+ }
20+ }
21+ return instance ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Created by renqun.yuan on 2015/10/14.
3+ */
4+ package com .javalearning .singleton ;
5+
6+ /**
7+ * Reference:
8+ * http://www.programcreek.com/2011/07/java-design-pattern-singleton/
9+ * http://coolshell.cn/articles/265.html
10+ * <p/>
11+ * Here is a simple use case. A country can have only one president
12+ * <p/>
13+ * Singleton Pattern
14+ * it should satisfy:
15+ * 1. private constructor - no other class can instantiate a new object.
16+ * 2.private reference - no external modification.
17+ * 3.public static method is the only place that can get an object.
18+ * <p/>
19+ * There are two modes:
20+ * 1. Eager Mode
21+ * 2. Lazy Mode
22+ * <p/>
23+ * Runtime.getRuntime() is singleton in Java
24+ * see: java.lang.Runtime
25+ **/
26+
Original file line number Diff line number Diff line change 1+ package com .javalearning ;
2+
3+ import junit .framework .Test ;
4+ import junit .framework .TestCase ;
5+ import junit .framework .TestSuite ;
6+
7+ /**
8+ * Unit test for simple App.
9+ */
10+ public class AppTest
11+ extends TestCase {
12+ /**
13+ * Create the test case
14+ *
15+ * @param testName name of the test case
16+ */
17+ public AppTest (String testName ) {
18+ super (testName );
19+ }
20+
21+ /**
22+ * @return the suite of tests being tested
23+ */
24+ public static Test suite () {
25+ return new TestSuite (AppTest .class );
26+ }
27+
28+ /**
29+ * Rigourous Test :-)
30+ */
31+ public void testApp () {
32+ assertTrue (true );
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3+ <modelVersion >4.0.0</modelVersion >
4+
5+ <groupId >com.javalearning</groupId >
6+ <artifactId >javalearning</artifactId >
7+ <version >1.0-SNAPSHOT</version >
8+ <packaging >pom</packaging >
9+
10+ <name >learningJava</name >
11+ <url >http://maven.apache.org</url >
12+
13+ <modules >
14+ <module >designPatterns</module >
15+ </modules >
16+
17+ <properties >
18+ <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
19+ <junit .version>3.8.1</junit .version>
20+ </properties >
21+
22+ <dependencyManagement >
23+ <dependencies >
24+ <dependency >
25+ <groupId >junit</groupId >
26+ <artifactId >junit</artifactId >
27+ <version >${junit.version} </version >
28+ <scope >test</scope >
29+ </dependency >
30+ </dependencies >
31+ </dependencyManagement >
32+
33+ <build >
34+ <finalName >learningJava</finalName >
35+ <plugins >
36+ <plugin >
37+ <artifactId >maven-compiler-plugin</artifactId >
38+ <configuration >
39+ <source >1.7</source >
40+ <target >1.7</target >
41+ </configuration >
42+ </plugin >
43+ </plugins >
44+ </build >
45+ </project >
You can’t perform that action at this time.
0 commit comments