summaryrefslogtreecommitdiff
path: root/Year_2/SE/design_patterns
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2023-01-07 18:51:03 +0100
committerSanto Cariotti <santo@dcariotti.me>2023-01-07 18:51:03 +0100
commit9240342b366db9999f11659a1f1c396ba418ad78 (patch)
treef34e9259cbb6f8d4bcdea234e4b2c927950411cc /Year_2/SE/design_patterns
parentba36beaec6d37d26b075d96e58aad73151d6d39e (diff)
Adds
Diffstat (limited to 'Year_2/SE/design_patterns')
-rw-r--r--Year_2/SE/design_patterns/singleton/Logs.java10
-rw-r--r--Year_2/SE/design_patterns/singleton/Main.java12
2 files changed, 22 insertions, 0 deletions
diff --git a/Year_2/SE/design_patterns/singleton/Logs.java b/Year_2/SE/design_patterns/singleton/Logs.java
new file mode 100644
index 0000000..85deef9
--- /dev/null
+++ b/Year_2/SE/design_patterns/singleton/Logs.java
@@ -0,0 +1,10 @@
+public class Logs {
+ private static Logs log;
+
+ private Logs(){}
+
+ public static Logs getInstanceLogs() {
+ if(log == null) log = new Logs();
+ return log;
+ }
+}
diff --git a/Year_2/SE/design_patterns/singleton/Main.java b/Year_2/SE/design_patterns/singleton/Main.java
new file mode 100644
index 0000000..5d05cea
--- /dev/null
+++ b/Year_2/SE/design_patterns/singleton/Main.java
@@ -0,0 +1,12 @@
+public class Main {
+ public static void main(String[] args) {
+ Logs l = Logs.getInstanceLogs();
+ Logs l2 = Logs.getInstanceLogs();
+
+ // Prints "true"
+ System.out.println(l == l2);
+
+ // Error, because the constructor is private
+ // Logs l3 = new Logs();
+ }
+}