瀏覽代碼

Add Singleton design pattern example in creational.singleton package

檀烈 8 小時之前
父節點
當前提交
54fa376018

+ 1 - 0
.gitignore

@@ -13,3 +13,4 @@
 hs_err_pid*
 
 .idea/
+target/

+ 11 - 0
src/main/java/cn/qinys/learn/PatternApplication.java

@@ -1,6 +1,7 @@
 package cn.qinys.learn;
 
 
+import cn.qinys.learn.creational.singleton.Singleton;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@@ -9,5 +10,15 @@ public class PatternApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(PatternApplication.class, args);
+
+        // Demonstrate Singleton pattern
+        Singleton singleton1 = Singleton.getInstance();
+        Singleton singleton2 = Singleton.getInstance();
+
+        // Check if both references point to the same instance
+        System.out.println("Are both instances the same? " + (singleton1 == singleton2));
+
+        // Call a method on the singleton
+        singleton1.showMessage();
     }
 }

+ 33 - 0
src/main/java/cn/qinys/learn/creational/singleton/Singleton.java

@@ -0,0 +1,33 @@
+package cn.qinys.learn.creational.singleton;
+
+/**
+ * Singleton Design Pattern Example
+ * Ensures only one instance of the class exists throughout the application.
+ */
+public class Singleton {
+
+    // Private static instance variable
+    private static volatile Singleton instance;
+
+    // Private constructor to prevent instantiation
+    private Singleton() {
+        // Initialization code if needed
+    }
+
+    // Public static method to get the instance
+    public static Singleton getInstance() {
+        if (instance == null) {
+            synchronized (Singleton.class) {
+                if (instance == null) {
+                    instance = new Singleton();
+                }
+            }
+        }
+        return instance;
+    }
+
+    // Example method
+    public void showMessage() {
+        System.out.println("Hello from Singleton instance!");
+    }
+}

+ 28 - 0
src/main/java/cn/qinys/learn/creational/singleton/Singleton2.java

@@ -0,0 +1,28 @@
+package cn.qinys.learn.creational.singleton;
+
+/**
+ * Singleton Design Pattern Example
+ * Ensures only one instance of the class exists throughout the application.
+ */
+public class Singleton2 {
+
+    // Public static method to get the instance
+    // Private static instance variable
+
+    private static final Singleton2 instance = new Singleton2();
+
+
+    public static Singleton2 getInstance() {
+        return instance;
+    }
+
+    // Private constructor to prevent instantiation
+    private Singleton2() {
+        // Initialization code if needed
+    }
+
+    // Example method
+    public void showMessage() {
+        System.out.println("Hello from Singleton2 instance!");
+    }
+}

+ 21 - 0
src/test/java/cn/qinys/learn/creational/singleton/SingletonTest.java

@@ -0,0 +1,21 @@
+package cn.qinys.learn.creational.singleton;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class SingletonTest {
+
+
+    @Test
+    void showMessage() {
+        Singleton instance = Singleton.getInstance();
+        instance.showMessage();
+    }
+
+    @Test
+    void showMessage2() {
+        Singleton2 instance = Singleton2.getInstance();
+        instance.showMessage();
+    }
+}