zhuyijun vor 2 Jahren
Commit
4473348007
60 geänderte Dateien mit 936 neuen und 0 gelöschten Zeilen
  1. 33 0
      .gitignore
  2. 8 0
      .idea/.gitignore
  3. 8 0
      .idea/compiler.xml
  4. 20 0
      .idea/jarRepositories.xml
  5. 19 0
      .idea/misc.xml
  6. 3 0
      .idea/sonarlint/issuestore/0/1/01f95235e084159bc461a677ecf5e834be0f6469
  7. 13 0
      .idea/sonarlint/issuestore/0/7/073414959b2cba568061c6170a81830d44091a42
  8. 2 0
      .idea/sonarlint/issuestore/1/c/1cb148ba0087a33e8bf02613d8b7c679b91fcd06
  9. 11 0
      .idea/sonarlint/issuestore/2/8/287f2e1f13dc10e6468834d9c189690ce18d7401
  10. 3 0
      .idea/sonarlint/issuestore/2/b/2bc4eff5039cddf9fee022a349fd17737a973e21
  11. 0 0
      .idea/sonarlint/issuestore/2/c/2ce023438f835f805ac4a1f962865f8747405a15
  12. 7 0
      .idea/sonarlint/issuestore/4/1/41bf42f211f3e74955ddbe790a3782c2138ea5e5
  13. 0 0
      .idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec
  14. 0 0
      .idea/sonarlint/issuestore/4/e/4ee28baa566c84b7148d31fd81fd340690b18c35
  15. 7 0
      .idea/sonarlint/issuestore/4/f/4fe8b705fa2a77020740ccba086b899a2f4e1396
  16. 3 0
      .idea/sonarlint/issuestore/5/3/538fa576cf057b796d7e79a9a755a0b83414ac78
  17. 2 0
      .idea/sonarlint/issuestore/5/5/55e78da8cef5a7f5f5f35b84ef0fffeffae25776
  18. 2 0
      .idea/sonarlint/issuestore/5/d/5dfc6bb5f771f251e413b26a2b5f818eeb42f5e3
  19. 3 0
      .idea/sonarlint/issuestore/7/7/7724da34f9b399daa66c91bb7cdef1740bcc5224
  20. 0 0
      .idea/sonarlint/issuestore/8/f/8f24cca5dad2bcc60cd2edd9f5b89b16a07bbf4f
  21. 11 0
      .idea/sonarlint/issuestore/9/1/911ee76b72050d7492f9d599612b9d7a503ddf61
  22. 0 0
      .idea/sonarlint/issuestore/9/6/96018b20f7c8fd3f289b14d1bdd86afc026ad845
  23. 2 0
      .idea/sonarlint/issuestore/a/2/a26362fa3f616a9cdc821117d710f7756288feb8
  24. 0 0
      .idea/sonarlint/issuestore/a/a/aa58d5a9b76b643349babf1bfe6d142bb431751c
  25. 0 0
      .idea/sonarlint/issuestore/b/a/ba03e3261c143c58db1241b701637785cfb88c69
  26. 3 0
      .idea/sonarlint/issuestore/b/e/beae95d5e17ec2f4f7b3046b49dafd01eb9a7e45
  27. 3 0
      .idea/sonarlint/issuestore/c/4/c488d16755d60311f19d1ec03c3ae7d4942c2b50
  28. 9 0
      .idea/sonarlint/issuestore/d/e/deed5314b092724d0a2b764a7c813cbad902cf9d
  29. 5 0
      .idea/sonarlint/issuestore/e/c/ecf607c149ca955271792aa8980e0112b3cb596d
  30. 2 0
      .idea/sonarlint/issuestore/f/0/f0002aa414906e03cfe735af5e205e3a37058275
  31. 2 0
      .idea/sonarlint/issuestore/f/1/f19d32621904e7c6e9bc23b597914e627016f477
  32. 53 0
      .idea/sonarlint/issuestore/index.pb
  33. 124 0
      .idea/uiDesigner.xml
  34. 6 0
      .idea/vcs.xml
  35. 20 0
      pom.xml
  36. 6 0
      src/main/java/cn/zyjblogs/design/singleton/Main.java
  37. 27 0
      src/main/java/cn/zyjblogs/design/singleton/Mgr01.java
  38. 30 0
      src/main/java/cn/zyjblogs/design/singleton/Mgr02.java
  39. 38 0
      src/main/java/cn/zyjblogs/design/singleton/Mgr03.java
  40. 38 0
      src/main/java/cn/zyjblogs/design/singleton/Mgr04.java
  41. 44 0
      src/main/java/cn/zyjblogs/design/singleton/Mgr05.java
  42. 49 0
      src/main/java/cn/zyjblogs/design/singleton/Mgr06.java
  43. 31 0
      src/main/java/cn/zyjblogs/design/singleton/Mgr07.java
  44. 19 0
      src/main/java/cn/zyjblogs/design/singleton/Mgr08.java
  45. 33 0
      src/main/java/cn/zyjblogs/design/strategy/demo1/Cat.java
  46. 5 0
      src/main/java/cn/zyjblogs/design/strategy/demo1/Comparable.java
  47. 13 0
      src/main/java/cn/zyjblogs/design/strategy/demo1/Comparator.java
  48. 23 0
      src/main/java/cn/zyjblogs/design/strategy/demo1/Dog.java
  49. 36 0
      src/main/java/cn/zyjblogs/design/strategy/demo1/Main.java
  50. 52 0
      src/main/java/cn/zyjblogs/design/strategy/demo1/Sorter.java
  51. 9 0
      src/main/java/cn/zyjblogs/design/strategy/demo2/Apple.java
  52. 8 0
      src/main/java/cn/zyjblogs/design/strategy/demo2/Banana.java
  53. 5 0
      src/main/java/cn/zyjblogs/design/strategy/demo2/Eat.java
  54. 13 0
      src/main/java/cn/zyjblogs/design/strategy/demo2/Person.java
  55. 9 0
      src/main/java/cn/zyjblogs/design/strategy/demo3/DefaultFireStrategy.java
  56. 5 0
      src/main/java/cn/zyjblogs/design/strategy/demo3/FireStrategy.java
  57. 15 0
      src/main/java/cn/zyjblogs/design/strategy/demo3/Gun.java
  58. 9 0
      src/main/java/cn/zyjblogs/design/strategy/demo3/GunSniperFireStrategy.java
  59. 26 0
      src/main/java/cn/zyjblogs/design/strategy/demo3/Person.java
  60. 9 0
      src/main/java/cn/zyjblogs/design/strategy/demo3/RifleFireStrategy.java

+ 33 - 0
.gitignore

@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/

+ 8 - 0
.idea/compiler.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="CompilerConfiguration">
+    <bytecodeTargetLevel>
+      <module name="singleton" target="8" />
+    </bytecodeTargetLevel>
+  </component>
+</project>

+ 20 - 0
.idea/jarRepositories.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="RemoteRepositoriesConfiguration">
+    <remote-repository>
+      <option name="id" value="central" />
+      <option name="name" value="Central Repository" />
+      <option name="url" value="http://maven.aliyun.com/nexus/content/repositories/central/" />
+    </remote-repository>
+    <remote-repository>
+      <option name="id" value="central" />
+      <option name="name" value="Maven Central repository" />
+      <option name="url" value="https://repo1.maven.org/maven2" />
+    </remote-repository>
+    <remote-repository>
+      <option name="id" value="jboss.community" />
+      <option name="name" value="JBoss Community repository" />
+      <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
+    </remote-repository>
+  </component>
+</project>

+ 19 - 0
.idea/misc.xml

@@ -0,0 +1,19 @@
+<?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>
+    <option name="ignoredFiles">
+      <set>
+        <option value="$PROJECT_DIR$/singleton/pom.xml" />
+      </set>
+    </option>
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>

+ 3 - 0
.idea/sonarlint/issuestore/0/1/01f95235e084159bc461a677ecf5e834be0f6469

@@ -0,0 +1,3 @@
+
+E
+java:S1611"/Remove the parentheses around the "a" parameter(­�À¶

+ 13 - 0
.idea/sonarlint/issuestore/0/7/073414959b2cba568061c6170a81830d44091a42

@@ -0,0 +1,13 @@
+
+n
+java:S3008	"SRename this field "INSTANCE" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(¿Ï¢ûýÿÿÿÿ
+y
+java:S3077	"WUse a thread-safe type; adding "volatile" is not enough to make this field thread-safe.(¿Ï¢ûýÿÿÿÿ8œ�ÏÿÌ/
+t
+java:S2142"^Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here.(ä ¹€
+Y
+java:S2276"CReplace the call to "Thread.sleep(...)" with a call to "wait(...)".(­²ÙÍ
+S	java:S106'"9Replace this use of System.out or System.err by a logger.(ùÙ·õüÿÿÿÿ
+B
+java:S1602,",Remove useless curly braces around statement(¥—ýð
+N	java:S106-"9Replace this use of System.out or System.err by a logger.(°á»í

+ 2 - 0
.idea/sonarlint/issuestore/1/c/1cb148ba0087a33e8bf02613d8b7c679b91fcd06

@@ -0,0 +1,2 @@
+
+S	java:S106"9Replace this use of System.out or System.err by a logger.(ùÙ·õüÿÿÿÿ

+ 11 - 0
.idea/sonarlint/issuestore/2/8/287f2e1f13dc10e6468834d9c189690ce18d7401

@@ -0,0 +1,11 @@
+
+n
+java:S3008"SRename this field "INSTANCE" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(¥ÖºÃøÿÿÿÿ
+t
+java:S2142"^Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here.(ä ¹€
+Y
+java:S2276"CReplace the call to "Thread.sleep(...)" with a call to "wait(...)".(­²ÙÍ
+S	java:S106""9Replace this use of System.out or System.err by a logger.(ùÙ·õüÿÿÿÿ
+B
+java:S1602'",Remove useless curly braces around statement(¥—ýð
+M	java:S106("9Replace this use of System.out or System.err by a logger.(¶ÏÀ}

+ 3 - 0
.idea/sonarlint/issuestore/2/b/2bc4eff5039cddf9fee022a349fd17737a973e21

@@ -0,0 +1,3 @@
+
+o
+java:S1210"YOverride "equals(Object obj)" to comply with the contract of the "compareTo(T o)" method.(‰Êý®

+ 0 - 0
.idea/sonarlint/issuestore/2/c/2ce023438f835f805ac4a1f962865f8747405a15


+ 7 - 0
.idea/sonarlint/issuestore/4/1/41bf42f211f3e74955ddbe790a3782c2138ea5e5

@@ -0,0 +1,7 @@
+
+n
+java:S3008"SRename this field "INSTANCE" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ùà“�øÿÿÿÿ
+S	java:S106"9Replace this use of System.out or System.err by a logger.(ùÙ·õüÿÿÿÿ
+B
+java:S1602",Remove useless curly braces around statement(¥—ýð
+S	java:S106"9Replace this use of System.out or System.err by a logger.(à†ðÏúÿÿÿÿ

+ 0 - 0
.idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec


+ 0 - 0
.idea/sonarlint/issuestore/4/e/4ee28baa566c84b7148d31fd81fd340690b18c35


+ 7 - 0
.idea/sonarlint/issuestore/4/f/4fe8b705fa2a77020740ccba086b899a2f4e1396

@@ -0,0 +1,7 @@
+
+Q
+java:S3740"/Provide the parametrized type for this generic.(ÁÓà¤þÿÿÿÿ8Û©Ì¥Í/
+Q	java:S125"<This block of commented-out lines of code should be removed.(ë´îÎ
+Q	java:S125"<This block of commented-out lines of code should be removed.(Ç�çž
+X	java:S125"<This block of commented-out lines of code should be removed.(ñªùÆ8¥�˜�Í/
+U	java:S106!"9Replace this use of System.out or System.err by a logger.(€«�Ÿ8ù€•�Í/

+ 3 - 0
.idea/sonarlint/issuestore/5/3/538fa576cf057b796d7e79a9a755a0b83414ac78

@@ -0,0 +1,3 @@
+
+v
+java:S1210"YOverride "equals(Object obj)" to comply with the contract of the "compareTo(T o)" method.(„±¿�8¤Ç²�Í/

+ 2 - 0
.idea/sonarlint/issuestore/5/5/55e78da8cef5a7f5f5f35b84ef0fffeffae25776

@@ -0,0 +1,2 @@
+
+M	java:S106"9Replace this use of System.out or System.err by a logger.(¿�š}

+ 2 - 0
.idea/sonarlint/issuestore/5/d/5dfc6bb5f771f251e413b26a2b5f818eeb42f5e3

@@ -0,0 +1,2 @@
+
+]	java:S125"<This block of commented-out lines of code should be removed.(¼ÆðÊøÿÿÿÿ8óŠø€Í/

+ 3 - 0
.idea/sonarlint/issuestore/7/7/7724da34f9b399daa66c91bb7cdef1740bcc5224

@@ -0,0 +1,3 @@
+
+ž
+java:S1186"€Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.(ǵí‘8¤òÆþÌ/

+ 0 - 0
.idea/sonarlint/issuestore/8/f/8f24cca5dad2bcc60cd2edd9f5b89b16a07bbf4f


+ 11 - 0
.idea/sonarlint/issuestore/9/1/911ee76b72050d7492f9d599612b9d7a503ddf61

@@ -0,0 +1,11 @@
+
+n
+java:S3008"SRename this field "INSTANCE" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(†œ–‘ûÿÿÿÿ
+t
+java:S2142"^Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here.(ä ¹€
+`
+java:S2276"CReplace the call to "Thread.sleep(...)" with a call to "wait(...)".(­²ÙÍ8÷õƒÿÌ/
+S	java:S106"9Replace this use of System.out or System.err by a logger.(ùÙ·õüÿÿÿÿ
+B
+java:S1602!",Remove useless curly braces around statement(¥—ýð
+S	java:S106""9Replace this use of System.out or System.err by a logger.(¿�«öøÿÿÿÿ

+ 0 - 0
.idea/sonarlint/issuestore/9/6/96018b20f7c8fd3f289b14d1bdd86afc026ad845


+ 2 - 0
.idea/sonarlint/issuestore/a/2/a26362fa3f616a9cdc821117d710f7756288feb8

@@ -0,0 +1,2 @@
+
+Z	java:S106"9Replace this use of System.out or System.err by a logger.(Ô‰ì‚þÿÿÿÿ8Ð…½�Í/

+ 0 - 0
.idea/sonarlint/issuestore/a/a/aa58d5a9b76b643349babf1bfe6d142bb431751c


+ 0 - 0
.idea/sonarlint/issuestore/b/a/ba03e3261c143c58db1241b701637785cfb88c69


+ 3 - 0
.idea/sonarlint/issuestore/b/e/beae95d5e17ec2f4f7b3046b49dafd01eb9a7e45

@@ -0,0 +1,3 @@
+
+S	java:S106"9Replace this use of System.out or System.err by a logger.(ùÙ·õüÿÿÿÿ
+U	java:S106"9Replace this use of System.out or System.err by a logger.(�äÏÏ8ì¡ÃþÌ/

+ 3 - 0
.idea/sonarlint/issuestore/c/4/c488d16755d60311f19d1ec03c3ae7d4942c2b50

@@ -0,0 +1,3 @@
+
+S	java:S106"9Replace this use of System.out or System.err by a logger.(ùÙ·õüÿÿÿÿ
+N	java:S106"9Replace this use of System.out or System.err by a logger.(�äÏÏ

+ 9 - 0
.idea/sonarlint/issuestore/d/e/deed5314b092724d0a2b764a7c813cbad902cf9d

@@ -0,0 +1,9 @@
+
+n
+java:S3008"SRename this field "INSTANCE" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ݶ÷™ûÿÿÿÿ
+{
+java:S2142"^Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here.(ä ¹€8´¥öþÌ/
+S	java:S106"9Replace this use of System.out or System.err by a logger.(ùÙ·õüÿÿÿÿ
+B
+java:S1602!",Remove useless curly braces around statement(¥—ýð
+S	java:S106""9Replace this use of System.out or System.err by a logger.(æ›ü¤þÿÿÿÿ

+ 5 - 0
.idea/sonarlint/issuestore/e/c/ecf607c149ca955271792aa8980e0112b3cb596d

@@ -0,0 +1,5 @@
+
+S	java:S106	"9Replace this use of System.out or System.err by a logger.(ùÙ·õüÿÿÿÿ
+B
+java:S1602",Remove useless curly braces around statement(¥—ýð
+N	java:S106"9Replace this use of System.out or System.err by a logger.(‘—äÛ

+ 2 - 0
.idea/sonarlint/issuestore/f/0/f0002aa414906e03cfe735af5e205e3a37058275

@@ -0,0 +1,2 @@
+
+U	java:S106"9Replace this use of System.out or System.err by a logger.(Ëî·¹8ÛÓº�Í/

+ 2 - 0
.idea/sonarlint/issuestore/f/1/f19d32621904e7c6e9bc23b597914e627016f477

@@ -0,0 +1,2 @@
+
+U	java:S106"9Replace this use of System.out or System.err by a logger.(”ݯ¬8“©Þ¥Í/

+ 53 - 0
.idea/sonarlint/issuestore/index.pb

@@ -0,0 +1,53 @@
+
+7
+pom.xml,4\4\442292b8a7efeabbe4cc176709b833b1792140ec
+d
+4src/main/java/cn/zyjblogs/design/singleton/Main.java,7\7\7724da34f9b399daa66c91bb7cdef1740bcc5224
+e
+5src/main/java/cn/zyjblogs/design/singleton/Mgr01.java,b\e\beae95d5e17ec2f4f7b3046b49dafd01eb9a7e45
+e
+5src/main/java/cn/zyjblogs/design/singleton/Mgr02.java,c\4\c488d16755d60311f19d1ec03c3ae7d4942c2b50
+e
+5src/main/java/cn/zyjblogs/design/singleton/Mgr03.java,d\e\deed5314b092724d0a2b764a7c813cbad902cf9d
+e
+5src/main/java/cn/zyjblogs/design/singleton/Mgr04.java,9\1\911ee76b72050d7492f9d599612b9d7a503ddf61
+e
+5src/main/java/cn/zyjblogs/design/singleton/Mgr05.java,2\8\287f2e1f13dc10e6468834d9c189690ce18d7401
+e
+5src/main/java/cn/zyjblogs/design/singleton/Mgr07.java,4\1\41bf42f211f3e74955ddbe790a3782c2138ea5e5
+e
+5src/main/java/cn/zyjblogs/design/singleton/Mgr08.java,e\c\ecf607c149ca955271792aa8980e0112b3cb596d
+e
+5src/main/java/cn/zyjblogs/design/singleton/Mgr06.java,0\7\073414959b2cba568061c6170a81830d44091a42
+h
+8src/main/java/cn/zyjblogs/design/strategy/demo1/Dog.java,2\b\2bc4eff5039cddf9fee022a349fd17737a973e21
+h
+8src/main/java/cn/zyjblogs/design/strategy/demo1/Cat.java,5\3\538fa576cf057b796d7e79a9a755a0b83414ac78
+o
+?src/main/java/cn/zyjblogs/design/strategy/demo1/Comparable.java,a\a\aa58d5a9b76b643349babf1bfe6d142bb431751c
+i
+9src/main/java/cn/zyjblogs/design/strategy/demo1/Main.java,4\f\4fe8b705fa2a77020740ccba086b899a2f4e1396
+k
+;src/main/java/cn/zyjblogs/design/strategy/demo1/Sorter.java,5\d\5dfc6bb5f771f251e413b26a2b5f818eeb42f5e3
+j
+:src/main/java/cn/zyjblogs/design/strategy/demo2/Apple.java,f\0\f0002aa414906e03cfe735af5e205e3a37058275
+k
+;src/main/java/cn/zyjblogs/design/strategy/demo2/Banana.java,a\2\a26362fa3f616a9cdc821117d710f7756288feb8
+o
+?src/main/java/cn/zyjblogs/design/strategy/demo1/Comparator.java,1\c\1cb148ba0087a33e8bf02613d8b7c679b91fcd06
+h
+8src/main/java/cn/zyjblogs/design/strategy/demo2/Eat.java,9\6\96018b20f7c8fd3f289b14d1bdd86afc026ad845
+k
+;src/main/java/cn/zyjblogs/design/strategy/demo2/Person.java,f\1\f19d32621904e7c6e9bc23b597914e627016f477
+q
+Asrc/main/java/cn/zyjblogs/design/strategy/demo3/FireStrategy.java,b\a\ba03e3261c143c58db1241b701637785cfb88c69
+x
+Hsrc/main/java/cn/zyjblogs/design/strategy/demo3/DefaultFireStrategy.java,2\c\2ce023438f835f805ac4a1f962865f8747405a15
+h
+8src/main/java/cn/zyjblogs/design/strategy/demo3/Gun.java,5\5\55e78da8cef5a7f5f5f35b84ef0fffeffae25776
+v
+Fsrc/main/java/cn/zyjblogs/design/strategy/demo3/RifleFireStrategy.java,8\f\8f24cca5dad2bcc60cd2edd9f5b89b16a07bbf4f
+z
+Jsrc/main/java/cn/zyjblogs/design/strategy/demo3/GunSniperFireStrategy.java,4\e\4ee28baa566c84b7148d31fd81fd340690b18c35
+k
+;src/main/java/cn/zyjblogs/design/strategy/demo3/Person.java,0\1\01f95235e084159bc461a677ecf5e834be0f6469

+ 124 - 0
.idea/uiDesigner.xml

@@ -0,0 +1,124 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Palette2">
+    <group name="Swing">
+      <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
+      </item>
+      <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
+      </item>
+      <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
+      </item>
+      <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
+        <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
+      </item>
+      <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
+        <initial-values>
+          <property name="text" value="Button" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
+        <initial-values>
+          <property name="text" value="RadioButton" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
+        <initial-values>
+          <property name="text" value="CheckBox" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
+        <initial-values>
+          <property name="text" value="Label" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
+          <preferred-size width="150" height="-1" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
+          <preferred-size width="150" height="-1" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
+          <preferred-size width="150" height="-1" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
+      </item>
+      <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
+          <preferred-size width="200" height="200" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
+          <preferred-size width="200" height="200" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
+      </item>
+      <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
+      </item>
+      <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
+      </item>
+      <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
+      </item>
+      <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
+          <preferred-size width="-1" height="20" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
+      </item>
+      <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
+      </item>
+    </group>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 20 - 0
pom.xml

@@ -0,0 +1,20 @@
+<?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>cn.zyjblogs</groupId>
+    <artifactId>design</artifactId>
+    <packaging>pom</packaging>
+    <version>1.0v</version>
+    <modules>
+        <module>singleton</module>
+    </modules>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+</project>

+ 6 - 0
src/main/java/cn/zyjblogs/design/singleton/Main.java

@@ -0,0 +1,6 @@
+package cn.zyjblogs.design.singleton;
+
+public class Main {
+    public static void main(String[] args) {
+    }
+}

+ 27 - 0
src/main/java/cn/zyjblogs/design/singleton/Mgr01.java

@@ -0,0 +1,27 @@
+package cn.zyjblogs.design.singleton;
+
+/**
+ * 单例模式
+ * 饿汉式
+ * 类加载到内存后,就实例化一个单例,JVM保证线程安全
+ * 简单实用,推荐使用!
+ * 缺点: 不管用到与否,类加载时就完成实例化
+ */
+public class Mgr01 {
+    private static final Mgr01 INSTANCE = new Mgr01();
+    private Mgr01() {
+
+    }
+    public static Mgr01 getInstance(){
+        return INSTANCE;
+    }
+    public void m(){
+        System.out.println("m");
+    }
+
+    public static void main(String[] args) {
+        Mgr01 m1 = Mgr01.getInstance();
+        Mgr01 m2 = Mgr01.getInstance();
+        System.out.println(m1==m2);
+    }
+}

+ 30 - 0
src/main/java/cn/zyjblogs/design/singleton/Mgr02.java

@@ -0,0 +1,30 @@
+package cn.zyjblogs.design.singleton;
+
+/**
+ * 单例模式
+ * 饿汉式
+ * 类加载到内存后,就实例化一个单例,JVM保证线程安全
+ * 简单实用,推荐使用!
+ * 缺点: 不管用到与否,类加载时就完成实例化
+ */
+public class Mgr02 {
+    private static final Mgr02 INSTANCE;
+    static {
+        INSTANCE = new Mgr02();
+    }
+    private Mgr02() {
+
+    }
+    public static Mgr02 getInstance(){
+        return INSTANCE;
+    }
+    public void m(){
+        System.out.println("m");
+    }
+
+    public static void main(String[] args) {
+        Mgr02 m1 = Mgr02.getInstance();
+        Mgr02 m2 = Mgr02.getInstance();
+        System.out.println(m1==m2);
+    }
+}

+ 38 - 0
src/main/java/cn/zyjblogs/design/singleton/Mgr03.java

@@ -0,0 +1,38 @@
+package cn.zyjblogs.design.singleton;
+
+/**
+ * 懒汉式
+ * 虽然也能达到按需初始化的目的,但却带来了线程不安全问题
+ */
+public class Mgr03 {
+    private static  Mgr03 INSTANCE;
+    private Mgr03() {
+    }
+    /**
+     * 线程不安全
+     * @return
+     */
+    public static  Mgr03 getInstance(){
+        if (INSTANCE == null){
+            try {
+                Thread.sleep(1);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            INSTANCE = new Mgr03();
+        }
+        return INSTANCE;
+    }
+
+    public void m(){
+        System.out.println("m");
+    }
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 100; i++) {
+            new Thread(()->{
+                System.out.println(Mgr03.getInstance().hashCode());
+            }).start();
+        }
+    }
+}

+ 38 - 0
src/main/java/cn/zyjblogs/design/singleton/Mgr04.java

@@ -0,0 +1,38 @@
+package cn.zyjblogs.design.singleton;
+
+/**
+ * 懒汉式
+ * 虽然也能达到按需初始化的目的,但却带来了线程不安全问题
+ */
+public class Mgr04 {
+    private static Mgr04 INSTANCE;
+    private Mgr04() {
+    }
+    /**
+     * 线程安全,但是要上锁,效率降低了
+     * @return
+     */
+    public static synchronized Mgr04 getInstance(){
+        if (INSTANCE == null){
+            try {
+                Thread.sleep(1);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            INSTANCE = new Mgr04();
+        }
+        return INSTANCE;
+    }
+
+    public void m(){
+        System.out.println("m");
+    }
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 100; i++) {
+            new Thread(()->{
+                System.out.println(Mgr04.getInstance().hashCode());
+            }).start();
+        }
+    }
+}

+ 44 - 0
src/main/java/cn/zyjblogs/design/singleton/Mgr05.java

@@ -0,0 +1,44 @@
+package cn.zyjblogs.design.singleton;
+
+/**
+ * 懒汉式
+ * 虽然也能达到按需初始化的目的,但却带来了线程不安全问题
+ */
+public class Mgr05 {
+    private static Mgr05 INSTANCE;
+
+    private Mgr05() {
+    }
+
+    /**
+     * 线程不安全,但是要上锁,效率降低了
+     *
+     * @return
+     */
+    public static  Mgr05 getInstance() {
+        if (INSTANCE == null) {
+            //通过减小同步代码块的方式提高效率,然后可不行
+            synchronized (Mgr05.class) {
+                try {
+                    Thread.sleep(1);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+            INSTANCE = new Mgr05();
+        }
+        return INSTANCE;
+    }
+
+    public void m() {
+        System.out.println("m");
+    }
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 100; i++) {
+            new Thread(() -> {
+                System.out.println(Mgr05.getInstance().hashCode());
+            }).start();
+        }
+    }
+}

+ 49 - 0
src/main/java/cn/zyjblogs/design/singleton/Mgr06.java

@@ -0,0 +1,49 @@
+package cn.zyjblogs.design.singleton;
+
+/**
+ * 懒汉式
+ * 虽然也能达到按需初始化的目的,但却带来了线程不安全问题
+ */
+public class Mgr06 {
+    //volatile 防止指令从拍 JIT
+    private static volatile Mgr06 INSTANCE;
+
+    private Mgr06() {
+    }
+
+    /**
+     * 线程安全,但是要上锁
+     *
+     * @return
+     */
+    public static Mgr06 getInstance() {
+        if (INSTANCE == null) {
+            //通过减小同步代码块的方式提高效率,
+            synchronized (Mgr06.class) {
+                //双重检查
+                if (INSTANCE != null) {
+                    return INSTANCE;
+                }
+                try {
+                    Thread.sleep(1);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+                INSTANCE = new Mgr06();
+            }
+        }
+        return INSTANCE;
+    }
+
+    public void m() {
+        System.out.println("m");
+    }
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 100; i++) {
+            new Thread(() -> {
+                System.out.println(Mgr06.getInstance().hashCode());
+            }).start();
+        }
+    }
+}

+ 31 - 0
src/main/java/cn/zyjblogs/design/singleton/Mgr07.java

@@ -0,0 +1,31 @@
+package cn.zyjblogs.design.singleton;
+
+/**
+ * 静态内部类方式
+ * JVM保证单例
+ * 加载外部类时不会加载内部类,这样可以实现懒加载
+ */
+public class Mgr07 {
+
+    private Mgr07() {
+    }
+
+    private static class Mgr07Holder {
+        private static Mgr07 INSTANCE = new Mgr07();
+    }
+
+    public static Mgr07 getInstance(){
+        return Mgr07Holder.INSTANCE;
+    }
+    public void m() {
+        System.out.println("m");
+    }
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 100; i++) {
+            new Thread(() -> {
+                System.out.println(Mgr07.getInstance().hashCode());
+            }).start();
+        }
+    }
+}

+ 19 - 0
src/main/java/cn/zyjblogs/design/singleton/Mgr08.java

@@ -0,0 +1,19 @@
+package cn.zyjblogs.design.singleton;
+
+/**
+ * 不仅可以解决线程同步,还可以防止反序列化
+ */
+public enum Mgr08 {
+    INSTANCE;
+    public void m() {
+        System.out.println("m");
+    }
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 100; i++) {
+            new Thread(() -> {
+                System.out.println(Mgr08.INSTANCE.hashCode());
+            }).start();
+        }
+    }
+}

+ 33 - 0
src/main/java/cn/zyjblogs/design/strategy/demo1/Cat.java

@@ -0,0 +1,33 @@
+package cn.zyjblogs.design.strategy.demo1;
+
+public class Cat implements Comparable<Cat>{
+    int weight;
+    int height;
+
+    public Cat(int weight, int height) {
+        this.weight = weight;
+        this.height = height;
+    }
+
+    /**
+     * 对猫的体重进行排序
+     * @param c
+     * @return
+     */
+    @Override
+    public int compareTo(Cat c) {
+        if(this.weight < c.weight) return -1;
+        else if (this.weight > c.weight) return 1;
+        else return 0;
+    }
+
+    @Override
+    public String toString() {
+        return "Cat{" +
+                "weight=" + weight +
+                ", height=" + height +
+                '}';
+    }
+
+
+}

+ 5 - 0
src/main/java/cn/zyjblogs/design/strategy/demo1/Comparable.java

@@ -0,0 +1,5 @@
+package cn.zyjblogs.design.strategy.demo1;
+
+public interface Comparable<T> {
+    int compareTo(T o);
+}

+ 13 - 0
src/main/java/cn/zyjblogs/design/strategy/demo1/Comparator.java

@@ -0,0 +1,13 @@
+package cn.zyjblogs.design.strategy.demo1;
+
+/**
+ * 比较器
+ */
+@FunctionalInterface
+public interface Comparator<T> {
+    int compare(T o1,T o2);
+    //default 方法可不需要实现
+    default void m(){
+        System.out.println("m");
+    }
+}

+ 23 - 0
src/main/java/cn/zyjblogs/design/strategy/demo1/Dog.java

@@ -0,0 +1,23 @@
+package cn.zyjblogs.design.strategy.demo1;
+
+public class Dog implements Comparable<Dog> {
+    int food;
+
+    public Dog(int food) {
+        this.food = food;
+    }
+
+    @Override
+    public String toString() {
+        return "Dog{" +
+                "food=" + food +
+                '}';
+    }
+
+    @Override
+    public int compareTo(Dog d) {
+        if (this.food < d.food) return -1;
+        else if (this.food > d.food) return 1;
+        else return 0;
+    }
+}

+ 36 - 0
src/main/java/cn/zyjblogs/design/strategy/demo1/Main.java

@@ -0,0 +1,36 @@
+package cn.zyjblogs.design.strategy.demo1;
+
+import java.util.Arrays;
+
+public class Main {
+    public static void main(String[] args) {
+//        int[] a ={9,2,3,5,7,1,4};
+//        Sorter sorter = new Sorter();
+//        sorter.sort(a);
+//        System.out.println(Arrays.toString(a));
+
+//        Cat[] a = {new Cat(3,3),new Cat(5,5),new Cat(1,1)};
+//        Sorter sorter = new Sorter();
+//        sorter.sort(a);
+//        System.out.println(Arrays.toString(a));
+
+//        Dog[] b = {new Dog(3),new Dog(5),new Dog(1)};
+//        Sorter<Dog> sorter = new Sorter();
+//        sorter.sort(b,(a,d)->{
+//            if (a.food < d.food) return -1;
+//            else if (a.food > d.food) return 1;
+//            else return 0;
+//        });
+//        System.out.println(Arrays.toString(b));
+
+        Cat[] c = {new Cat(3,3),new Cat(5,5),new Cat(1,1)};
+        Sorter<Cat> sorter1 = new Sorter();
+        sorter1.sort(c,(m,n)->{
+            if (m.height < n.height) return -1;
+            else if (m.height > n.height) return 1;
+            else return 0;
+        });
+        System.out.println(Arrays.toString(c));
+
+    }
+}

+ 52 - 0
src/main/java/cn/zyjblogs/design/strategy/demo1/Sorter.java

@@ -0,0 +1,52 @@
+package cn.zyjblogs.design.strategy.demo1;
+
+/**
+ * 排序
+ */
+public class Sorter<T> {
+
+    public  void sort(T[] arr,Comparator<T> comparator){
+        for (int i = 0; i < arr.length - 1; i++){
+            int minPos = i;
+            for (int j = i+1; j < arr.length; j++) {
+                minPos = comparator.compare(arr[j],arr[minPos]) == -1 ? j : minPos;
+            }
+            swap(arr,i,minPos);
+        }
+    }
+    public void swap(T[] arr,int i, int j) {
+        T temp = arr[i];
+        arr[i] = arr[j];
+        arr[j] = temp;
+    }
+//    public static void sort(int[] arr){
+//        for (int i = 0; i < arr.length - 1; i++){
+//            int minPos = i;
+//            for (int j = i+1; j < arr.length; j++) {
+//                minPos = arr[j] < arr[minPos] ? j : minPos;
+//            }
+//            swap(arr,i,minPos);
+//        }
+//    }
+//
+//    public static void swap(int[] arr,int i, int j) {
+//        int temp = arr[i];
+//        arr[i] = arr[j];
+//        arr[j] = temp;
+//    }
+//    public static void sort(Cat[] arr){
+//        for (int i = 0; i < arr.length - 1; i++){
+//            int minPos = i;
+//            for (int j = i+1; j < arr.length; j++) {
+//                minPos = arr[j].compareTo(arr[minPos]) == -1 ? j : minPos;
+//            }
+//            swap(arr,i,minPos);
+//        }
+//    }
+//
+//    public static void swap(Cat[] arr,int i, int j) {
+//        Cat temp = arr[i];
+//        arr[i] = arr[j];
+//        arr[j] = temp;
+//    }
+}

+ 9 - 0
src/main/java/cn/zyjblogs/design/strategy/demo2/Apple.java

@@ -0,0 +1,9 @@
+package cn.zyjblogs.design.strategy.demo2;
+
+public class Apple implements Eat{
+
+    @Override
+    public void eat() {
+        System.out.println("吃苹果");
+    }
+}

+ 8 - 0
src/main/java/cn/zyjblogs/design/strategy/demo2/Banana.java

@@ -0,0 +1,8 @@
+package cn.zyjblogs.design.strategy.demo2;
+
+public class Banana implements Eat{
+    @Override
+    public void eat() {
+        System.out.println("吃香蕉");
+    }
+}

+ 5 - 0
src/main/java/cn/zyjblogs/design/strategy/demo2/Eat.java

@@ -0,0 +1,5 @@
+package cn.zyjblogs.design.strategy.demo2;
+
+public interface Eat {
+    void eat();
+}

+ 13 - 0
src/main/java/cn/zyjblogs/design/strategy/demo2/Person.java

@@ -0,0 +1,13 @@
+package cn.zyjblogs.design.strategy.demo2;
+
+public class Person {
+    public void eat(Eat eat){
+        eat.eat();
+    }
+    public static void main(String[] args) {
+        Person person1 = new Person();
+        person1.eat(new Apple());
+        person1.eat(new Banana());
+        person1.eat(()-> System.out.println("吃饭"));
+    }
+}

+ 9 - 0
src/main/java/cn/zyjblogs/design/strategy/demo3/DefaultFireStrategy.java

@@ -0,0 +1,9 @@
+package cn.zyjblogs.design.strategy.demo3;
+
+public class DefaultFireStrategy implements FireStrategy{
+
+    @Override
+    public void fire(Gun g) {
+        g.shoot("手枪");
+    }
+}

+ 5 - 0
src/main/java/cn/zyjblogs/design/strategy/demo3/FireStrategy.java

@@ -0,0 +1,5 @@
+package cn.zyjblogs.design.strategy.demo3;
+
+public interface FireStrategy {
+    void fire(Gun g);
+}

+ 15 - 0
src/main/java/cn/zyjblogs/design/strategy/demo3/Gun.java

@@ -0,0 +1,15 @@
+package cn.zyjblogs.design.strategy.demo3;
+
+public class Gun {
+    FireStrategy fireStrategy;
+    public Gun(FireStrategy fireStrategy) {
+        this.fireStrategy = fireStrategy;
+    }
+    public void fire() {
+        fireStrategy.fire(this);
+    }
+    public void shoot(String name) {
+        System.out.println(name+" 发出一个子弹");
+    }
+
+}

+ 9 - 0
src/main/java/cn/zyjblogs/design/strategy/demo3/GunSniperFireStrategy.java

@@ -0,0 +1,9 @@
+package cn.zyjblogs.design.strategy.demo3;
+
+public class GunSniperFireStrategy implements FireStrategy{
+
+    @Override
+    public void fire(Gun g) {
+        g.shoot("炮狙");
+    }
+}

+ 26 - 0
src/main/java/cn/zyjblogs/design/strategy/demo3/Person.java

@@ -0,0 +1,26 @@
+package cn.zyjblogs.design.strategy.demo3;
+
+import java.lang.reflect.InvocationTargetException;
+
+public class Person {
+    public static void main(String[] args) {
+        Gun gun = new Gun(new DefaultFireStrategy());
+        gun.fire();
+        Gun gun1 = new Gun(new RifleFireStrategy());
+        gun1.fire();
+        Gun gun2= new Gun(new GunSniperFireStrategy());
+        gun2.fire();
+        //lamdba表达式
+        Gun gun3= new Gun((a)-> a.shoot("机枪"));
+        gun3.fire();
+        //反射
+        try {
+            Class<?> aClass = Class.forName("cn.zyjblogs.design.strategy.demo3.GunSniperFireStrategy");
+            GunSniperFireStrategy n = (GunSniperFireStrategy)aClass.getDeclaredConstructor().newInstance();
+            Gun gun4 = new Gun(n);
+            gun4.fire();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 9 - 0
src/main/java/cn/zyjblogs/design/strategy/demo3/RifleFireStrategy.java

@@ -0,0 +1,9 @@
+package cn.zyjblogs.design.strategy.demo3;
+
+public class RifleFireStrategy implements FireStrategy{
+
+    @Override
+    public void fire(Gun g) {
+        g.shoot("步枪");
+    }
+}