commit 18dad6087e9956d67b517c2447261db251aa832c Author: zeek <984294471@qq.com> Date: Sun Feb 23 22:01:09 2020 +0800 迁移仓库 diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..cf326bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +/.idea/* +/.idea/ +.idea/* +.idea/ +out/* +out/ +*.iml +tags +_book +node_modules +gradle/ +build/ +.gradle +bin/* +.classpath +.project +.settings diff --git a/README.md b/README.md new file mode 100644 index 0000000..4799b00 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# 设计模式 + +设计模式是我们必须要掌握的一项技能
+设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、 +让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的;设计模式使代码编制真正工程化;设计模式 +是软件工程的基石脉络,如同大厦的结构一样。 + + +## 六大设计原则 + +* [单一职责原理](principle#1单一职责原理-) +* [里氏替换原则](principle#1里氏替换原则) +* [依赖倒置原则](principle#3依赖倒置原则) +* [接口隔离原则](principle#4接口隔离原则) +* [迪米特法则](principle#5迪米特法则) +* [开闭原则](principle#6开闭原则) + +## 23种设计模式 + +* 创建型模式: +[简单工厂模式](simpleFactory)、[抽象工厂模式](com.zeekling.abstractFactory)、[单例模式](simgleton) 、[建造者模式](Builder)、[原型模式](com.zeekling.Prototype); +* 结构型模式: +[适配器模式](com.zeekling.adapter) 、[桥接模式](bridge)、[代理模式](gproxy)、[组合模式](gcomposite)、[外观模式](gfacade) 、[装饰模式](gdecorate)、[享元模式](gflyweight); +* 行为型模式: +[责任链模式](com.zeekling.handler)、[迭代子模式](giterator) 、[备忘录模式](gmemento) 、[命令模式](gcommand)、[状态模式](gstate) 、[观察者模式](gobserver) 、[策略模式](gstrategy) 、[模板模式](gtemplate) 、[访问者模式](gvisitor) 、[中介者模式](gmediator) 、[解释器模式](ginterpreter); + + +## 存在的问题 +在学习设计模式时,发现了一个问题:自己这样看别人例子,没有实践,对设计模式的理解不是特别容易。不过我尽我可能理解。 + +## 感悟 + +最近发现一味的在网上google看博客是不行的,这样自己对知识很难有一个整体的把握,觉得google看博客和看书结合起来是一个很不错的选择。 + diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..2180f7b --- /dev/null +++ b/build.gradle @@ -0,0 +1,14 @@ +group 'com.zeekling' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..53d2d89 --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java com.zeekling.command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' com.zeekling.command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java com.zeekling.command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..008d22f --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' com.zeekling.command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get com.zeekling.command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the com.zeekling.command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the com.zeekling.command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/pics/abstractFactory.jpg b/pics/abstractFactory.jpg new file mode 100644 index 0000000..ab8cb7e Binary files /dev/null and b/pics/abstractFactory.jpg differ diff --git a/pics/bridge.jpg b/pics/bridge.jpg new file mode 100644 index 0000000..83311e5 Binary files /dev/null and b/pics/bridge.jpg differ diff --git a/pics/builder.png b/pics/builder.png new file mode 100644 index 0000000..b411c12 Binary files /dev/null and b/pics/builder.png differ diff --git a/pics/classAdapter.png b/pics/classAdapter.png new file mode 100644 index 0000000..802ee88 Binary files /dev/null and b/pics/classAdapter.png differ diff --git a/pics/command.jpg b/pics/command.jpg new file mode 100644 index 0000000..a200067 Binary files /dev/null and b/pics/command.jpg differ diff --git a/pics/composite.jpg b/pics/composite.jpg new file mode 100644 index 0000000..09f9f20 Binary files /dev/null and b/pics/composite.jpg differ diff --git a/pics/decorator.png b/pics/decorator.png new file mode 100644 index 0000000..b6891c9 Binary files /dev/null and b/pics/decorator.png differ diff --git a/pics/facade.jpg b/pics/facade.jpg new file mode 100644 index 0000000..6ef63f8 Binary files /dev/null and b/pics/facade.jpg differ diff --git a/pics/flyweight1.png b/pics/flyweight1.png new file mode 100644 index 0000000..19a81ba Binary files /dev/null and b/pics/flyweight1.png differ diff --git a/pics/flyweight2.png b/pics/flyweight2.png new file mode 100644 index 0000000..0ef263f Binary files /dev/null and b/pics/flyweight2.png differ diff --git a/pics/handler.png b/pics/handler.png new file mode 100644 index 0000000..a9c77da Binary files /dev/null and b/pics/handler.png differ diff --git a/pics/head.jpg b/pics/head.jpg new file mode 100644 index 0000000..087d6dd Binary files /dev/null and b/pics/head.jpg differ diff --git a/pics/iterator.jpg b/pics/iterator.jpg new file mode 100644 index 0000000..12a27a0 Binary files /dev/null and b/pics/iterator.jpg differ diff --git a/pics/memento.png b/pics/memento.png new file mode 100644 index 0000000..46b8d5d Binary files /dev/null and b/pics/memento.png differ diff --git a/pics/objectAdapter.png b/pics/objectAdapter.png new file mode 100644 index 0000000..e5ce2fa Binary files /dev/null and b/pics/objectAdapter.png differ diff --git a/pics/prototype.png b/pics/prototype.png new file mode 100644 index 0000000..d9e1b89 Binary files /dev/null and b/pics/prototype.png differ diff --git a/pics/proxy.png b/pics/proxy.png new file mode 100644 index 0000000..21bb250 Binary files /dev/null and b/pics/proxy.png differ diff --git a/pics/simpleFactory.png b/pics/simpleFactory.png new file mode 100644 index 0000000..ccbec53 Binary files /dev/null and b/pics/simpleFactory.png differ diff --git a/pics/simplePrototype.png b/pics/simplePrototype.png new file mode 100644 index 0000000..6ced993 Binary files /dev/null and b/pics/simplePrototype.png differ diff --git a/principle/readme.md b/principle/readme.md new file mode 100644 index 0000000..839351f --- /dev/null +++ b/principle/readme.md @@ -0,0 +1,66 @@ +## 设计模式的六大原则 +### 1、单一职责原理 +不要存在多余一个类变更的原因。通俗的说,集一个类只负责一项职责。总结一句话:不能代码量少,把牛头马嘴一起往一个类塞。基本和unix的编程思想一致。 +优点: +- 减低系统复杂度,结构清晰. +- 提高代码的可读性 +- 提高系统的可维护性 + +### 2、里氏替换原则 +定义:所有引用基类(父类)的地方必须能透明地使用其子类的对象。 + +简单来讲就是,在软件中将一个基类对象替换成它的子类对象,程序将不会产生任何错误和异常,反过来则不成立,如果一个软件实体使 +用的是一个子类对象的话,那么它不一定能够使用基类对象。 + +* 子类可以实现父类的抽象方法,但不能覆盖父类的非抽象方法。 +* 子类中可以增加自己特有的方法。 +* 当子类的方法重载父类方法时,方法的前置条件()即方法的行参)要比父类方法输入的参数更加宽松。 +* 当子类的方法实现父类的抽象方法时,方法的后置条件()即方法的返回值)要比父类更严格。 + +采用里氏替换原则的目的就是增强程序的健壮性,版本升级时也可以保持非常好的兼容性。即使增加子类,原有的子类还可以继续运行。 +在实际项目中,每个子类对应不同的业务含义,使用父类作为参数,传递不同的子类完成不同的业务逻辑,非常完美。 + +尽量不要重写父类已经实现了的方法,可以用接口等其他方法绕过。 + +### 3、依赖倒置原则 +定义:抽象不应该依赖于细节,细节应当依赖于抽象。换言之,要针对接口编程,而不是针对实现编程。 + +依赖倒置原则的本质就是通过抽象(接口或抽象类)使各个类或模块的实现彼此独立,不互相影响,实现模块间的松耦合。在项目中使 +用,我们只要遵循以下几个规则就可以. + +- 每个类尽量都有接口或抽象类,或者抽象类和接口两者都具备。 +- 变量的表名类型尽量是接口或者抽象类。 +- 任何类都不应该从具体类派生。 +- 尽量不要覆写基类的方法。 +- 结合里氏替换原则使用 + +高层模块不应该依赖底层模块,二者应该依赖其抽象;抽象不应该依赖细节;细节应该依赖抽象。 +总结:读用抽象的接口来描述相同的动作,降低实现这个动作的人和物之间的耦合度。 + +### 4、接口隔离原则 +客户端不应该依赖他不需要的接口;一个类对另一个类的依赖应该建立在最小的接口上。 +根据接口隔离原则,当一个接口太大时,我们需要将它分割成一些更细小的接口,使用该接口的客户端仅需知道与之相关的方法即 +可。每一个接口应该承担一种相对独立的角色,不干不该干的事,该干的事都要干。 +看到这里好像接口隔离原则与单一职责原则是相同的。其实接口隔离原则与单一职责原则的审视角度是不相同的,单一职责原则要 +求的是类和接口职责单一,注重的是职责,这是业务逻辑上的划分,而接口隔离原则要求接口的方法尽量少。 +接口隔离原则是对接口进行规范约束,其包含的以下4层含义: + +- 接口要尽量少。 +- 接口要高内聚。 +- 定制服务。 +- 接口设计师有限度的。 + +在使用接口隔离原则时,我们需要注意控制接口的粒度,接口不能太小,如果太小会导致系统中接口泛滥,不利于维护;接口也不 +能太大,太大的接口将违背接口隔离原则,灵活性较差,使用起来很不方便。一般而言,接口中仅包含为某一类用户定制的方法即 +可,不应该强迫客户依赖于那些它们不用的方法。 + +接口隔离原则是对接口的定义,同时也是对类的定义,接口和类尽量使用原子接口或原子类来组装。但是,这个原子该怎么划分是 +设计模式中的一大难题,在实践中可以根据以下几个规则来衡量. + +### 5、迪米特法则 +迪米特法则又叫最小知道原则。通俗的来讲,就是一个类对自己依赖的类知道的越少越好。也就是说,对于被依赖的类来说,无论逻辑多么复杂, +都尽量将>逻辑封装在类的内部,对外除了提供的public方法,不对外泄漏任何信息。 +总结: + +### 6、开闭原则 +尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。 diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..1a120d6 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'design-pattern' + diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..223836d --- /dev/null +++ b/src/README.md @@ -0,0 +1,39 @@ +# 设计模式 + +设计模式是我们必须要掌握的一项技能
+设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、 +让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的;设计模式使代码编制真正工程化;设计模式 +是软件工程的基石脉络,如同大厦的结构一样。 + + +## 六大设计原则 + +* [单一职责原理](principle#1单一职责原理-)
+* [里氏替换原则](principle#1里氏替换原则)
+* [依赖倒置原则](principle#3依赖倒置原则)
+* [接口隔离原则](principle#4接口隔离原则)
+* [迪米特法则](principle#5迪米特法则)
+* [开闭原则](principle#6开闭原则)
+ +## 23种设计模式 + +* 创建型模式: +[简单工厂模式](main/java/simpleFactory)、[抽象工厂模式](main/java/abstractFactory)、[单例模式](main/java/simgleton) 、[建造者模式](main/java/Builder)、[原型模式](main/java/Prototype);
+* 结构型模式: +[适配器模式](main/java/adapter) 、[桥接模式](main/java/bridge)、[代理模式](gproxy)、[组合模式](gcomposite)、[外观模式](gfacade) 、[装饰模式](gdecorate)、[享元模式](gflyweight);
+* 行为型模式: +[责任链模式](main/java/handler)、[迭代子模式](giterator) 、[备忘录模式](gmemento) 、[命令模式](gcommand)、[状态模式](gstate) 、[观察者模式](gobserver) 、[策略模式](gstrategy) 、[模板模式](gtemplate) 、[访问者模式](gvisitor) 、[中介者模式](gmediator) 、[解释器模式](ginterpreter); + +## 相关github项目 + +[最近在看设计模式时在github上面看到了一个写的不错的有关设计模式的项目就fork了过了打算仔细看看](https://github.com/lzh-fork/DPModel) +## 存在的问题 +在学习设计模式时,发现了一个问题:自己这样看别人例子,没有实践,对设计模式的理解不是特别容易。不过我尽我可能理解。 + +## 感悟 + +最近发现一味的在网上google看博客是不行的,这样自己对知识很难有一个整体的把握,觉得google看博客和看书结合起来是一个很不错的选择。 + + + +---- diff --git a/src/main/java/com/zeekling/abstractFactory/AbstractFactory.java b/src/main/java/com/zeekling/abstractFactory/AbstractFactory.java new file mode 100755 index 0000000..2bcf4cb --- /dev/null +++ b/src/main/java/com/zeekling/abstractFactory/AbstractFactory.java @@ -0,0 +1,12 @@ +package com.zeekling.abstractFactory; + +/** + * @apiNote 抽象工厂模式 + * @author zeekling + * @version 1.0 + * @since 2019-12-04 + */ +public interface AbstractFactory { + void america(); + void chinese(); +} diff --git a/src/main/java/com/zeekling/abstractFactory/Board.java b/src/main/java/com/zeekling/abstractFactory/Board.java new file mode 100755 index 0000000..bd5a5c9 --- /dev/null +++ b/src/main/java/com/zeekling/abstractFactory/Board.java @@ -0,0 +1,11 @@ +package com.zeekling.abstractFactory; + +/** + * 创造主板 + */ +public interface Board { + + void americaBoard(); + + void chineseBoard(); +} diff --git a/src/main/java/com/zeekling/abstractFactory/Display.java b/src/main/java/com/zeekling/abstractFactory/Display.java new file mode 100755 index 0000000..3117236 --- /dev/null +++ b/src/main/java/com/zeekling/abstractFactory/Display.java @@ -0,0 +1,11 @@ +package com.zeekling.abstractFactory; + +/** + * 创建显示屏 + */ +public interface Display { + + void americaDisplay(); + + void chineseDisplay(); +} diff --git a/src/main/java/com/zeekling/abstractFactory/README.md b/src/main/java/com/zeekling/abstractFactory/README.md new file mode 100644 index 0000000..2400fe7 --- /dev/null +++ b/src/main/java/com/zeekling/abstractFactory/README.md @@ -0,0 +1,7 @@ +# 抽象工厂模式 +## 简介 +抽象工厂模式为创建一组对象提供了一种解决方案。与简单工厂模式,抽象工厂模式中的具体工厂模式不只是创建一种产品,他负责创建一组产品。 +## 使用场景 +为创建一组对象提供了一种解决方案 +## UML类图 +![抽象工厂模式](https://git.zeekling.cn/java/designPattern/raw/master/pics/abstractFactory.jpg) diff --git a/src/main/java/com/zeekling/abstractFactory/impl/BoardImpl.java b/src/main/java/com/zeekling/abstractFactory/impl/BoardImpl.java new file mode 100755 index 0000000..9967520 --- /dev/null +++ b/src/main/java/com/zeekling/abstractFactory/impl/BoardImpl.java @@ -0,0 +1,18 @@ +package com.zeekling.abstractFactory.impl; + +import com.zeekling.abstractFactory.Board; + +/** + * Created by lzh on 16-6-10. + */ +public class BoardImpl implements Board { + @Override + public void americaBoard() { + System.out.println("美国产主板"); + } + + @Override + public void chineseBoard() { + System.out.println("中国产主板"); + } +} diff --git a/src/main/java/com/zeekling/abstractFactory/impl/DisplayImpl.java b/src/main/java/com/zeekling/abstractFactory/impl/DisplayImpl.java new file mode 100755 index 0000000..27b9679 --- /dev/null +++ b/src/main/java/com/zeekling/abstractFactory/impl/DisplayImpl.java @@ -0,0 +1,18 @@ +package com.zeekling.abstractFactory.impl; + +import com.zeekling.abstractFactory.Display; + +/** + * + */ +public class DisplayImpl implements Display { + @Override + public void americaDisplay() { + System.out.println("美国产显示屏"); + } + + @Override + public void chineseDisplay() { + System.out.println("中国产显示屏"); + } +} diff --git a/src/main/java/com/zeekling/abstractFactory/impl/FactoryImpl.java b/src/main/java/com/zeekling/abstractFactory/impl/FactoryImpl.java new file mode 100755 index 0000000..a21fa80 --- /dev/null +++ b/src/main/java/com/zeekling/abstractFactory/impl/FactoryImpl.java @@ -0,0 +1,34 @@ +package com.zeekling.abstractFactory.impl; + +import com.zeekling.abstractFactory.AbstractFactory; +import com.zeekling.abstractFactory.Display; +import com.zeekling.abstractFactory.Board; + +/** + * Created by lzh on 16-6-10. + */ +public class FactoryImpl implements AbstractFactory { + private Display button = null; + private Board panel = null; + + public FactoryImpl() { + try { + button = DisplayImpl.class.newInstance(); + panel = BoardImpl.class.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + } + + @Override + public void america() { + panel.americaBoard(); + button.americaDisplay(); + } + + @Override + public void chinese() { + panel.chineseBoard(); + button.chineseDisplay(); + } +} diff --git a/src/main/java/com/zeekling/adapter/Alibaba.java b/src/main/java/com/zeekling/adapter/Alibaba.java new file mode 100755 index 0000000..4aa92ce --- /dev/null +++ b/src/main/java/com/zeekling/adapter/Alibaba.java @@ -0,0 +1,10 @@ +package com.zeekling.adapter; + +/** + * Created by lzh on 3/30/16. + */ +public class Alibaba { + public void mayun(){ + System.out.println("Alibaba"); + } +} diff --git a/src/main/java/com/zeekling/adapter/Baidu.java b/src/main/java/com/zeekling/adapter/Baidu.java new file mode 100755 index 0000000..43b45c5 --- /dev/null +++ b/src/main/java/com/zeekling/adapter/Baidu.java @@ -0,0 +1,8 @@ +package com.zeekling.adapter; + +/** + * Created by lzh on 3/30/16. + */ +public interface Baidu { + void liyanhong(); +} diff --git a/src/main/java/com/zeekling/adapter/BaiduImp.java b/src/main/java/com/zeekling/adapter/BaiduImp.java new file mode 100755 index 0000000..d3a378d --- /dev/null +++ b/src/main/java/com/zeekling/adapter/BaiduImp.java @@ -0,0 +1,12 @@ +package com.zeekling.adapter; + +/** + * Created by lzh on 3/30/16. + */ +public class BaiduImp extends Alibaba implements Baidu { + @Override + public void liyanhong() { + super.mayun(); + System.out.println("Baidu"); + } +} diff --git a/src/main/java/com/zeekling/adapter/readme.md b/src/main/java/com/zeekling/adapter/readme.md new file mode 100755 index 0000000..8bb10e6 --- /dev/null +++ b/src/main/java/com/zeekling/adapter/readme.md @@ -0,0 +1,49 @@ +# 适配器模式 +## 适配器模式有两种实现方式: +类适配器模式和对象适配器模式 +* 类适配器模式:就是让适配器角色继承源角色、实现目标接口。 +* 对象适配器模式:就是在目标接口的实现类中new一个源角色的对象,通过这个对象达到适配的效果。 +在tomcat中有好多地方都用到了适配器模式,在平时的开发中也遇到了适配器模式。 +## 类适配器模式 +![类适配器模式](http://www.zeekling.cn/gogs/zeek/designPattern/raw/master/pics/classAdapter.png)
+在上图中可以看出,Adaptee类并没有sampleOperation2()方法,而客户端则期待这个方法。为使客户端能够 +使用Adaptee类,提供一个中间环节,即类Adapter,把Adaptee的API与Target类的API衔接起来。Adapter与 +Adaptee是继承关系,这决定了这个适配器模式是类的。 +#### 目标(Target)角色 +这就是所期待得到的接口。注意:由于这里讨论的是类适配器模式,因此目标不可以是类。 +#### 源(Adapee)角色 +现在需要适配的接口。 +#### 适配器(Adaper)角色 +适配器类是本模式的核心。适配器把源接口转换成目标接口。显然,这一角色不可以是接口,而必须是具体类。 +## 对象适配器 +与类的适配器模式一样,对象的适配器模式把被适配的类的API转换成为目标类的API,与类的适配器模式不同 +的是,对象的适配器模式不是使用继承关系连接到Adaptee类,而是使用委派关系连接到Adaptee类。
+ +![对象适配器模式](http://www.zeekling.cn/gogs/zeek/designPattern/raw/master/pics/objectAdapter.png)
+ +从上图可以看出,Adaptee类并没有sampleOperation2()方法,而客户端则期待这个方法。为使客户端能够使用 +Adaptee类,需要提供一个包装Wrapper类Adapter。这个包装类包装了一个Adaptee的实例,从而此包装类能够 +把Adaptee的API与Target类的API衔接起来。Adapter与Adaptee是委派关系,这决定了适配器模式是对象的。 +## 优点 +* 将目标类和适配者类解耦,通过引入一个适配器类来重用现有的适配者类,而无须修改原有代码。 +* 增加了类的透明性和复用性,将具体的实现封装在适配者类中,对于客户端类来说是透明的,而且提高了适配者的复用性。 +* 灵活性和扩展性都非常好,通过使用配置文件,可以很方便地更换适配器,也可以在不修改原有代码的基础上增加新的适 +配器类,完全符合“开闭原则”。
+ +#### 类适配器模式还具有如下优点 +* 由于适配器类是适配者类的子类,因此可以在适配器类中置换一些适配者的方法,使得适配器的灵活性更强。 + +#### 对象适配器模式还具有如下优点 +* 一个对象适配器可以把多个不同的适配者适配到同一个目标,也就是说,同一个适配器可以把适配者类和它的子类都适配到目标接口。 + +## 缺点 + +#### 类适配器模式的缺点 +* 一次最多只能适配一个适配者类,而且目标抽象类只能为抽象类,不能为具体类,其使用有一定的局限性, +不能将一个适配者类和它的子类都适配到目标接口。 + +#### 对象适配器的缺点 +* 与类适配器模式相比,要想置换适配者类的方法就不容易。如果一定要置换掉适配者类的一个或多个方法, +就只好先做一个适配者类的子类,将适配者类的方法置换掉,然后再把适配者类的子类当做真正的适配者进 +行适配,实现过程较为复杂。 + diff --git a/src/main/java/com/zeekling/adapter/test/Test.java b/src/main/java/com/zeekling/adapter/test/Test.java new file mode 100755 index 0000000..2d0787b --- /dev/null +++ b/src/main/java/com/zeekling/adapter/test/Test.java @@ -0,0 +1,14 @@ +package adapter.adapter.test; + +import com.zeekling.adapter.Baidu; +import com.zeekling.adapter.BaiduImp; + +/** + * Created by lzh on 3/30/16. + */ +public class Test { + public static void main(String[] args) { + Baidu baidu = new BaiduImp(); + baidu.liyanhong(); + } +} diff --git a/src/main/java/com/zeekling/bridge/AbstractCar.java b/src/main/java/com/zeekling/bridge/AbstractCar.java new file mode 100755 index 0000000..0244a91 --- /dev/null +++ b/src/main/java/com/zeekling/bridge/AbstractCar.java @@ -0,0 +1,8 @@ +package com.zeekling.bridge; + +/** + * Created by lzh on 16-6-11. + */ +public abstract class AbstractCar { + public abstract void run(); +} diff --git a/src/main/java/com/zeekling/bridge/AbstractRoad.java b/src/main/java/com/zeekling/bridge/AbstractRoad.java new file mode 100755 index 0000000..bef0956 --- /dev/null +++ b/src/main/java/com/zeekling/bridge/AbstractRoad.java @@ -0,0 +1,16 @@ +package com.zeekling.bridge; + +/** + * Created by lzh on 16-6-11. + */ +public abstract class AbstractRoad { + private AbstractCar car = null; + public abstract void run(); + public void setCar(AbstractCar car){ + this.car = car; + } + + public AbstractCar getCar() { + return car; + } +} diff --git a/src/main/java/com/zeekling/bridge/Car.java b/src/main/java/com/zeekling/bridge/Car.java new file mode 100755 index 0000000..9941d1e --- /dev/null +++ b/src/main/java/com/zeekling/bridge/Car.java @@ -0,0 +1,11 @@ +package com.zeekling.bridge; + +/** + * Created by lzh on 16-6-11. + */ +public class Car extends AbstractCar { + @Override + public void run() { + System.out.println("car"); + } +} diff --git a/src/main/java/com/zeekling/bridge/Street.java b/src/main/java/com/zeekling/bridge/Street.java new file mode 100755 index 0000000..b11312d --- /dev/null +++ b/src/main/java/com/zeekling/bridge/Street.java @@ -0,0 +1,13 @@ +package com.zeekling.bridge; + +/** + * Created by lzh on 16-6-11. + */ +public class Street extends AbstractRoad { + + @Override + public void run() { + super.getCar().run(); + System.out.println(super.getCar().getClass().getName()+" run on street"); + } +} diff --git a/src/main/java/com/zeekling/bridge/readme.md b/src/main/java/com/zeekling/bridge/readme.md new file mode 100755 index 0000000..b4e4108 --- /dev/null +++ b/src/main/java/com/zeekling/bridge/readme.md @@ -0,0 +1,8 @@ +# 桥接模式: +## 简介 +Bridge 模式又叫做桥接模式,是构造型的设计模式之一。Bridge模式基于类的最小设计原则,通过使用 +封装,聚合以及继承等行为来让不同的类承担不同的责任。它的主要特点是把抽象(abstraction)与行 +为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。 +## 桥接模式结构 +![桥接模式结构](http://www.zeekling.cn/gogs/zeek/designPattern/raw/master/pics/bridge.jpg) + diff --git a/src/main/java/com/zeekling/bridge/test/Test.java b/src/main/java/com/zeekling/bridge/test/Test.java new file mode 100755 index 0000000..a9e2756 --- /dev/null +++ b/src/main/java/com/zeekling/bridge/test/Test.java @@ -0,0 +1,16 @@ +package bridge.bridge.test; + +import com.zeekling.bridge.AbstractRoad; +import com.zeekling.bridge.Car; +import com.zeekling.bridge.Street; + +/** + * Created by lzh on 16-6-11. + */ +public class Test { + public static void main(String[] args) { + AbstractRoad abstractRoad = new Street(); + abstractRoad.setCar(new Car()); + abstractRoad.run(); + } +} diff --git a/src/main/java/com/zeekling/builder/Ant.java b/src/main/java/com/zeekling/builder/Ant.java new file mode 100755 index 0000000..83103e5 --- /dev/null +++ b/src/main/java/com/zeekling/builder/Ant.java @@ -0,0 +1,35 @@ +package com.zeekling.builder; + + +/** + * Created by lzh on 3/28/16. + */ +public class Ant { + private String head; + private String body; + private String foot; + + public String getFoot() { + return foot; + } + + public void setFoot(String foot) { + this.foot = foot; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + public String getHead() { + return head; + } + + public void setHead(String head) { + this.head = head; + } +} diff --git a/src/main/java/com/zeekling/builder/AntBuilder.java b/src/main/java/com/zeekling/builder/AntBuilder.java new file mode 100755 index 0000000..f9255ff --- /dev/null +++ b/src/main/java/com/zeekling/builder/AntBuilder.java @@ -0,0 +1,15 @@ +package com.zeekling.builder; + +/** + * 创造蚂蚁需要的所有步骤以及获取蚂蚁 + */ +public interface AntBuilder { + + void buildHead(); + + void buildBody(); + + void buildFoot(); + + Ant getAnt(); +} diff --git a/src/main/java/com/zeekling/builder/AntBuilderImp.java b/src/main/java/com/zeekling/builder/AntBuilderImp.java new file mode 100755 index 0000000..141e97f --- /dev/null +++ b/src/main/java/com/zeekling/builder/AntBuilderImp.java @@ -0,0 +1,28 @@ +package com.zeekling.builder; + + +/** + * 创造蚂蚁需要的所有步骤以及获取蚂蚁 + */ +public class AntBuilderImp implements AntBuilder { + private Ant ant = new Ant(); + @Override + public void buildHead() { + ant.setHead("head"); + } + + @Override + public void buildBody() { + ant.setBody("body"); + } + + @Override + public void buildFoot() { + ant.setFoot("foot"); + } + + @Override + public Ant getAnt() { + return ant; + } +} diff --git a/src/main/java/com/zeekling/builder/Director.java b/src/main/java/com/zeekling/builder/Director.java new file mode 100755 index 0000000..6897ab3 --- /dev/null +++ b/src/main/java/com/zeekling/builder/Director.java @@ -0,0 +1,25 @@ +package com.zeekling.builder; + + +/** + * 指挥创建蚂蚁 + */ +public class Director { + + private AntBuilder builder; + + public Director(AntBuilder builder){ + this.builder = builder; + } + + /** + * 按照指定的顺序创建蚂蚁 + * @return 创建得到的蚂蚁 + */ + public Ant buildAnt(){ + builder.buildHead(); + builder.buildBody(); + builder.buildFoot(); + return builder.getAnt(); + } +} diff --git a/src/main/java/com/zeekling/builder/readme.md b/src/main/java/com/zeekling/builder/readme.md new file mode 100644 index 0000000..a3679f0 --- /dev/null +++ b/src/main/java/com/zeekling/builder/readme.md @@ -0,0 +1,24 @@ +# 建造者模式 +## 简介 +建造者模式(com.zeekling.builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式, +它提供了一种创建对象的最佳方式。 +## 主要解决 +主要解决在软件系统中,有时候面临着"一个复杂对象"的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求 +的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。 +## 建造者的结构 +![建造者模式](http://www.zeekling.cn/gogs/zeek/designPattern/raw/master/pics/builder.png) + +## 抽象建造者(com.zeekling.builder)角色 +给出一个抽象接口,以规范产品对象的各个组成成分的建造。一般而言,此接口独立于应用程序的商业逻辑。模式中直接创建 +产品对象的是具体建造者 ConcreteBuilder角色。具体建造者类必须实现这个接口所要求的两种方法:一种是建造方法 +buildPart1和 buildPart2,另一种是返还结构方法retrieveResult。一般来说,产品所包含的零件数目与建造方法的数目相符。 +换言之,有多少 零件,就有多少相应的建造方法。 +## 具体建造者(ConcreteBuilder)角色 +担任这个角色的是与应用程序紧密相关的一些类,它们在应用程序调用下创建产品的实例。这个角色要完成的任务包括: +1.实现抽象建造者Builder所声明的接口,给出一步一步地完成创建产品实例的操作。2.在建造过程完成后,提供产品的实例。 +## 导演者(Director)角色 +担任这个角色的类调用具体建造者角色以创建产品对象。应当指出的是,导演者角色并没有产品类的具体知识,真正拥有产品 +类的具体知识的是具体建造者角色。 +## 产品(Product)角色 +产品便是建造中的复杂对象。一般来说,一个系统中会有多于一个的产品类,而且这些产品类并不一定有共同的接口,而完全 +可以是不相关联的。 diff --git a/src/main/java/com/zeekling/builder/test/Test.java b/src/main/java/com/zeekling/builder/test/Test.java new file mode 100755 index 0000000..d6b3816 --- /dev/null +++ b/src/main/java/com/zeekling/builder/test/Test.java @@ -0,0 +1,21 @@ +package Builder.builder.test; + + +import com.zeekling.builder.Ant; +import com.zeekling.builder.AntBuilder; +import com.zeekling.builder.AntBuilderImp; +import com.zeekling.builder.Director; + +/** + * 建造者模式 + */ +public class Test { + public static void main(String[] args) { + AntBuilder builder = new AntBuilderImp(); + Director director = new Director(builder); + System.out.println("begin build......."); + Ant ant = director.buildAnt(); + System.out.println("build end"); + System.out.println("head=" + ant.getHead()+",body="+ant.getBody()+",foot="+ant.getFoot()); + } +} diff --git a/src/main/java/com/zeekling/command/Command.java b/src/main/java/com/zeekling/command/Command.java new file mode 100755 index 0000000..583fe4e --- /dev/null +++ b/src/main/java/com/zeekling/command/Command.java @@ -0,0 +1,11 @@ +package com.zeekling.command; + +/** + * + * 命令模式 + */ +public interface Command { + + void execute(); + +} diff --git a/src/main/java/com/zeekling/command/Eat.java b/src/main/java/com/zeekling/command/Eat.java new file mode 100755 index 0000000..f081f07 --- /dev/null +++ b/src/main/java/com/zeekling/command/Eat.java @@ -0,0 +1,18 @@ +package com.zeekling.command; + +/** + * 具体命令。 + */ +public class Eat implements Command { + + private Person person; + + public Eat(Person person){ + this.person = person; + } + + @Override + public void execute() { + person.action("eat"); + } +} diff --git a/src/main/java/com/zeekling/command/Invoker.java b/src/main/java/com/zeekling/command/Invoker.java new file mode 100644 index 0000000..c2801eb --- /dev/null +++ b/src/main/java/com/zeekling/command/Invoker.java @@ -0,0 +1,32 @@ +package com.zeekling.command; + +/** + * @author 令照辉 [zeekling@163.com] + * @version 1.0 + * @apiNote + * @since 2018-05-21 + */ +public class Invoker { + + private Command command; + + public Invoker(){} + + public Invoker(Command command){ + this.command = command; + } + + public void call(){ + if (command != null){ + command.execute(); + } + } + + public Command getCommand() { + return command; + } + + public void setCommand(Command command) { + this.command = command; + } +} diff --git a/src/main/java/com/zeekling/command/Person.java b/src/main/java/com/zeekling/command/Person.java new file mode 100644 index 0000000..c0baf2f --- /dev/null +++ b/src/main/java/com/zeekling/command/Person.java @@ -0,0 +1,30 @@ +package com.zeekling.command; + +/** + * @author 令照辉 [zeekling@163.com] + * @version 1.0 + * @apiNote + * @since 2018-05-21 + */ +public class Person { + + private String name; + + public Person(){} + + public Person(String name){ + this.name = name; + } + + public void action(String action){ + System.out.println(name + ":" + action); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/com/zeekling/command/Sleep.java b/src/main/java/com/zeekling/command/Sleep.java new file mode 100755 index 0000000..78ac775 --- /dev/null +++ b/src/main/java/com/zeekling/command/Sleep.java @@ -0,0 +1,18 @@ +package com.zeekling.command; + +/** + * Created by lzh on 4/1/16. + */ +public class Sleep implements Command { + + private Person person; + + public Sleep(Person person){ + this.person = person; + } + + @Override + public void execute() { + person.action("sleep"); + } +} diff --git a/src/main/java/com/zeekling/command/Walk.java b/src/main/java/com/zeekling/command/Walk.java new file mode 100755 index 0000000..7573532 --- /dev/null +++ b/src/main/java/com/zeekling/command/Walk.java @@ -0,0 +1,17 @@ +package com.zeekling.command; + +/** + * Created by lzh on 4/1/16. + */ +public class Walk implements Command { + private Person person; + + public Walk(Person person) { + this.person = person; + } + + @Override + public void execute() { + person.action("walk"); + } +} diff --git a/src/main/java/com/zeekling/command/readme.md b/src/main/java/com/zeekling/command/readme.md new file mode 100755 index 0000000..d383e42 --- /dev/null +++ b/src/main/java/com/zeekling/command/readme.md @@ -0,0 +1,34 @@ +# 命令模式 +又称Action模式或者Transaction模式。它属于对象的行为模式。命令模式把一个请求或者操作封装到一个对象中。命令模式 +允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和撤销功能。 + +## 主要角色 + +#### 抽象命令(Command)角色 +声明执行操作的接口。 +#### 具体命令(ConcreteCommand)角色 +将一个接收者对象绑定到一个动作上。调用接收者相应的操作,以实现Execute方法。 +#### 客户端(Client)角色 +创建一个命令对象并设定它的接收者。 +#### 请求者(Invoker)角色 +负责调用命令对象的执行请求; +#### 接收者(Receiver)角色 +负责具体实施和执行一个请求相关的操作。任何一个类都可以作为一个接收者。 + +## 命令模式结构图 + +![命令模式](http://www.zeekling.cn/gogs/zeek/designPattern/raw/master/pics/command.jpg) + +## 命令模式的优点 +1、在命令者模式中,请求者不直接与接受者互交,既请求者不包含接受者的引用,因此彻底消除了彼此之间的耦合。 +2、命令者模式满足了软件的“开-闭原则”。如果增加新的具体命令和该命令的接受者,不必修改调用者的代码,调用者就可以 +直接使用新的命令对象。反之如果增加新的调用者,不必修改现有的具体命令和接受者。新增加的调用者就可以使用已有的具 +体命令。 +3、由于请求者的请求被封装到了具体命令中,那么就可以将具体命令保存到持久化媒介中,在需要的时候重新执行这个具体 +命令。因此使用命令者模式可以记录日志。 +4、使用命令者模式可以对请求者的请求进行排队,每个请求者各自对应一个具体命令,因此可以按一定的顺序执行这些命令。 +## 命令模式应用场景 +1、程序需要在不同的时刻指定,排列和执行请求。 +2、程序需要提供撤销操作 。 +3、程序需要支持宏操作 。 + diff --git a/src/main/java/com/zeekling/command/test/Test.java b/src/main/java/com/zeekling/command/test/Test.java new file mode 100755 index 0000000..074334c --- /dev/null +++ b/src/main/java/com/zeekling/command/test/Test.java @@ -0,0 +1,23 @@ +package com.zeekling.command.test; + + +import com.zeekling.command.*; + +/** + * Created by lzh on 3/31/16. + */ +public class Test { + public static void main(String[] args) { + Person person = new Person("令照辉"); + Invoker invoker = new Invoker(); + Command command = new Eat(person); + invoker.setCommand(command); + invoker.call(); + command = new Walk(person); + invoker.setCommand(command); + invoker.call(); + command = new Sleep(person); + invoker.setCommand(command); + invoker.call(); + } +} diff --git a/src/main/java/com/zeekling/composite/Component.java b/src/main/java/com/zeekling/composite/Component.java new file mode 100755 index 0000000..cfb5457 --- /dev/null +++ b/src/main/java/com/zeekling/composite/Component.java @@ -0,0 +1,26 @@ +package com.zeekling.composite; + +/** + * Created by lzh on 3/30/16. + * 组合模式。 + */ +public abstract class Component { + + String msg; + + public Component(String msg){ + this.msg = msg; + } + + public void add(Component component){ + + } + + public void remove(Component component){ + + } + + public void display(){ + + } +} diff --git a/src/main/java/com/zeekling/composite/Composite.java b/src/main/java/com/zeekling/composite/Composite.java new file mode 100755 index 0000000..c67d787 --- /dev/null +++ b/src/main/java/com/zeekling/composite/Composite.java @@ -0,0 +1,34 @@ +package com.zeekling.composite; +import java.util.ArrayList; + +/** + * Created by lzh on 3/30/16. + */ +public class Composite extends Component { + private ArrayList components = new ArrayList<>(); + public Composite(String msg) { + super(msg); + } + + @Override + public void add(Component component) { + super.add(component); + components.add(component); + } + + @Override + public void remove(Component component) { + super.remove(component); + if(components.contains(component)){ + components.remove(component); + } + } + + @Override + public void display() { + super.display(); + for (Component c:components) { + System.out.println(c.msg); + } + } +} diff --git a/src/main/java/com/zeekling/composite/Part.java b/src/main/java/com/zeekling/composite/Part.java new file mode 100755 index 0000000..b1f20b8 --- /dev/null +++ b/src/main/java/com/zeekling/composite/Part.java @@ -0,0 +1,11 @@ +package com.zeekling.composite; + +/** + * Created by lzh on 3/30/16. + */ +public class Part extends Component { + + public Part(String msg) { + super(msg); + } +} diff --git a/src/main/java/com/zeekling/composite/readme.md b/src/main/java/com/zeekling/composite/readme.md new file mode 100755 index 0000000..1cd7573 --- /dev/null +++ b/src/main/java/com/zeekling/composite/readme.md @@ -0,0 +1,34 @@ +# 组合模式 +## 简介 +组合模式对单个对象(叶子对象)和组合对象(组合对象)具有一致性,它将对象组织到树结构中,可以用来描述整体与部分的关 +系。同时它也模糊了简单元素(叶子对象)和复杂元素(容器对象)的概念,使得客户能够像处理简单元素一样来处理复杂元素, +从而使客户程序能够与复杂元素的内部结构解耦。组合模式使用面向对象的思想来实现树形结构的构建与处理,描述了如何将 +容器对象和叶子对象进行递归组合,实现简单,灵活性好。由于在软件开发中存在大量的树形结构,因此组合模式是一种使用 +频率较高的结构型设计模式,Java SE 中的 AWT 和 Swing包的设计就基于组合模式,在这些界面包中为用户提供了大量的容 +器构件(如Container)和成员构件(如 Checkbox、Button 和 TextComponent等)。 + +## 结构 + +![组合模式](https://github.com/lzh984294471/designPattern/raw/master/pics/composite.jpg) + +## 组合模式的优点 + +(1) 组合模式可以清楚地定义分层次的复杂对象,表示对象的全部或部分层次,它让客户端忽略了层次的差异,方便对整个层 +次结构进行控制。 +(2) 客户端可以一致地使用一个组合结构或其中单个对象,不必关心处理的是单个对象还是整个组合结构,简化了客户端代码。 +(3) 在组合模式中增加新的容器构件和叶子构件都很方便,无须对现有类库进行任何修改,符合“开闭原则”。 +(4) 组合模式为树形结构的面向对象实现提供了一种灵活的解决方案,通过叶子对象和容器对象的递归组合,可以形成复杂的 +树形结构,但对树形结构的控制却非常简单。 + +## 组合模式的缺点 + +在增加新构件时很难对容器中的构件类型进行限制。有时候我们希望一个容器中只能有某些特定类型的对象,例如在某个文件 +夹中只能包含文本文件,使用组合模式时,不能依赖类型系统来施加这些约束,因为它们都来自于相同的抽象层,在这种情况 +下,必须通过在运行时进行类型检查来实现,这个实现过程较为复杂。 + +## 适用场景 + +(1) 在具有整体和部分的层次结构中,希望通过一种方式忽略整体与部分的差异,客户端可以一致地对待它们。 +(2) 在一个使用面向对象语言开发的系统中需要处理一个树形结构。 +(3) 在一个系统中能够分离出叶子对象和容器对象,而且它们的类型不固定,需要增加一些新的类型。 + diff --git a/src/main/java/com/zeekling/composite/test/Test.java b/src/main/java/com/zeekling/composite/test/Test.java new file mode 100755 index 0000000..2ff949a --- /dev/null +++ b/src/main/java/com/zeekling/composite/test/Test.java @@ -0,0 +1,20 @@ +package composite.composite.test; + +import com.zeekling.composite.Component; +import com.zeekling.composite.Composite; +import com.zeekling.composite.Part; + +/** + * 组合模式 + */ +public class Test { + + public static void main(String[] args) { + Component composite = new Composite("composite"); + Component one = new Part("part one"); + Component two = new Part("part two"); + composite.add(one); + composite.add(two); + composite.display(); + } +} diff --git a/src/main/java/com/zeekling/decorate/Change.java b/src/main/java/com/zeekling/decorate/Change.java new file mode 100755 index 0000000..89a190b --- /dev/null +++ b/src/main/java/com/zeekling/decorate/Change.java @@ -0,0 +1,18 @@ +package com.zeekling.decorate; + +/** + * Created by lzh on 3/29/16. + */ +public class Change implements Decorate{ + + private Decorate decorate; + + public Change(Decorate decorate){ + this.decorate = decorate; + } + + @Override + public void act() { + decorate.act(); + } +} diff --git a/src/main/java/com/zeekling/decorate/ChangeOne.java b/src/main/java/com/zeekling/decorate/ChangeOne.java new file mode 100755 index 0000000..5327704 --- /dev/null +++ b/src/main/java/com/zeekling/decorate/ChangeOne.java @@ -0,0 +1,20 @@ +package com.zeekling.decorate; + +/** + * Created by lzh on 3/29/16. + */ +public class ChangeOne extends Change { + + public ChangeOne(Decorate decorate) { + super(decorate); + } + @Override + public void act() { + super.act(); + say(); + System.out.println("ChangeOne"); + } + public void say(){ + System.out.println("say"); + } +} diff --git a/src/main/java/com/zeekling/decorate/ChangeTwo.java b/src/main/java/com/zeekling/decorate/ChangeTwo.java new file mode 100755 index 0000000..270c65a --- /dev/null +++ b/src/main/java/com/zeekling/decorate/ChangeTwo.java @@ -0,0 +1,22 @@ +package com.zeekling.decorate; + +/** + * Created by lzh on 3/29/16. + */ +public class ChangeTwo extends Change { + + public ChangeTwo(Decorate decorate) { + super(decorate); + } + + @Override + public void act() { + super.act(); + eat(); + System.out.println("changeTwo"); + } + + public void eat(){ + System.out.println("eat"); + } +} diff --git a/src/main/java/com/zeekling/decorate/Decorate.java b/src/main/java/com/zeekling/decorate/Decorate.java new file mode 100755 index 0000000..68c0d85 --- /dev/null +++ b/src/main/java/com/zeekling/decorate/Decorate.java @@ -0,0 +1,10 @@ +package com.zeekling.decorate; + +/** + * Created by lzh on 3/29/16. + */ +public interface Decorate { + + void act(); + +} diff --git a/src/main/java/com/zeekling/decorate/DecorateObject.java b/src/main/java/com/zeekling/decorate/DecorateObject.java new file mode 100755 index 0000000..e43d9b8 --- /dev/null +++ b/src/main/java/com/zeekling/decorate/DecorateObject.java @@ -0,0 +1,11 @@ + +package com.zeekling.decorate; +/** + * Created by lzh on 3/29/16. + */ +public class DecorateObject implements Decorate { + @Override + public void act() { + System.out.println("猜猜我是谁"); + } +} diff --git a/src/main/java/com/zeekling/decorate/readme.md b/src/main/java/com/zeekling/decorate/readme.md new file mode 100755 index 0000000..96e0778 --- /dev/null +++ b/src/main/java/com/zeekling/decorate/readme.md @@ -0,0 +1,15 @@ +# 装饰器模式 +## 简介 + +装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任。换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不使用创造更多子类的情况下,将对象的功能加以扩展。 + +## 装饰器模式类图 +![装饰器模式类图](http://www.zeekling.cn/gogs/zeek/designPattern/raw/master/pics/decorator.png) + +## 主要角色 +* 抽象构建角色:给出一个抽象接口,以规范准备接受附加责任的对象。 +* 具体构件角色:定义一个将要接受附加责任的类。 +* 装饰角色:持有一个构件对象的实例,并定义一个与抽象构件接口一致的接口。 +* 具体装饰器角色:负责给构件对象“贴上”附加的责任。 + + diff --git a/src/main/java/com/zeekling/decorate/test/Test.java b/src/main/java/com/zeekling/decorate/test/Test.java new file mode 100755 index 0000000..eec9b96 --- /dev/null +++ b/src/main/java/com/zeekling/decorate/test/Test.java @@ -0,0 +1,19 @@ +package decorate.decorate.test; + +import com.zeekling.decorate.ChangeOne; +import com.zeekling.decorate.ChangeTwo; +import com.zeekling.decorate.Decorate; +import com.zeekling.decorate.DecorateObject; + +/** + * Created by lzh on 3/29/16. + */ +public class Test { + public static void main(String[] args) { + Decorate decorate = new DecorateObject(); + Decorate changeOne = new ChangeOne(decorate); + Decorate changeTwo = new ChangeTwo(decorate); + changeOne.act(); + changeTwo.act(); + } +} diff --git a/src/main/java/com/zeekling/facade/Baidu.java b/src/main/java/com/zeekling/facade/Baidu.java new file mode 100755 index 0000000..e0b1d80 --- /dev/null +++ b/src/main/java/com/zeekling/facade/Baidu.java @@ -0,0 +1,9 @@ +package com.zeekling.facade; + +/** + * Created by lzh on 3/29/16. + */ +public interface Baidu { + void computerShutdown(); + void searchSomething(); +} diff --git a/src/main/java/com/zeekling/facade/BaiduImp.java b/src/main/java/com/zeekling/facade/BaiduImp.java new file mode 100755 index 0000000..d24b788 --- /dev/null +++ b/src/main/java/com/zeekling/facade/BaiduImp.java @@ -0,0 +1,19 @@ +package com.zeekling.facade; + +/** + * Created by lzh on 3/29/16. + */ +public class BaiduImp implements Baidu { + @Override + public void computerShutdown() { + Computer computer = new ComputerImp(); + computer.shutdown(); + + } + + @Override + public void searchSomething() { + Search search = new SearchImp(); + search.search(); + } +} diff --git a/src/main/java/com/zeekling/facade/Computer.java b/src/main/java/com/zeekling/facade/Computer.java new file mode 100755 index 0000000..eaf4ea2 --- /dev/null +++ b/src/main/java/com/zeekling/facade/Computer.java @@ -0,0 +1,8 @@ +package com.zeekling.facade; + +/** + * Created by lzh on 3/29/16. + */ +public interface Computer { + void shutdown(); +} diff --git a/src/main/java/com/zeekling/facade/ComputerImp.java b/src/main/java/com/zeekling/facade/ComputerImp.java new file mode 100755 index 0000000..2b2c630 --- /dev/null +++ b/src/main/java/com/zeekling/facade/ComputerImp.java @@ -0,0 +1,11 @@ + +package com.zeekling.facade; +/** + * Created by lzh on 3/29/16. + */ +public class ComputerImp implements Computer { + @Override + public void shutdown() { + System.out.println("computer closed "); + } +} diff --git a/src/main/java/com/zeekling/facade/Search.java b/src/main/java/com/zeekling/facade/Search.java new file mode 100755 index 0000000..dd9307a --- /dev/null +++ b/src/main/java/com/zeekling/facade/Search.java @@ -0,0 +1,8 @@ + +package com.zeekling.facade; +/** + * Created by lzh on 3/29/16. + */ +public interface Search { + void search(); +} diff --git a/src/main/java/com/zeekling/facade/SearchImp.java b/src/main/java/com/zeekling/facade/SearchImp.java new file mode 100755 index 0000000..1722620 --- /dev/null +++ b/src/main/java/com/zeekling/facade/SearchImp.java @@ -0,0 +1,11 @@ +package com.zeekling.facade; + +/** + * Created by lzh on 3/29/16. + */ +public class SearchImp implements Search { + @Override + public void search() { + System.out.println("search something "); + } +} diff --git a/src/main/java/com/zeekling/facade/readme.md b/src/main/java/com/zeekling/facade/readme.md new file mode 100755 index 0000000..ba60d95 --- /dev/null +++ b/src/main/java/com/zeekling/facade/readme.md @@ -0,0 +1,23 @@ +# 外观模式 +## 简介 +外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。外观模式又称为门面模式,它是一种对象结构型模式。 +## 模式结构 + +![外观模式](https://github.com/lzh984294471/designPattern/raw/master/pics/facade.jpg) +## 优点 +* 对客户屏蔽子系统组件,减少了客户处理的对象数目并使得子系统使用起来更加容易。通过引入外观模式,客户代码将变得很简单,与之关联的对象也很少。 +* 实现了子系统与客户之间的松耦合关系,这使得子系统的组件变化不会影响到调用它的客户类,只需要调整外观类即可。 +* 降低了大型软件系统中的编译依赖性,并简化了系统在不同平台之间的移植过程,因为编译一个子系统一般不需要编译所有其他的子系统。一个子系统的修改对其他子系统没有任何影响,而且子系统内部变化也不会影响到外观对象。 +* 只是提供了一个访问子系统的统一入口,并不影响用户直接使用子系统类。 + +## 缺点 +* 不能很好地限制客户使用子系统类,如果对客户访问子系统类做太多的限制则减少了可变性和灵活性。 +* 在不引入抽象外观类的情况下,增加新的子系统可能需要修改外观类或客户端的源代码,违背了“开闭原则”。 + +## 使用环境 +* 当要为一个复杂子系统提供一个简单接口时可以使用外观模式。该接口可以满足大多数用户的需求,而且用户也可以越过外观类直接访问子系统。 +* 客户程序与多个子系统之间存在很大的依赖性。引入外观类将子系统与客户以及其他子系统解耦,可以提高子系统的独立性和可移植性。 +* 在层次化结构中,可以使用外观模式定义系统中每一层的入口,层与层之间不直接产生联系,而通过外观类建立联系,降低层之间的耦合度。 + +## 应用 + diff --git a/src/main/java/com/zeekling/facade/test/Test.java b/src/main/java/com/zeekling/facade/test/Test.java new file mode 100755 index 0000000..2586cee --- /dev/null +++ b/src/main/java/com/zeekling/facade/test/Test.java @@ -0,0 +1,19 @@ +package facade.facade.test; + +import com.zeekling.facade.Baidu; +import com.zeekling.facade.BaiduImp; + +/** + * Created by lzh on 3/29/16. + */ +public class Test { + /** + * 外观模式 + * @param args + */ + public static void main(String[] args) { + Baidu baidu = new BaiduImp(); + baidu.computerShutdown(); + baidu.searchSomething(); + } +} diff --git a/src/main/java/com/zeekling/flyweight/ConcreteFlyweight.java b/src/main/java/com/zeekling/flyweight/ConcreteFlyweight.java new file mode 100755 index 0000000..82c2c84 --- /dev/null +++ b/src/main/java/com/zeekling/flyweight/ConcreteFlyweight.java @@ -0,0 +1,27 @@ +package com.zeekling.flyweight; + +/** + * Created by lzh on 3/30/16. + * 具体享元 + */ +public class ConcreteFlyweight implements Flyweight{ + String in; + + /** + * + * @param in 内部享元 + */ + public ConcreteFlyweight(String in){ + this.in = in; + } + + /** + * + * @param out 外部享元 + */ + @Override + public void say(String out) { + System.out.println(out+": i am out "); + System.out.println(in+":i am in "); + } +} diff --git a/src/main/java/com/zeekling/flyweight/Flyweight.java b/src/main/java/com/zeekling/flyweight/Flyweight.java new file mode 100755 index 0000000..62c1d96 --- /dev/null +++ b/src/main/java/com/zeekling/flyweight/Flyweight.java @@ -0,0 +1,9 @@ + +package com.zeekling.flyweight; +/** + * Created by lzh on 3/30/16. + * 具体享元 + */ +public interface Flyweight { + void say(String out); +} diff --git a/src/main/java/com/zeekling/flyweight/FlyweightFactory.java b/src/main/java/com/zeekling/flyweight/FlyweightFactory.java new file mode 100755 index 0000000..ef0cd3b --- /dev/null +++ b/src/main/java/com/zeekling/flyweight/FlyweightFactory.java @@ -0,0 +1,21 @@ +package com.zeekling.flyweight; + +import java.util.Hashtable; + +/** + * Created by lzh on 3/30/16. + * 享元工厂 + */ +public class FlyweightFactory { + private static Hashtable flyweights = new Hashtable<>(); + public static Flyweight factory(String in){ + Flyweight flyweight ; + flyweight = flyweights.get(in); + if(null == flyweight){ + flyweight = new ConcreteFlyweight(in); + flyweights.put(in,flyweight); + } + return flyweight; + } + +} diff --git a/src/main/java/com/zeekling/flyweight/readme.md b/src/main/java/com/zeekling/flyweight/readme.md new file mode 100755 index 0000000..bf3d09f --- /dev/null +++ b/src/main/java/com/zeekling/flyweight/readme.md @@ -0,0 +1,38 @@ +# 享元模式 +## 简述 +享元模式采用一个共享来避免大量拥有相同内容对象的开销。这种开销最常见、最直观的就是内容的消耗。享元对象能够做到共享的关键是区分内蕴态和外蕴态。
+一个内蕴状态是存储在享元对象内部的,并且是不会随着环境的改变而有所不同。因此,一个享元可以具有内蕴状态并可以共享。
+一个外蕴状态是随着环境的改变的、不可共享的。享元对象的外蕴状态必须有客户端保存,并在享元对象被创建之后,在需要使用的时候在传入享元对象内部。 +外蕴状态不可以影响到享元对象的内蕴状态,他们是相互独立的
+享元模式可以分为单纯享元模式和复合享元模式。
+ +## 单纯享元模式 +在单纯享元模式中,所有的对象都是可以共享。
+ +![单纯享元模式](https://github.com/lzh984294471/designPattern/raw/master/pics/flyweight1.png) + +### 单纯享元模式中所涉及到的角色: + +* 抽象享元角色:给出一个抽象接口,以规定所有具体的享元角色需要实现的方法。 +* 具体享元角色:实现抽象享元角色所规定出的接口。如果有内蕴状态的话,必须负责为内蕴状态提供存储空间。 +* 享元工厂角色:本角色负责创建和管理享元角色。本角色必须保证享元对象可以被系统适当的共享。当一个客户端对象调用一个享元对象的时候,享元工厂角色会 +检查系统中是否已经存在有一个符合要求的享元对象。如果已经有了,享元工厂角色就应当提供这个已有的享元对象;如果系统中没有一个合适的享元对象的话,享 +元工厂角色就应当创建一个合适的享元角色。 + +## 复合享元模式 + +### 简述 +在单纯享元模式中,所有的享元对象都是单纯享元对象,也就是都可以直接共享的。还有一种较为复杂的情况,将一些单纯享元使用合成模式加以复用,形成复合享 +元对象。这样的复合享元对象本身不能共享,但是他们可以分解成单纯享元对象,而后者则可以共享。
+ +![复合享元模式](https://github.com/lzh984294471/designPattern/raw/master/pics/flyweight2.png)
+ +### 复合享元角色所涉及的角色: + +* 抽象享元角色:给出一个抽象接口,以规定出所有具体享元角色需要实现的方法。 +* 实现抽象享元角色所规定出的接口。如果有内蕴状态的话,必须负责为内蕴状态提供存储空间。 +* 复合享元角色所代表的对象是不可以共享的,但是一个复合享元对象可以分解成为多个本身是单纯享元对象的组合。复合享元角色又称作不可共享的享元对象。 +* 本角色负责创建和管理享元角色。本角色必须保证享元对象可以被系统适当地共享。当一个客户端对象调用一个享元对象的时候,享元工厂角色会检查系统中是否 +已经有 一个符合要求的享元对象。如果已经有了,享元工厂角色就应当提供这个已有的享元对象;如果系统中没有一个适当的享元对象的话,享元工厂角色就应当创 +建一个 合适的享元对象。 + diff --git a/src/main/java/com/zeekling/flyweight/test/Test.java b/src/main/java/com/zeekling/flyweight/test/Test.java new file mode 100755 index 0000000..910fc83 --- /dev/null +++ b/src/main/java/com/zeekling/flyweight/test/Test.java @@ -0,0 +1,15 @@ +package flyweight.flyweight.test; + +import com.zeekling.flyweight.Flyweight; +import com.zeekling.flyweight.FlyweightFactory; + +/** + * Created by lzh on 3/30/16. + */ +public class Test { + public static void main(String[] args) { + Flyweight flyweight = FlyweightFactory.factory("lzh"); + flyweight.say("out"); + + } +} diff --git a/src/main/java/com/zeekling/handler/ConcreteHandler.java b/src/main/java/com/zeekling/handler/ConcreteHandler.java new file mode 100755 index 0000000..3b462b2 --- /dev/null +++ b/src/main/java/com/zeekling/handler/ConcreteHandler.java @@ -0,0 +1,16 @@ +package com.zeekling.handler; + +/** + * Created by lzh on 3/31/16. + */ +public class ConcreteHandler extends Handler { + @Override + public void requestHandler() { + if(getHandler() != null){ + System.out.println("let off request"); + getHandler().requestHandler(); + }else { + System.out.println("deal with request"); + } + } +} diff --git a/src/main/java/com/zeekling/handler/Handler.java b/src/main/java/com/zeekling/handler/Handler.java new file mode 100755 index 0000000..134f0d8 --- /dev/null +++ b/src/main/java/com/zeekling/handler/Handler.java @@ -0,0 +1,18 @@ +package com.zeekling.handler; + +/** + * Created by lzh on 3/31/16. + * 责任链模式 + */ +public abstract class Handler { + private Handler handler; + public abstract void requestHandler(); + + public void setHandler(Handler handler) { + this.handler = handler; + } + + public Handler getHandler() { + return handler; + } +} diff --git a/src/main/java/com/zeekling/handler/readme.md b/src/main/java/com/zeekling/handler/readme.md new file mode 100755 index 0000000..fa0d565 --- /dev/null +++ b/src/main/java/com/zeekling/handler/readme.md @@ -0,0 +1,13 @@ +# 责任链模式 +## 简述 +使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系,将这个对象连成一条链,并沿着这条链传递该请求, +直到有一个对象处理他为止。 +## 角色 +### 抽象处理者角色(Handler) +定义出一个处理请求的接口。如果需要,接口可以定义 出一个方法以设定和返回对下家的引用。这个角色通常由一个Java抽象类或者Java接口实现。 +### 具体处理者角色(ConcreteHandler) +具体处理者接到请求后,可以选择将请求处理掉,或者将请求传给下家。由于具体处理者持有对下家的引用,因此,如果需要,具体处理者可以访问下家。 +## 类图 +![责任链模式](https://github.com/lzh984294471/designPattern/raw/master/pics/handler.png)
+ + diff --git a/src/main/java/com/zeekling/handler/test/Test.java b/src/main/java/com/zeekling/handler/test/Test.java new file mode 100755 index 0000000..b4f6310 --- /dev/null +++ b/src/main/java/com/zeekling/handler/test/Test.java @@ -0,0 +1,16 @@ +package handler.handler.test; + +import com.zeekling.handler.ConcreteHandler; +import com.zeekling.handler.Handler; + +/** + * Created by lzh on 3/31/16. + */ +public class Test { + public static void main(String[] args) { + Handler handler1 = new ConcreteHandler(); + Handler handler2 = new ConcreteHandler(); + handler1.setHandler(handler2); + handler1.requestHandler(); + } +} diff --git a/src/main/java/com/zeekling/interpreter/readme.md b/src/main/java/com/zeekling/interpreter/readme.md new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/com/zeekling/iterator/Aggregate.java b/src/main/java/com/zeekling/iterator/Aggregate.java new file mode 100755 index 0000000..3eabad0 --- /dev/null +++ b/src/main/java/com/zeekling/iterator/Aggregate.java @@ -0,0 +1,7 @@ +package com.zeekling.iterator; +/** + * Created by lzh on 3/31/16. + */ +public abstract class Aggregate { + public abstract Iterator createIterator(); +} diff --git a/src/main/java/com/zeekling/iterator/AggregateImp.java b/src/main/java/com/zeekling/iterator/AggregateImp.java new file mode 100755 index 0000000..a7fd4fe --- /dev/null +++ b/src/main/java/com/zeekling/iterator/AggregateImp.java @@ -0,0 +1,25 @@ +package com.zeekling.iterator; + +/** + * Created by lzh on 3/31/16. + */ +public class AggregateImp extends Aggregate { + private Object[] objects = null; + public AggregateImp(Object[] objects){ + this.objects = objects; + } + public Object getElement(int index){ + if(index < objects.length){ + return objects[index]; + }else { + return null; + } + } + @Override + public Iterator createIterator() { + return new IteratorImp(this); + } + public int getSize(){ + return objects.length; + } +} diff --git a/src/main/java/com/zeekling/iterator/Iterator.java b/src/main/java/com/zeekling/iterator/Iterator.java new file mode 100755 index 0000000..d13ac46 --- /dev/null +++ b/src/main/java/com/zeekling/iterator/Iterator.java @@ -0,0 +1,11 @@ +package com.zeekling.iterator; + +/** + * Created by lzh on 3/31/16. + */ +public interface Iterator { + void next(); + boolean isLast(); + void first(); + Object current(); +} diff --git a/src/main/java/com/zeekling/iterator/IteratorImp.java b/src/main/java/com/zeekling/iterator/IteratorImp.java new file mode 100755 index 0000000..a9b2865 --- /dev/null +++ b/src/main/java/com/zeekling/iterator/IteratorImp.java @@ -0,0 +1,36 @@ +package com.zeekling.iterator; + +/** + * Created by lzh on 3/31/16. + */ +public class IteratorImp implements Iterator { + private AggregateImp aggregateImp = null; + private int size = 0; + private int index = 0; + public IteratorImp(AggregateImp aggregateImp){ + this.aggregateImp = aggregateImp; + this.size = aggregateImp.getSize(); + index = 0; + } + @Override + public void next() { + if(index < size){ + index ++; + } + } + + @Override + public boolean isLast() { + return index>=size; + } + + @Override + public void first() { + index = 0; + } + + @Override + public Object current() { + return aggregateImp.getElement(index); + } +} diff --git a/src/main/java/com/zeekling/iterator/readme.md b/src/main/java/com/zeekling/iterator/readme.md new file mode 100755 index 0000000..61f2862 --- /dev/null +++ b/src/main/java/com/zeekling/iterator/readme.md @@ -0,0 +1,32 @@ +# 迭代子模式 +## 简述 +迭代子模式可以顺序访问一个聚集中的元素而不必暴露聚集的内部表象。
+迭代子可分为外禀迭代子和内禀迭代子。
+迭代子模式有两种实现方式,分别是白箱聚集与外禀迭代子和黑箱聚集于内禀迭代子。
+ +## 类图 + +![迭代子模式](https://github.com/lzh984294471/designPattern/raw/master/pics/iterator.jpg) + +## 结构 + +* 抽象容器:一般是一个接口,提供一个iterator()方法,例如java中的Collection接口,List接口,Set接口等。 +* 具体容器:就是抽象容器的具体实现类,比如List接口的有序列表实现ArrayList,List接口的链表实现LinkList, +Set接口的哈希列表的实现HashSet等。 +* 抽象迭代器:定义遍历元素所需要的方法,一般来说会有这么三个方法:取得第一个元素的方法first(),取得下 +一个元素的方法next(),判断是否遍历结束的方法isDone()(或者叫hasNext()),移出当前对象的方法remove(). +* 迭代器实现:实现迭代器接口中定义的方法,完成集合的迭代。 + +## 优缺点 +### 优点 +* 简化了遍历方式,对于对象集合的遍历,还是比较麻烦的,对于数组或者有序列表,我们尚可以通过游标来取得, +但用户需要在对集合了解很清楚的前提下,自行遍历对象,但是对于hash表来说,用户遍历起来就比较麻烦了。而引 +入了迭代器方法后,用户用起来就简单的多了。 +* 可以提供多种遍历方式,比如说对有序列表,我们可以根据需要提供正序遍历,倒序遍历两种迭代器,用户用起来 +只需要得到我们实现好的迭代器,就可以方便的对集合进行遍历了。 +* 封装性良好,用户只需要得到迭代器就可以遍历,而对于遍历算法则不用去关心。 + +### 缺点 + +* 对于比较简单的遍历(像数组或者有序列表),使用迭代器方式遍历较为繁琐,大家可能都有感觉,像ArrayList, +我们宁可愿意使用for循环和get方法来遍历集合。 diff --git a/src/main/java/com/zeekling/iterator/test/Test.java b/src/main/java/com/zeekling/iterator/test/Test.java new file mode 100755 index 0000000..bb6b67b --- /dev/null +++ b/src/main/java/com/zeekling/iterator/test/Test.java @@ -0,0 +1,20 @@ +package iterator.iterator.test; + +import com.zeekling.iterator.AggregateImp; +import com.zeekling.iterator.Iterator; + +/** + * Created by lzh on 3/31/16. + * 迭代子模式 + */ +public class Test { + public static void main(String[] args) { + Object[] object = {1,2,3,4,5,6,7}; + AggregateImp aggregateImp = new AggregateImp(object); + Iterator iterator = aggregateImp.createIterator(); + while(!iterator.isLast()){ + System.out.print(iterator.current()+"\t"); + iterator.next(); + } + } +} diff --git a/src/main/java/com/zeekling/mediator/readme.md b/src/main/java/com/zeekling/mediator/readme.md new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/com/zeekling/memento/Caretaker.java b/src/main/java/com/zeekling/memento/Caretaker.java new file mode 100755 index 0000000..864198a --- /dev/null +++ b/src/main/java/com/zeekling/memento/Caretaker.java @@ -0,0 +1,15 @@ +package com.zeekling.memento; + +/** + * Created by lzh on 4/1/16. + */ +public class Caretaker { + private Memento memento; + + public Memento getMemento() { + return memento; + } + public void saveMemento(Memento memento){ + this.memento = memento; + } +} diff --git a/src/main/java/com/zeekling/memento/Memento.java b/src/main/java/com/zeekling/memento/Memento.java new file mode 100755 index 0000000..f075a07 --- /dev/null +++ b/src/main/java/com/zeekling/memento/Memento.java @@ -0,0 +1,17 @@ +package com.zeekling.memento; +/** + * Created by lzh on 4/1/16. + */ +public class Memento { + private String msg; + public Memento(String msg){ + this.msg = msg; + } + public String getMsg(){ + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } +} diff --git a/src/main/java/com/zeekling/memento/Originator.java b/src/main/java/com/zeekling/memento/Originator.java new file mode 100755 index 0000000..f93fe1f --- /dev/null +++ b/src/main/java/com/zeekling/memento/Originator.java @@ -0,0 +1,20 @@ +package com.zeekling.memento; +/** + * Created by lzh on 4/1/16. + * 备忘录模式 + */ +public class Originator { + private String msg; + public Memento createMemento(String msg){ + this.msg = msg; + return new Memento(msg); + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public String getMsg() { + return msg; + } +} diff --git a/src/main/java/com/zeekling/memento/readme.md b/src/main/java/com/zeekling/memento/readme.md new file mode 100755 index 0000000..94a3c88 --- /dev/null +++ b/src/main/java/com/zeekling/memento/readme.md @@ -0,0 +1,24 @@ +# 备忘录模式 +## 简述 +备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态 +捕捉(Capture)住,并外部化,存储起来,从而可以在将来合适的时候把这个对象还原到存储起来的状态。 + +## 类图 + +![备忘录模式](https://github.com/lzh984294471/designPattern/raw/master/pics/memento.png) + +## 优点 + +1. 有时一些发起人对象的内部信息在发起人对象以外的地方,当必须又发起人自己读取,这时备忘录模式可以把复杂的 +发起人的内部信息对其他对象屏蔽起来从而恰当的保持封装的边界。 +2. 本模式简化了发起人,发起人不需要管理和保存内部状态的一个个版本。客户端可以自行的管理他们所需要的这些状 +态的版本 +3. 发起人角色状态改变时,有可能这个状态无效,这时候可以使用暂时存储起来的备忘录讲状态复原。 + + +## 缺点 + +1. 如果发起人角色的状态需要完整的存到备忘录对象中,那么在资源消耗上备忘录可能很昂贵。 +2. 当负责人将一个对象存储起来的时候,负责人可能并不知道这个状态占用太多的存储空间,从而无法提醒用户一个操作是否昂 +贵。 +3. 当发起人角色改变的时候,有可能这个协议无效。如果状态改变的成功率不高的话,不如采取“假如”协议模式。 diff --git a/src/main/java/com/zeekling/memento/test/Test.java b/src/main/java/com/zeekling/memento/test/Test.java new file mode 100755 index 0000000..b67fa1c --- /dev/null +++ b/src/main/java/com/zeekling/memento/test/Test.java @@ -0,0 +1,16 @@ +package memento.memento.test; + +import com.zeekling.memento.Caretaker; +import com.zeekling.memento.Originator; + +/** + * Created by lzh on 4/1/16. + */ +public class Test { + public static void main(String[] args) { + Originator originator = new Originator(); + Caretaker caretaker = new Caretaker(); + caretaker.saveMemento(originator.createMemento("令照辉真帅")); + System.out.println(caretaker.getMemento().getMsg()); + } +} diff --git a/src/main/java/com/zeekling/observer/Observer.java b/src/main/java/com/zeekling/observer/Observer.java new file mode 100755 index 0000000..d9f51b7 --- /dev/null +++ b/src/main/java/com/zeekling/observer/Observer.java @@ -0,0 +1,8 @@ +package com.zeekling.observer; + +/** + * Created by lzh on 3/31/16. + */ +public interface Observer { + void update(String msg); +} diff --git a/src/main/java/com/zeekling/observer/ObserverImp.java b/src/main/java/com/zeekling/observer/ObserverImp.java new file mode 100755 index 0000000..d53370c --- /dev/null +++ b/src/main/java/com/zeekling/observer/ObserverImp.java @@ -0,0 +1,11 @@ +package com.zeekling.observer; + +/** + * Created by lzh on 3/31/16. + */ +public class ObserverImp implements Observer { + @Override + public void update(String msg) { + System.out.println(this.toString() + ":\t" + msg); + } +} diff --git a/src/main/java/com/zeekling/observer/Subject.java b/src/main/java/com/zeekling/observer/Subject.java new file mode 100755 index 0000000..33a62d0 --- /dev/null +++ b/src/main/java/com/zeekling/observer/Subject.java @@ -0,0 +1,9 @@ +package com.zeekling.observer; +/** + * Created by lzh on 3/31/16. + */ +public interface Subject { + void addObserver(Observer observer); + void removeObserver(Observer observer); + void updateObserver(String msg); +} diff --git a/src/main/java/com/zeekling/observer/SubjectImp.java b/src/main/java/com/zeekling/observer/SubjectImp.java new file mode 100755 index 0000000..5160701 --- /dev/null +++ b/src/main/java/com/zeekling/observer/SubjectImp.java @@ -0,0 +1,37 @@ + +package com.zeekling.observer; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by lzh on 3/31/16. + */ +public class SubjectImp implements Subject { + private List observers = new ArrayList<>(); + @Override + public void addObserver(Observer observer) { + if( !observers.contains(observer) ){ + observers.add(observer); + System.out.println(observer+"is added"); + }else { + throw new RuntimeException(observer+" is already in this Subject"); + } + } + + @Override + public void removeObserver(Observer observer) { + if(observers.contains(observer)){ + observers.remove(observer); + System.out.println(observer+"is remove"); + }else { + throw new RuntimeException(observer+" is not in this Subject,please add first"); + } + } + + @Override + public void updateObserver(String msg) { + for(Observer observer:observers){ + observer.update(msg); + } + } +} diff --git a/src/main/java/com/zeekling/observer/readme.md b/src/main/java/com/zeekling/observer/readme.md new file mode 100755 index 0000000..e69de29 diff --git a/src/main/java/com/zeekling/observer/test/Test.java b/src/main/java/com/zeekling/observer/test/Test.java new file mode 100755 index 0000000..ab52928 --- /dev/null +++ b/src/main/java/com/zeekling/observer/test/Test.java @@ -0,0 +1,21 @@ +package observer.observer.test; + +import com.zeekling.observer.Observer; +import com.zeekling.observer.ObserverImp; +import com.zeekling.observer.Subject; +import com.zeekling.observer.SubjectImp; + +/** + * Created by lzh on 3/30/16. + */ +public class Test { + public static void main(String[] args) { + Observer observer = new ObserverImp(); + Observer observer1 = new ObserverImp(); + Subject subject = new SubjectImp(); + subject.addObserver(observer); + subject.addObserver(observer1); + subject.updateObserver("lzh"); + subject.removeObserver(observer); + } +} diff --git a/src/main/java/com/zeekling/prototype/Prototype.java b/src/main/java/com/zeekling/prototype/Prototype.java new file mode 100755 index 0000000..66ae460 --- /dev/null +++ b/src/main/java/com/zeekling/prototype/Prototype.java @@ -0,0 +1,13 @@ +package com.zeekling.prototype; + +/** + * Created by lzh on 3/29/16. + */ +public interface Prototype extends Cloneable { + + void say(); + + void setName(String name); + + Prototype clone() throws CloneNotSupportedException; +} diff --git a/src/main/java/com/zeekling/prototype/PrototypeSub.java b/src/main/java/com/zeekling/prototype/PrototypeSub.java new file mode 100755 index 0000000..f56964e --- /dev/null +++ b/src/main/java/com/zeekling/prototype/PrototypeSub.java @@ -0,0 +1,24 @@ + +package com.zeekling.prototype; + +/** + * Created by lzh on 3/29/16. + */ +public class PrototypeSub implements Prototype { + private String name; + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public Prototype clone() throws CloneNotSupportedException{ + return (Prototype) super.clone(); + } + + @Override + public void say() { + System.out.println("hello world " + name); + } +} diff --git a/src/main/java/com/zeekling/prototype/readme.md b/src/main/java/com/zeekling/prototype/readme.md new file mode 100755 index 0000000..e808bc8 --- /dev/null +++ b/src/main/java/com/zeekling/prototype/readme.md @@ -0,0 +1,27 @@ +# 原型模式 +## 简介 +原型模式要求对象实现一个可以“克隆”自身的接口,这样就可以通过复制一个实例对象本身来创建一个新的 +实例。这样一来,通过原型实例创建新的对象,就不再需要关心这个实例本身的类型,只要实现了克隆自身 +的方法,就可以通过这个方法来获取新的对象,而无须再去通过new来创建。 +原型模式有两种表现形式:(1)简单形式、(2)登记形式,这两种表现形式仅仅是原型模式的不同实现。 +## 简单形式的原型模式 +> (1)客户Client角色:客户类提出创建对象的请求。 +> (2)抽象原型Prototype角色:这是一个抽象角色,通常由一个Java接口或Java抽象类实现。此角色给出所有的具体原 +>型类所需的接口。 +> (3)具体原型(Concrete com.zeekling.prototype)角色:被复制的对象。此角色需要实现抽象的原型角色所要求的接口。
+ +![简单形式的原型模式](http://www.zeekling.cn/gogs/zeek/designPattern/raw/master/pics/simplePrototype.png) + +## 登记形式的原型模式 +作为原型模式的第二种形式,它多了一个原型管理器PrototypeManager角色,该角色的作用是:创建具体 +原型类的对象,并记录每一个被创建的对象。
+![登记形式的原型模式](http://www.zeekling.cn/gogs/zeek/designPattern/raw/master/pics/prototype.png) +## 两种形式的比较 +简单形式和登记形式的原型模式各有其长处和短处。 +如果需要创建的原型对象数目较少而且比较固定的话,可以采取第一种形式。在这种情况下,原型对象的 +引用可以由客户端自己保存。如果要创建的原型对象数目不固定的话,可以采取第二种形式。在这种情况 +下,客户端不保存对原型对象的引用,这个任务被交给管理员对象。在复制一个原型对象之前,客户端可 +以查看管理员对象是否已经有一个满足要求的原型对象。如果有,可以直接从管理员类取得这个对象引用; +如果没有,客户端就需要自行复制此原型对象。 +## 深克隆和浅克隆 +[深克隆和浅克隆的例子](http://blog.csdn.net/lzh984294471/article/details/51749865) diff --git a/src/main/java/com/zeekling/prototype/test/Test.java b/src/main/java/com/zeekling/prototype/test/Test.java new file mode 100755 index 0000000..fb3f89c --- /dev/null +++ b/src/main/java/com/zeekling/prototype/test/Test.java @@ -0,0 +1,18 @@ + +package Prototype.prototype.test; + +import com.zeekling.prototype.Prototype; +import com.zeekling.prototype.PrototypeSub; + +/** + * Created by lzh on 3/29/16. + */ +public class Test { + public static void main(String[] args) throws CloneNotSupportedException { + Prototype prototype = new PrototypeSub(); + prototype.setName("令照辉"); + Prototype prototypeClone; + prototypeClone = prototype.clone(); + prototypeClone.say(); + } +} diff --git a/src/main/java/com/zeekling/proxy/DynamicProxy.java b/src/main/java/com/zeekling/proxy/DynamicProxy.java new file mode 100644 index 0000000..d3cfdae --- /dev/null +++ b/src/main/java/com/zeekling/proxy/DynamicProxy.java @@ -0,0 +1,43 @@ +package com.zeekling.proxy; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +/** + * @author 令照辉 [zeekling@163.com] + * @version 1.0 + * @since 2018-05-13 + */ +public class DynamicProxy implements InvocationHandler { + + private Object target; + + public DynamicProxy(){ + super(); + } + + public DynamicProxy(Object target){ + super(); + this.target = target; + } + + + @Override + public Object invoke(Object o, Method method, Object[] args) throws Throwable { + Object result = null; + if (target != null){ + System.out.println("--------before-----"); + result = method.invoke(target,args); + System.out.println("--------after------"); + } + return result; + } + + public Object getTarget() { + return target; + } + + public void setTarget(Object target) { + this.target = target; + } +} diff --git a/src/main/java/com/zeekling/proxy/Person.java b/src/main/java/com/zeekling/proxy/Person.java new file mode 100755 index 0000000..c800f6f --- /dev/null +++ b/src/main/java/com/zeekling/proxy/Person.java @@ -0,0 +1,11 @@ +package com.zeekling.proxy; + +/** + * Created by lzh on 3/29/16. + */ +public interface Person { + /** + * + */ + void say(); +} diff --git a/src/main/java/com/zeekling/proxy/Proxy.java b/src/main/java/com/zeekling/proxy/Proxy.java new file mode 100755 index 0000000..eead337 --- /dev/null +++ b/src/main/java/com/zeekling/proxy/Proxy.java @@ -0,0 +1,12 @@ + +package com.zeekling.proxy; +/** + * Created by lzh on 3/29/16. + */ +public class Proxy implements Person { + private Person person = new Real(); + @Override + public void say() { + person.say(); + } +} diff --git a/src/main/java/com/zeekling/proxy/Real.java b/src/main/java/com/zeekling/proxy/Real.java new file mode 100755 index 0000000..59822a4 --- /dev/null +++ b/src/main/java/com/zeekling/proxy/Real.java @@ -0,0 +1,11 @@ +package com.zeekling.proxy; + +/** + * Created by lzh on 3/29/16. + */ +public class Real implements Person { + @Override + public void say() { + System.out.println("class name:" + this.getClass().getName()); + } +} diff --git a/src/main/java/com/zeekling/proxy/RealSecond.java b/src/main/java/com/zeekling/proxy/RealSecond.java new file mode 100644 index 0000000..c81df8c --- /dev/null +++ b/src/main/java/com/zeekling/proxy/RealSecond.java @@ -0,0 +1,14 @@ +package com.zeekling.proxy; + +/** + * @author 令照辉 [zeekling@163.com] + * @version 1.0 + * @apiNote + * @since 2018-05-13 + */ +public class RealSecond implements Person { + @Override + public void say() { + System.out.println("class name:" + this.getClass().getName()); + } +} diff --git a/src/main/java/com/zeekling/proxy/readme.md b/src/main/java/com/zeekling/proxy/readme.md new file mode 100644 index 0000000..c0a170b --- /dev/null +++ b/src/main/java/com/zeekling/proxy/readme.md @@ -0,0 +1,14 @@ +### 静态代理模式 +#### 包含角色 +* Subject:抽象主题角色,可以是接口,也可以是抽象类。 +* RealSubject:真实主题角色。业务逻辑的具体执行者。 +* ProxySubject:代理主题角色。内部含有RealSubject的引用,负责对真实角色的调用,并在真实主题角色处理前后做预处理 + 和善后工作。 + +#### 应用场景 +>例如:假设有一组对象都实现同一个接口,实现同样的方法,但这组对象中有一部分对象需要有单独的方法,传统的笨办 +>法是在每一个应用端都加上这个单独的方法,但是代码重用性低,耦合性高。 + + +### UML图 +![代理模式](http://www.zeekling.cn/gogs/zeek/designPattern/raw/master/pics/proxy.png) diff --git a/src/main/java/com/zeekling/proxy/test/Test.java b/src/main/java/com/zeekling/proxy/test/Test.java new file mode 100755 index 0000000..c72939e --- /dev/null +++ b/src/main/java/com/zeekling/proxy/test/Test.java @@ -0,0 +1,28 @@ + +package proxy.proxy.test; + +import com.zeekling.proxy.DynamicProxy; +import com.zeekling.proxy.Person; +import com.zeekling.proxy.Real; +import com.zeekling.proxy.RealSecond; + +import java.lang.reflect.Proxy; + +/** + * Created by lzh on 3/29/16. + * 代理模式 + */ +public class Test { + public static void main(String[] args) { + Person person = new com.zeekling.proxy.Proxy(); + Person real = new Real(); + Person realSecond = new RealSecond(); + person.say(); + DynamicProxy dynamicProxy = new DynamicProxy(real); + Person proxy = (Person) Proxy.newProxyInstance(person.getClass().getClassLoader(), + person.getClass().getInterfaces(), dynamicProxy); + proxy.say(); + dynamicProxy.setTarget(realSecond); + proxy.say(); + } +} diff --git a/src/main/java/com/zeekling/simgleton/HungrySingleton.java b/src/main/java/com/zeekling/simgleton/HungrySingleton.java new file mode 100755 index 0000000..e31e8e3 --- /dev/null +++ b/src/main/java/com/zeekling/simgleton/HungrySingleton.java @@ -0,0 +1,17 @@ +package com.zeekling.simgleton; + +/** + * Created by lzh on 3/30/16. + */ +public class HungrySingleton { + private static final HungrySingleton HUNGRY_SIMGLETON = new HungrySingleton(); + private HungrySingleton(){ + + } + public static HungrySingleton getInstance(){ + return HungrySingleton.HUNGRY_SIMGLETON; + } + public void say(){ + System.out.println("I am hungry"); + } +} diff --git a/src/main/java/com/zeekling/simgleton/LazySingleton.java b/src/main/java/com/zeekling/simgleton/LazySingleton.java new file mode 100755 index 0000000..ca15037 --- /dev/null +++ b/src/main/java/com/zeekling/simgleton/LazySingleton.java @@ -0,0 +1,27 @@ +package com.zeekling.simgleton; + +/** + * Created by lzh on 3/30/16. + */ +public class LazySingleton { + private static LazySingleton lazySimgleton = null; + private LazySingleton(){} + public static LazySingleton getInstance(){ + //线程不安全 +// if(lazySimgleton == null){ +// lazySimgleton = new LazySingleton(); +// } + //线程安全 + if(lazySimgleton == null){ + synchronized(LazySingleton.class){ + if(lazySimgleton == null){ + lazySimgleton = new LazySingleton(); + } + } + } + return lazySimgleton; + } + public void say(){ + System.out.println("I am lazy"); + } +} diff --git a/src/main/java/com/zeekling/simgleton/readme.md b/src/main/java/com/zeekling/simgleton/readme.md new file mode 100755 index 0000000..3fb2f1e --- /dev/null +++ b/src/main/java/com/zeekling/simgleton/readme.md @@ -0,0 +1,11 @@ +# 单例模式 +## 简介 +在应用这个模式时,单例对象的类必须保证只有一个实例存在。 +## 实现单例模式的思路 +一个类能返回对象一个引用(永远是同一个)和一个获得该实例的方法(必须是静态方法,通常使用getInstance这个 +名称);当我们调用这个方法时,如果类持有的引用不为空就返回这个引用,如果类保持的引用为空就创建该类的实 +例并将实例的引用赋予该类保持的引用;同时我们还将该类的构造函数定义为私有方法,这样其他处的代码就无法通 +过调用该类的构造函数来实例化该类的对象,只有通过该类提供的静态方法来得到该类的唯一实例。 +## 实现方式 +饿汉方式:指全局的单例实例在类装载时构建。()一般认为这种方式要更加安全些) +懒汉方式:指全局的单例实例在第一次被使用时构建。 diff --git a/src/main/java/com/zeekling/simgleton/test/Test.java b/src/main/java/com/zeekling/simgleton/test/Test.java new file mode 100755 index 0000000..d9847a4 --- /dev/null +++ b/src/main/java/com/zeekling/simgleton/test/Test.java @@ -0,0 +1,19 @@ + +package simgleton.simgleton.test; + +import com.zeekling.simgleton.HungrySingleton; +import com.zeekling.simgleton.LazySingleton; + +/** + * Created by lzh on 3/30/16. + */ +public class Test { + public static void main(String[] args) { + //懒汉模式 + LazySingleton lazySingleton = LazySingleton.getInstance(); + lazySingleton.say(); + //饿汉模式 + HungrySingleton hungrySingleton = HungrySingleton.getInstance(); + hungrySingleton.say(); + } +} diff --git a/src/main/java/com/zeekling/simpleFactory/Car.java b/src/main/java/com/zeekling/simpleFactory/Car.java new file mode 100755 index 0000000..ef625e9 --- /dev/null +++ b/src/main/java/com/zeekling/simpleFactory/Car.java @@ -0,0 +1,13 @@ +package com.zeekling.simpleFactory; + +/** + * Created by lzh on 3/28/16. + */ +public interface Car { + + String name(); + + String color(); + + int pay(); +} diff --git a/src/main/java/com/zeekling/simpleFactory/CarFactory.java b/src/main/java/com/zeekling/simpleFactory/CarFactory.java new file mode 100755 index 0000000..a21616a --- /dev/null +++ b/src/main/java/com/zeekling/simpleFactory/CarFactory.java @@ -0,0 +1,9 @@ + +package com.zeekling.simpleFactory; + +/** + * Created by lzh on 3/28/16. + */ +public interface CarFactory { + Car getCar(Class clasz); +} diff --git a/src/main/java/com/zeekling/simpleFactory/CarFactoryImpl.java b/src/main/java/com/zeekling/simpleFactory/CarFactoryImpl.java new file mode 100755 index 0000000..d3f66da --- /dev/null +++ b/src/main/java/com/zeekling/simpleFactory/CarFactoryImpl.java @@ -0,0 +1,24 @@ + +package com.zeekling.simpleFactory; + +/** + * Created by lzh on 3/28/16. + */ +public class CarFactoryImpl implements CarFactory{ + private Car car; + + /** + * + * @param clazz 传入要利用工厂进行实现的类 + * @return 返回构造器实例化后的方法 + */ + public Car getCar(Class clazz){ + try { + car = (Car) Class.forName(clazz.getName()).newInstance(); + //newInstance()是弱类型,只能调用无参构造器 + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + e.printStackTrace(); + } + return car; + } +} diff --git a/src/main/java/com/zeekling/simpleFactory/Ferrari.java b/src/main/java/com/zeekling/simpleFactory/Ferrari.java new file mode 100755 index 0000000..fafaa18 --- /dev/null +++ b/src/main/java/com/zeekling/simpleFactory/Ferrari.java @@ -0,0 +1,19 @@ + +package com.zeekling.simpleFactory; + +/** + * Created by lzh on 3/28/16. + */ +public class Ferrari implements Car { + public String name() { + return "Ferrari"; + } + + public String color() { + return "red"; + } + + public int pay() { + return 2000; + } +} diff --git a/src/main/java/com/zeekling/simpleFactory/RollsRoyce.java b/src/main/java/com/zeekling/simpleFactory/RollsRoyce.java new file mode 100755 index 0000000..d1be4da --- /dev/null +++ b/src/main/java/com/zeekling/simpleFactory/RollsRoyce.java @@ -0,0 +1,19 @@ + +package com.zeekling.simpleFactory; + +/** + * Created by lzh on 3/28/16. + */ +public class RollsRoyce implements Car { + public String name() { + return "RollsRoyce"; + } + + public String color() { + return "green"; + } + + public int pay() { + return 3000; + } +} diff --git a/src/main/java/com/zeekling/simpleFactory/readme.md b/src/main/java/com/zeekling/simpleFactory/readme.md new file mode 100644 index 0000000..c3c610c --- /dev/null +++ b/src/main/java/com/zeekling/simpleFactory/readme.md @@ -0,0 +1,10 @@ +# 简单工厂模式 +## 简介 +当我们需要创建各种不同对象,这些类称为具体产品类,而是将他们的公共的代码进行抽象和提取后封装在一个抽象产品类和产品接口中,每一个具体类都是抽象产品类的子类;然后提供一个工厂类用于创建各种产品,在工厂中提供创建产品方法。 +## 使用场景 +>1. 工厂类负责创建的对象比较少,由于创建的对象比较少,不会造成工厂方法中的业务逻辑太过复杂。 +>2. 客户端只知道传入工厂类的参数,对于如何创建对象并不关心。 + +## UML类图分析 +![简单工厂模式](https://img.zeekling.cn/images/2019/12/01/simpleFactory.png) + diff --git a/src/main/java/com/zeekling/state/ConcreteState1.java b/src/main/java/com/zeekling/state/ConcreteState1.java new file mode 100755 index 0000000..4885db5 --- /dev/null +++ b/src/main/java/com/zeekling/state/ConcreteState1.java @@ -0,0 +1,12 @@ +package com.zeekling.state; +/** + * Created by lzh on 4/1/16. + * 状态模式 + */ +public class ConcreteState1 implements State { + @Override + public void handle(String handle) { + System.out.println(handle); + System.out.println(ConcreteState1.class.getName()); + } +} diff --git a/src/main/java/com/zeekling/state/ConcreteState2.java b/src/main/java/com/zeekling/state/ConcreteState2.java new file mode 100755 index 0000000..43f9e1c --- /dev/null +++ b/src/main/java/com/zeekling/state/ConcreteState2.java @@ -0,0 +1,12 @@ +package com.zeekling.state; +/** + * Created by lzh on 4/1/16. + * 状态模式 + */ +public class ConcreteState2 implements State { + @Override + public void handle(String handle) { + System.out.println(handle); + System.out.println(ConcreteState2.class.getName()); + } +} diff --git a/src/main/java/com/zeekling/state/Context.java b/src/main/java/com/zeekling/state/Context.java new file mode 100755 index 0000000..1b6a987 --- /dev/null +++ b/src/main/java/com/zeekling/state/Context.java @@ -0,0 +1,15 @@ +package com.zeekling.state; +/** + * Created by lzh on 4/1/16. + * 状态模式 + */ +public class Context { + private State state; + + public void setState(State state) { + this.state = state; + } + public void handle(String handle){ + state.handle(handle); + } +} diff --git a/src/main/java/com/zeekling/state/State.java b/src/main/java/com/zeekling/state/State.java new file mode 100755 index 0000000..d69575f --- /dev/null +++ b/src/main/java/com/zeekling/state/State.java @@ -0,0 +1,9 @@ +package com.zeekling.state; + +/** + * Created by lzh on 4/1/16. + * 状态模式 + */ +public interface State { + void handle(String handle); +} diff --git a/src/main/java/com/zeekling/state/readme.md b/src/main/java/com/zeekling/state/readme.md new file mode 100755 index 0000000..e69de29 diff --git a/src/main/java/com/zeekling/state/test/Test.java b/src/main/java/com/zeekling/state/test/Test.java new file mode 100755 index 0000000..631198e --- /dev/null +++ b/src/main/java/com/zeekling/state/test/Test.java @@ -0,0 +1,19 @@ +package state.state.test; + +import com.zeekling.state.ConcreteState1; +import com.zeekling.state.ConcreteState2; +import com.zeekling.state.Context; + +/** + * Created by lzh on 4/1/16. + * 状态模式 + */ +public class Test { + public static void main(String[] args) { + Context context = new Context(); + context.setState(new ConcreteState1()); + context.handle("lzh"); + context.setState(new ConcreteState2()); + context.handle("ct"); + } +} diff --git a/src/main/java/com/zeekling/strategy/Context.java b/src/main/java/com/zeekling/strategy/Context.java new file mode 100755 index 0000000..9145224 --- /dev/null +++ b/src/main/java/com/zeekling/strategy/Context.java @@ -0,0 +1,14 @@ +package com.zeekling.strategy; + +/** + * Created by lzh on 3/30/16. + */ +public class Context { + private Strategy strategy; + public Context(Strategy strategy){ + this.strategy = strategy; + } + public void action(){ + strategy.doSomething(); + } +} diff --git a/src/main/java/com/zeekling/strategy/Eat.java b/src/main/java/com/zeekling/strategy/Eat.java new file mode 100755 index 0000000..2fcaefb --- /dev/null +++ b/src/main/java/com/zeekling/strategy/Eat.java @@ -0,0 +1,11 @@ + +package com.zeekling.strategy; +/** + * Created by lzh on 3/30/16. + */ +public class Eat implements Strategy { + @Override + public void doSomething() { + System.out.println("i like eat tiger.."); + } +} diff --git a/src/main/java/com/zeekling/strategy/Say.java b/src/main/java/com/zeekling/strategy/Say.java new file mode 100755 index 0000000..8cc27ea --- /dev/null +++ b/src/main/java/com/zeekling/strategy/Say.java @@ -0,0 +1,10 @@ +package com.zeekling.strategy; +/** + * Created by lzh on 3/30/16. + */ +public class Say implements Strategy { + @Override + public void doSomething() { + System.out.println("令照辉真帅"); + } +} diff --git a/src/main/java/com/zeekling/strategy/Strategy.java b/src/main/java/com/zeekling/strategy/Strategy.java new file mode 100755 index 0000000..f01df91 --- /dev/null +++ b/src/main/java/com/zeekling/strategy/Strategy.java @@ -0,0 +1,8 @@ + +package com.zeekling.strategy; +/** + * Created by lzh on 3/30/16. + */ +public interface Strategy { + void doSomething(); +} diff --git a/src/main/java/com/zeekling/strategy/Walk.java b/src/main/java/com/zeekling/strategy/Walk.java new file mode 100755 index 0000000..8662ded --- /dev/null +++ b/src/main/java/com/zeekling/strategy/Walk.java @@ -0,0 +1,11 @@ + +package com.zeekling.strategy; +/** + * Created by lzh on 3/30/16. + */ +public class Walk implements Strategy { + @Override + public void doSomething() { + System.out.println("i walk to Baijing"); + } +} diff --git a/src/main/java/com/zeekling/strategy/readme.md b/src/main/java/com/zeekling/strategy/readme.md new file mode 100755 index 0000000..e69de29 diff --git a/src/main/java/com/zeekling/strategy/test/Test.java b/src/main/java/com/zeekling/strategy/test/Test.java new file mode 100755 index 0000000..caa19bd --- /dev/null +++ b/src/main/java/com/zeekling/strategy/test/Test.java @@ -0,0 +1,21 @@ + +package strategy.strategy.test; + +import com.zeekling.strategy.Context; +import com.zeekling.strategy.Eat; +import com.zeekling.strategy.Say; +import com.zeekling.strategy.Walk; + +/** + * Created by lzh on 3/30/16. + */ +public class Test { + public static void main(String[] args) { + Context say = new Context(new Say()); + Context eat = new Context(new Eat()); + Context walk = new Context(new Walk()); + say.action(); + eat.action(); + walk.action(); + } +} diff --git a/src/main/java/com/zeekling/template/AbstractFather.java b/src/main/java/com/zeekling/template/AbstractFather.java new file mode 100755 index 0000000..a3eea7d --- /dev/null +++ b/src/main/java/com/zeekling/template/AbstractFather.java @@ -0,0 +1,12 @@ +package com.zeekling.template; + +/** + * Created by lzh on 3/30/16. + * 模板方法模式 + */ +public class AbstractFather { + public void say(){} + public void walk(){} + public void eat(){} + public void sleep(){} +} diff --git a/src/main/java/com/zeekling/template/LZH_1.java b/src/main/java/com/zeekling/template/LZH_1.java new file mode 100755 index 0000000..98008a6 --- /dev/null +++ b/src/main/java/com/zeekling/template/LZH_1.java @@ -0,0 +1,11 @@ +package com.zeekling.template; +/** + * Created by lzh on 3/30/16. + */ +public class LZH_1 extends AbstractFather { + @Override + public void sleep() { + super.sleep(); + System.out.println("lzh sleep"); + } +} diff --git a/src/main/java/com/zeekling/template/LZH_2.java b/src/main/java/com/zeekling/template/LZH_2.java new file mode 100755 index 0000000..61af059 --- /dev/null +++ b/src/main/java/com/zeekling/template/LZH_2.java @@ -0,0 +1,12 @@ + +package com.zeekling.template; +/** + * Created by lzh on 3/30/16. + */ +public class LZH_2 extends AbstractFather { + @Override + public void eat() { + super.eat(); + System.out.println("lzh eat food "); + } +} diff --git a/src/main/java/com/zeekling/template/Test.java b/src/main/java/com/zeekling/template/Test.java new file mode 100755 index 0000000..0a604d2 --- /dev/null +++ b/src/main/java/com/zeekling/template/Test.java @@ -0,0 +1,14 @@ + +package com.zeekling.template; +/** + * Created by lzh on 3/30/16. + *模板方法模式 + */ +public class Test { + public static void main(String[] args) { + AbstractFather lzh01 = new LZH_1(); + AbstractFather lzh02 = new LZH_2(); + lzh01.sleep(); + lzh02.eat(); + } +} diff --git a/src/main/java/com/zeekling/template/readme.md b/src/main/java/com/zeekling/template/readme.md new file mode 100755 index 0000000..e69de29 diff --git a/src/main/java/com/zeekling/visitor/readme.md b/src/main/java/com/zeekling/visitor/readme.md new file mode 100755 index 0000000..e69de29 diff --git a/src/test/java/com/zeekling/abstractFactory/abstractFactoryTest.java b/src/test/java/com/zeekling/abstractFactory/abstractFactoryTest.java new file mode 100644 index 0000000..3989d57 --- /dev/null +++ b/src/test/java/com/zeekling/abstractFactory/abstractFactoryTest.java @@ -0,0 +1,20 @@ +package com.zeekling.abstractFactory; + +import com.zeekling.abstractFactory.impl.FactoryImpl; +import org.junit.Test; + +/** + * @author 令照辉 [zeekling@163.com] + * @version 1.0 + * @apiNote + * @since 2019-12-02 + */ +public class abstractFactoryTest { + + @Test + public void abstractFactory(){ + AbstractFactory factory = new FactoryImpl(); + factory.america(); + factory.chinese(); + } +} diff --git a/src/test/java/com/zeekling/simpleFactory/simpleFactoryTest.java b/src/test/java/com/zeekling/simpleFactory/simpleFactoryTest.java new file mode 100644 index 0000000..a76be06 --- /dev/null +++ b/src/test/java/com/zeekling/simpleFactory/simpleFactoryTest.java @@ -0,0 +1,26 @@ +package com.zeekling.simpleFactory; + +import org.junit.Test; + +/** + * @author 令照辉 [zeekling@163.com] + * @version 1.0 + * @apiNote + * @since 2019-12-02 + */ +public class simpleFactoryTest { + + + @Test + public void printCar(){ + CarFactory factory = new CarFactoryImpl(); + //利用工厂创建Ferrari类 + Car ferrari = factory.getCar(Ferrari.class); + System.out.println(ferrari.name()+"\t"+ferrari.color()+"\t"+ferrari.pay()); + //利用工厂创建RollsRoyce类 + Car rollsRoyce = factory.getCar(RollsRoyce.class); + System.out.println(rollsRoyce.name()+"\t"+rollsRoyce.color()+"\t"+rollsRoyce.pay()); + } + + +}