Bladeren bron

Initial commit

code_hero 2 weken geleden
commit
525ead5147

+ 38 - 0
.gitignore

@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 7 - 0
.idea/MarsCodeWorkspaceAppSettings.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="com.codeverse.userSettings.MarscodeWorkspaceAppSettingsState">
+    <option name="ckgOperationStatus" value="SUCCESS" />
+    <option name="progress" value="1.0" />
+  </component>
+</project>

+ 7 - 0
.idea/encodings.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding">
+    <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
+    <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
+  </component>
+</project>

+ 25 - 0
.idea/misc.xml

@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ExternalStorageConfigurationManager" enabled="true" />
+  <component name="MavenProjectsManager">
+    <option name="originalFiles">
+      <list>
+        <option value="$PROJECT_DIR$/pom.xml" />
+      </list>
+    </option>
+  </component>
+  <component name="ProjectInspectionProfilesVisibleTreeState">
+    <entry key="Project Default">
+      <profile-state>
+        <selected-state>
+          <State>
+            <id>用户定义</id>
+          </State>
+        </selected-state>
+      </profile-state>
+    </entry>
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="corretto-17 (6)" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>

+ 4 - 0
.idea/vcs.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings" defaultProject="true" />
+</project>

+ 23 - 0
pom.xml

@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.mszlu.rpc</groupId>
+    <artifactId>desien-pattern</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <version>1.5.18</version>
+        </dependency>
+    </dependencies>
+</project>

+ 28 - 0
src/main/java/singletonPattern/SingletonFactory.java

@@ -0,0 +1,28 @@
+package singletonPattern;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Arrays;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+
+public  class SingletonFactory {
+    private static ConcurrentHashMap<Class, Object>map = new ConcurrentHashMap<>();
+
+    public synchronized static <T> T getInstance(Class<T> clazz,Object ... args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
+        //判断map中是否已经有
+        if(map.containsKey(clazz)) {
+            return (T) map.get(clazz);
+        }
+        //构造函数的参数
+        Class[] classes=null;
+        if(args!=null&&args.length>0){
+            classes = Arrays.stream(args).map(Object::getClass).toArray(Class[]::new);
+        }
+        Constructor<T> constructor = clazz.getConstructor(classes);
+        T o =  constructor.newInstance(args);
+        map.put(clazz,o);
+        return o;
+    }
+
+}

+ 27 - 0
src/main/java/singletonPattern/User.java

@@ -0,0 +1,27 @@
+package singletonPattern;
+
+public class User {
+    String name;
+    Integer age;
+    public User() {}
+    public User(String name, Integer age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    public Integer getAge() {
+        return age;
+    }
+
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

+ 33 - 0
src/main/java/singletonPattern/main.java

@@ -0,0 +1,33 @@
+package singletonPattern;
+
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class main {
+    private static final Logger log = LoggerFactory.getLogger(main.class);
+    private static User instance = null;
+    static class MyThread extends Thread {
+        String name;
+        Integer age;
+        public MyThread(String name, Integer age) {
+            this.name = name;
+            this.age = age;
+        }
+        @Override
+        public void run() {
+            try {
+                instance = SingletonFactory.getInstance(User.class,name,age);
+                log.info(instance.toString());
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
+    public static void main(String[] args) {
+        for (int i = 0; i < 1000; i++) {
+            MyThread myThread = new MyThread("zky" + i, i);
+            myThread.start();
+        }
+    }
+}