From 377705a6c93fa48b833892ba355388ec031142b6 Mon Sep 17 00:00:00 2001 From: James <1146556298@qq.com> Date: Wed, 29 Nov 2017 21:46:42 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=AE=8C=E6=88=90=20=E7=AD=96=E7=95=A5?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=20Sample?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + strategy/pom.xml | 51 +++++++++++++++++++ .../java/me/zbl/strategy/Application.java | 41 +++++++++++++++ .../java/me/zbl/strategy/BusinessMan.java | 44 ++++++++++++++++ .../zbl/strategy/TransportationAirplane.java | 40 +++++++++++++++ .../zbl/strategy/TransportationStrategy.java | 33 ++++++++++++ .../me/zbl/strategy/TransportationTrain.java | 40 +++++++++++++++ .../zbl/strategy/TransportationVehicle.java | 40 +++++++++++++++ 8 files changed, 290 insertions(+) create mode 100644 strategy/pom.xml create mode 100644 strategy/src/main/java/me/zbl/strategy/Application.java create mode 100644 strategy/src/main/java/me/zbl/strategy/BusinessMan.java create mode 100644 strategy/src/main/java/me/zbl/strategy/TransportationAirplane.java create mode 100644 strategy/src/main/java/me/zbl/strategy/TransportationStrategy.java create mode 100644 strategy/src/main/java/me/zbl/strategy/TransportationTrain.java create mode 100644 strategy/src/main/java/me/zbl/strategy/TransportationVehicle.java diff --git a/pom.xml b/pom.xml index d29f668..771cc5f 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,7 @@ memento observer state + strategy diff --git a/strategy/pom.xml b/strategy/pom.xml new file mode 100644 index 0000000..7584771 --- /dev/null +++ b/strategy/pom.xml @@ -0,0 +1,51 @@ + + + + + java_design_patterns + com.github.JamesZBL + 1.11.9-SNAPSHOT + + 4.0.0 + + strategy + + + junit + junit + test + + + org.mockito + mockito-core + test + + + + \ No newline at end of file diff --git a/strategy/src/main/java/me/zbl/strategy/Application.java b/strategy/src/main/java/me/zbl/strategy/Application.java new file mode 100644 index 0000000..45ca170 --- /dev/null +++ b/strategy/src/main/java/me/zbl/strategy/Application.java @@ -0,0 +1,41 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.strategy; + +/** + * Strategy + */ +public class Application { + + public static void main(String[] args) { + BusinessMan man = new BusinessMan(new TransportationAirplane()); + man.transport(); + + man.changetStrategy(new TransportationTrain()); + man.transport(); + + man.changetStrategy(new TransportationVehicle()); + man.transport(); + } +} diff --git a/strategy/src/main/java/me/zbl/strategy/BusinessMan.java b/strategy/src/main/java/me/zbl/strategy/BusinessMan.java new file mode 100644 index 0000000..bb484a0 --- /dev/null +++ b/strategy/src/main/java/me/zbl/strategy/BusinessMan.java @@ -0,0 +1,44 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.strategy; + +/** + * 商人 + */ +public class BusinessMan { + + private TransportationStrategy strategy; + + public BusinessMan(TransportationStrategy strategy) { + this.strategy = strategy; + } + + public void changetStrategy(TransportationStrategy strategy) { + this.strategy = strategy; + } + + public void transport() { + this.strategy.go(); + } +} diff --git a/strategy/src/main/java/me/zbl/strategy/TransportationAirplane.java b/strategy/src/main/java/me/zbl/strategy/TransportationAirplane.java new file mode 100644 index 0000000..3a4ce0b --- /dev/null +++ b/strategy/src/main/java/me/zbl/strategy/TransportationAirplane.java @@ -0,0 +1,40 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.strategy; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 乘飞机 + */ +public class TransportationAirplane implements TransportationStrategy { + + private static final Logger LOGGER = LoggerFactory.getLogger(TransportationAirplane.class); + + @Override + public void go() { + LOGGER.info("乘飞机从北京去广州"); + } +} diff --git a/strategy/src/main/java/me/zbl/strategy/TransportationStrategy.java b/strategy/src/main/java/me/zbl/strategy/TransportationStrategy.java new file mode 100644 index 0000000..96db438 --- /dev/null +++ b/strategy/src/main/java/me/zbl/strategy/TransportationStrategy.java @@ -0,0 +1,33 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.strategy; + +/** + * 交通方式策略 + */ +@FunctionalInterface +public interface TransportationStrategy { + + void go(); +} diff --git a/strategy/src/main/java/me/zbl/strategy/TransportationTrain.java b/strategy/src/main/java/me/zbl/strategy/TransportationTrain.java new file mode 100644 index 0000000..b7318f0 --- /dev/null +++ b/strategy/src/main/java/me/zbl/strategy/TransportationTrain.java @@ -0,0 +1,40 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.strategy; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 乘火车 + */ +public class TransportationTrain implements TransportationStrategy { + + private static final Logger LOGGER = LoggerFactory.getLogger(TransportationTrain.class); + + @Override + public void go() { + LOGGER.info("乘高铁从北京去上海"); + } +} diff --git a/strategy/src/main/java/me/zbl/strategy/TransportationVehicle.java b/strategy/src/main/java/me/zbl/strategy/TransportationVehicle.java new file mode 100644 index 0000000..ffbffc4 --- /dev/null +++ b/strategy/src/main/java/me/zbl/strategy/TransportationVehicle.java @@ -0,0 +1,40 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.strategy; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 驾车 + */ +public class TransportationVehicle implements TransportationStrategy { + + private static final Logger LOGGER = LoggerFactory.getLogger(TransportationVehicle.class); + + @Override + public void go() { + LOGGER.info("驾车从北京去天津"); + } +} From 991fd52a01b52fa6c9f32f46d379d2ed637c42e2 Mon Sep 17 00:00:00 2001 From: James <1146556298@qq.com> Date: Wed, 29 Nov 2017 21:49:03 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=A8=A1=E5=BC=8F=20Module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + template-method/pom.xml | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 template-method/pom.xml diff --git a/pom.xml b/pom.xml index 771cc5f..539135c 100644 --- a/pom.xml +++ b/pom.xml @@ -66,6 +66,7 @@ observer state strategy + templatemethod diff --git a/template-method/pom.xml b/template-method/pom.xml new file mode 100644 index 0000000..fc24cc3 --- /dev/null +++ b/template-method/pom.xml @@ -0,0 +1,51 @@ + + + + + java_design_patterns + com.github.JamesZBL + 1.11.9-SNAPSHOT + + 4.0.0 + + template-method + + + junit + junit + test + + + org.mockito + mockito-core + test + + + + \ No newline at end of file From 93762a07d2e9641c4da3e2631dd6e03b50e7d10f Mon Sep 17 00:00:00 2001 From: James <1146556298@qq.com> Date: Wed, 29 Nov 2017 22:27:58 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=AE=8C=E6=88=90=20=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=A8=A1=E5=BC=8F=20Sample?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/Application.java | 42 ++++++++++++ .../src/main/java/LearningMethod.java | 68 +++++++++++++++++++ .../src/main/java/NegativeLearinngMethod.java | 51 ++++++++++++++ .../src/main/java/PositiveLearningMethod.java | 51 ++++++++++++++ template-method/src/main/java/Student.java | 54 +++++++++++++++ 5 files changed, 266 insertions(+) create mode 100644 template-method/src/main/java/Application.java create mode 100644 template-method/src/main/java/LearningMethod.java create mode 100644 template-method/src/main/java/NegativeLearinngMethod.java create mode 100644 template-method/src/main/java/PositiveLearningMethod.java create mode 100644 template-method/src/main/java/Student.java diff --git a/template-method/src/main/java/Application.java b/template-method/src/main/java/Application.java new file mode 100644 index 0000000..0b3be27 --- /dev/null +++ b/template-method/src/main/java/Application.java @@ -0,0 +1,42 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Template method + */ +public class Application { + + private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); + + public static void main(String[] args) { + Student student = new Student(new PositiveLearningMethod()); + student.learn("上课走神", "同学"); + LOGGER.info("更换学习方法"); + student.changeMethod(new NegativeLearinngMethod()); + student.learn("认证听讲", "老师"); + } +} diff --git a/template-method/src/main/java/LearningMethod.java b/template-method/src/main/java/LearningMethod.java new file mode 100644 index 0000000..5515542 --- /dev/null +++ b/template-method/src/main/java/LearningMethod.java @@ -0,0 +1,68 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 学习方法的抽象类 + */ +public abstract class LearningMethod { + + private static final Logger LOGGER = LoggerFactory.getLogger(LearningMethod.class); + + /** + * 预习效果 + * + * @return + */ + protected abstract String preLearning(); + + /** + * 状态 + * + * @param description 学习状态 + */ + protected abstract void Learning(String description); + + /** + * 请教对象 + * + * @param adviser 请教对象 + */ + protected abstract void afterLearning(String adviser); + + /** + * 学习过程 + * + * @param description 听课状态 + * @param adviser 请假对象 + */ + public void learn(String description, String adviser) { + String preLearningResult = preLearning(); + LOGGER.info("{}", preLearningResult); + Learning(description); + afterLearning(adviser); + } +} diff --git a/template-method/src/main/java/NegativeLearinngMethod.java b/template-method/src/main/java/NegativeLearinngMethod.java new file mode 100644 index 0000000..a178faf --- /dev/null +++ b/template-method/src/main/java/NegativeLearinngMethod.java @@ -0,0 +1,51 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 消极的学习方法 + */ +public class NegativeLearinngMethod extends LearningMethod { + + private static final Logger LOGGER = LoggerFactory.getLogger(NegativeLearinngMethod.class); + + @Override + protected String preLearning() { + return "几乎没有预习,对上课要学的内容一无所知"; + } + + @Override + protected void Learning(String description) { + LOGGER.info("学习状态:{}", description); + } + + @Override + protected void afterLearning(String adviser) { + if (!adviser.equals("")) { + LOGGER.info("只有很少的知识点没有听懂,于是找{}提问", adviser); + } + } +} diff --git a/template-method/src/main/java/PositiveLearningMethod.java b/template-method/src/main/java/PositiveLearningMethod.java new file mode 100644 index 0000000..5fabb15 --- /dev/null +++ b/template-method/src/main/java/PositiveLearningMethod.java @@ -0,0 +1,51 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 积极的学习方法 + */ +public class PositiveLearningMethod extends LearningMethod { + + private static final Logger LOGGER = LoggerFactory.getLogger(PositiveLearningMethod.class); + + @Override + protected String preLearning() { + return "预习到位,为听课打下很好的基础"; + } + + @Override + protected void Learning(String description) { + LOGGER.info("学习状态:{}", description); + } + + @Override + protected void afterLearning(String adviser) { + if (!adviser.equals("")) { + LOGGER.info("只有很少的知识点没有听懂,于是找{}提问", adviser); + } + } +} diff --git a/template-method/src/main/java/Student.java b/template-method/src/main/java/Student.java new file mode 100644 index 0000000..cbe230f --- /dev/null +++ b/template-method/src/main/java/Student.java @@ -0,0 +1,54 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * 学生 + */ +public class Student { + + private LearningMethod method; + + public Student(LearningMethod method) { + this.method = method; + } + + /** + * 学习 + * + * @param description 状态 + * @param adviser 请教对象 + */ + public void learn(String description, String adviser) { + method.learn(description, adviser); + } + + /** + * 更换学习方法 + * + * @param method + */ + public void changeMethod(LearningMethod method) { + this.method = method; + } +} From 1177e07dcfca14724b94d7821eb1a63ecd4587fb Mon Sep 17 00:00:00 2001 From: James <1146556298@qq.com> Date: Wed, 29 Nov 2017 22:31:32 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20module=20=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 539135c..1404ac6 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ observer state strategy - templatemethod + template-method From 2532c1fdd828017a4445fc6af1637c439e06bf07 Mon Sep 17 00:00:00 2001 From: James <1146556298@qq.com> Date: Thu, 30 Nov 2017 13:05:28 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20gitter=20badge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4a95f0c..27ddf50 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/JamesZBL/java_design_patterns/blob/master/LICENSE) [![Build status](https://travis-ci.org/JamesZBL/java_design_patterns.svg?branch=master)](https://travis-ci.org/JamesZBL/java_design_patterns) - +[![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/java_design_patterns/) ## 介绍 设计模式(Design pattern)是用于面向对象程序设计的、有效提高代码重用效率、有着明确使用场景分类的程序设计规范的总结。使用设计模式的 From f39e4f90ea317afd50c05825bcdfbcb037b3cbc0 Mon Sep 17 00:00:00 2001 From: James <1146556298@qq.com> Date: Thu, 30 Nov 2017 13:29:14 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E8=80=85=E6=A8=A1=E5=BC=8F=20Module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + visitor/pom.xml | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 visitor/pom.xml diff --git a/pom.xml b/pom.xml index 1404ac6..d75fb78 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,7 @@ state strategy template-method + visitor diff --git a/visitor/pom.xml b/visitor/pom.xml new file mode 100644 index 0000000..cc39f7f --- /dev/null +++ b/visitor/pom.xml @@ -0,0 +1,51 @@ + + + + + java_design_patterns + com.github.JamesZBL + 1.11.9-SNAPSHOT + + 4.0.0 + + visitor + + + junit + junit + test + + + org.mockito + mockito-core + test + + + + \ No newline at end of file From 39baab8dc2a63c7fa6bac8c435cc472ba6066158 Mon Sep 17 00:00:00 2001 From: James <1146556298@qq.com> Date: Thu, 30 Nov 2017 13:58:13 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=8C=85=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/{ => me/zbl/template/method}/Application.java | 1 + .../main/java/{ => me/zbl/template/method}/LearningMethod.java | 1 + .../{ => me/zbl/template/method}/NegativeLearinngMethod.java | 1 + .../{ => me/zbl/template/method}/PositiveLearningMethod.java | 1 + .../src/main/java/{ => me/zbl/template/method}/Student.java | 1 + 5 files changed, 5 insertions(+) rename template-method/src/main/java/{ => me/zbl/template/method}/Application.java (98%) rename template-method/src/main/java/{ => me/zbl/template/method}/LearningMethod.java (98%) rename template-method/src/main/java/{ => me/zbl/template/method}/NegativeLearinngMethod.java (98%) rename template-method/src/main/java/{ => me/zbl/template/method}/PositiveLearningMethod.java (98%) rename template-method/src/main/java/{ => me/zbl/template/method}/Student.java (98%) diff --git a/template-method/src/main/java/Application.java b/template-method/src/main/java/me/zbl/template/method/Application.java similarity index 98% rename from template-method/src/main/java/Application.java rename to template-method/src/main/java/me/zbl/template/method/Application.java index 0b3be27..62a460f 100644 --- a/template-method/src/main/java/Application.java +++ b/template-method/src/main/java/me/zbl/template/method/Application.java @@ -21,6 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +package me.zbl.template.method; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/template-method/src/main/java/LearningMethod.java b/template-method/src/main/java/me/zbl/template/method/LearningMethod.java similarity index 98% rename from template-method/src/main/java/LearningMethod.java rename to template-method/src/main/java/me/zbl/template/method/LearningMethod.java index 5515542..05356be 100644 --- a/template-method/src/main/java/LearningMethod.java +++ b/template-method/src/main/java/me/zbl/template/method/LearningMethod.java @@ -21,6 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +package me.zbl.template.method; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/template-method/src/main/java/NegativeLearinngMethod.java b/template-method/src/main/java/me/zbl/template/method/NegativeLearinngMethod.java similarity index 98% rename from template-method/src/main/java/NegativeLearinngMethod.java rename to template-method/src/main/java/me/zbl/template/method/NegativeLearinngMethod.java index a178faf..e66c22d 100644 --- a/template-method/src/main/java/NegativeLearinngMethod.java +++ b/template-method/src/main/java/me/zbl/template/method/NegativeLearinngMethod.java @@ -21,6 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +package me.zbl.template.method; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/template-method/src/main/java/PositiveLearningMethod.java b/template-method/src/main/java/me/zbl/template/method/PositiveLearningMethod.java similarity index 98% rename from template-method/src/main/java/PositiveLearningMethod.java rename to template-method/src/main/java/me/zbl/template/method/PositiveLearningMethod.java index 5fabb15..5f049cb 100644 --- a/template-method/src/main/java/PositiveLearningMethod.java +++ b/template-method/src/main/java/me/zbl/template/method/PositiveLearningMethod.java @@ -21,6 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +package me.zbl.template.method; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/template-method/src/main/java/Student.java b/template-method/src/main/java/me/zbl/template/method/Student.java similarity index 98% rename from template-method/src/main/java/Student.java rename to template-method/src/main/java/me/zbl/template/method/Student.java index cbe230f..36fad95 100644 --- a/template-method/src/main/java/Student.java +++ b/template-method/src/main/java/me/zbl/template/method/Student.java @@ -21,6 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +package me.zbl.template.method; /** * 学生 From 20f3d1edea36d46ef0603a5e3fe2857046cbca09 Mon Sep 17 00:00:00 2001 From: James <1146556298@qq.com> Date: Thu, 30 Nov 2017 13:58:30 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E5=AE=8C=E6=88=90=20=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E8=80=85=E6=A8=A1=E5=BC=8F=20Sample?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/me/zbl/visitor/Boss.java | 45 +++++++++++++++++ .../main/java/me/zbl/visitor/BossVisitor.java | 50 +++++++++++++++++++ .../main/java/me/zbl/visitor/Engineer.java | 45 +++++++++++++++++ .../java/me/zbl/visitor/EngineerVisitor.java | 50 +++++++++++++++++++ .../src/main/java/me/zbl/visitor/Manager.java | 45 +++++++++++++++++ .../java/me/zbl/visitor/ManagerVisitor.java | 50 +++++++++++++++++++ .../src/main/java/me/zbl/visitor/Unit.java | 45 +++++++++++++++++ .../main/java/me/zbl/visitor/UnitVisitor.java | 36 +++++++++++++ 8 files changed, 366 insertions(+) create mode 100644 visitor/src/main/java/me/zbl/visitor/Boss.java create mode 100644 visitor/src/main/java/me/zbl/visitor/BossVisitor.java create mode 100644 visitor/src/main/java/me/zbl/visitor/Engineer.java create mode 100644 visitor/src/main/java/me/zbl/visitor/EngineerVisitor.java create mode 100644 visitor/src/main/java/me/zbl/visitor/Manager.java create mode 100644 visitor/src/main/java/me/zbl/visitor/ManagerVisitor.java create mode 100644 visitor/src/main/java/me/zbl/visitor/Unit.java create mode 100644 visitor/src/main/java/me/zbl/visitor/UnitVisitor.java diff --git a/visitor/src/main/java/me/zbl/visitor/Boss.java b/visitor/src/main/java/me/zbl/visitor/Boss.java new file mode 100644 index 0000000..28bb239 --- /dev/null +++ b/visitor/src/main/java/me/zbl/visitor/Boss.java @@ -0,0 +1,45 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.visitor; + +/** + * 老板 + */ +public class Boss extends Unit { + + public Boss(Unit... children) { + super(children); + } + + @Override + public void beVisited(UnitVisitor visitor) { + visitor.visitBoss(this); + super.beVisited(visitor); + } + + @Override + public String toString() { + return "老板"; + } +} diff --git a/visitor/src/main/java/me/zbl/visitor/BossVisitor.java b/visitor/src/main/java/me/zbl/visitor/BossVisitor.java new file mode 100644 index 0000000..387d913 --- /dev/null +++ b/visitor/src/main/java/me/zbl/visitor/BossVisitor.java @@ -0,0 +1,50 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.visitor; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 老板的访问者 + */ +public class BossVisitor implements UnitVisitor { + + private static final Logger LOGGER = LoggerFactory.getLogger(BossVisitor.class); + + @Override + public void visitEngineer(Engineer engineer) { + + } + + @Override + public void visitBoss(Boss boss) { + LOGGER.info("你好,{}", boss); + } + + @Override + public void visitManager(Manager manager) { + + } +} diff --git a/visitor/src/main/java/me/zbl/visitor/Engineer.java b/visitor/src/main/java/me/zbl/visitor/Engineer.java new file mode 100644 index 0000000..f90b317 --- /dev/null +++ b/visitor/src/main/java/me/zbl/visitor/Engineer.java @@ -0,0 +1,45 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.visitor; + +/** + * 工程师 + */ +public class Engineer extends Unit { + + public Engineer(Unit... children) { + super(children); + } + + @Override + public void beVisited(UnitVisitor visitor) { + visitor.visitEngineer(this); + super.beVisited(visitor); + } + + @Override + public String toString() { + return "工程师"; + } +} diff --git a/visitor/src/main/java/me/zbl/visitor/EngineerVisitor.java b/visitor/src/main/java/me/zbl/visitor/EngineerVisitor.java new file mode 100644 index 0000000..621acb2 --- /dev/null +++ b/visitor/src/main/java/me/zbl/visitor/EngineerVisitor.java @@ -0,0 +1,50 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.visitor; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 工程师的访问者 + */ +public class EngineerVisitor implements UnitVisitor { + + private static final Logger LOGGER = LoggerFactory.getLogger(EngineerVisitor.class); + + @Override + public void visitEngineer(Engineer engineer) { + LOGGER.info("你好,{}", engineer); + } + + @Override + public void visitBoss(Boss boss) { + + } + + @Override + public void visitManager(Manager manager) { + + } +} diff --git a/visitor/src/main/java/me/zbl/visitor/Manager.java b/visitor/src/main/java/me/zbl/visitor/Manager.java new file mode 100644 index 0000000..cd9727a --- /dev/null +++ b/visitor/src/main/java/me/zbl/visitor/Manager.java @@ -0,0 +1,45 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.visitor; + +/** + * 经理 + */ +public class Manager extends Unit { + + public Manager(Unit... children) { + super(children); + } + + @Override + public void beVisited(UnitVisitor visitor) { + visitor.visitManager(this); + super.beVisited(visitor); + } + + @Override + public String toString() { + return "经理"; + } +} diff --git a/visitor/src/main/java/me/zbl/visitor/ManagerVisitor.java b/visitor/src/main/java/me/zbl/visitor/ManagerVisitor.java new file mode 100644 index 0000000..1286372 --- /dev/null +++ b/visitor/src/main/java/me/zbl/visitor/ManagerVisitor.java @@ -0,0 +1,50 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.visitor; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 经理的访问者 + */ +public class ManagerVisitor implements UnitVisitor { + + private static final Logger LOGGER = LoggerFactory.getLogger(ManagerVisitor.class); + + @Override + public void visitEngineer(Engineer engineer) { + + } + + @Override + public void visitBoss(Boss boss) { + + } + + @Override + public void visitManager(Manager manager) { + LOGGER.info("你好,{}", manager); + } +} diff --git a/visitor/src/main/java/me/zbl/visitor/Unit.java b/visitor/src/main/java/me/zbl/visitor/Unit.java new file mode 100644 index 0000000..d80312f --- /dev/null +++ b/visitor/src/main/java/me/zbl/visitor/Unit.java @@ -0,0 +1,45 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.visitor; + +/** + * 可访问的单元 + */ +public abstract class Unit { + + private Unit[] children; + + public Unit(Unit... children) { + this.children = children; + } + + /** + * 接受访问 + */ + public void beVisited(UnitVisitor visitor) { + for (Unit childUnit : children) { + childUnit.beVisited(visitor); + } + } +} diff --git a/visitor/src/main/java/me/zbl/visitor/UnitVisitor.java b/visitor/src/main/java/me/zbl/visitor/UnitVisitor.java new file mode 100644 index 0000000..932b419 --- /dev/null +++ b/visitor/src/main/java/me/zbl/visitor/UnitVisitor.java @@ -0,0 +1,36 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package me.zbl.visitor; + +/** + * 访问者接口 + */ +public interface UnitVisitor { + + void visitEngineer(Engineer engineer); + + void visitBoss(Boss boss); + + void visitManager(Manager manager); +}