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)是用于面向对象程序设计的、有效提高代码重用效率、有着明确使用场景分类的程序设计规范的总结。使用设计模式的 diff --git a/pom.xml b/pom.xml index d29f668..d75fb78 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,9 @@ memento observer state + strategy + template-method + visitor 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("驾车从北京去天津"); + } +} 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 diff --git a/template-method/src/main/java/me/zbl/template/method/Application.java b/template-method/src/main/java/me/zbl/template/method/Application.java new file mode 100644 index 0000000..62a460f --- /dev/null +++ b/template-method/src/main/java/me/zbl/template/method/Application.java @@ -0,0 +1,43 @@ +/** + * 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.template.method; + +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/me/zbl/template/method/LearningMethod.java b/template-method/src/main/java/me/zbl/template/method/LearningMethod.java new file mode 100644 index 0000000..05356be --- /dev/null +++ b/template-method/src/main/java/me/zbl/template/method/LearningMethod.java @@ -0,0 +1,69 @@ +/** + * 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.template.method; + +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/me/zbl/template/method/NegativeLearinngMethod.java b/template-method/src/main/java/me/zbl/template/method/NegativeLearinngMethod.java new file mode 100644 index 0000000..e66c22d --- /dev/null +++ b/template-method/src/main/java/me/zbl/template/method/NegativeLearinngMethod.java @@ -0,0 +1,52 @@ +/** + * 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.template.method; + +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/me/zbl/template/method/PositiveLearningMethod.java b/template-method/src/main/java/me/zbl/template/method/PositiveLearningMethod.java new file mode 100644 index 0000000..5f049cb --- /dev/null +++ b/template-method/src/main/java/me/zbl/template/method/PositiveLearningMethod.java @@ -0,0 +1,52 @@ +/** + * 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.template.method; + +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/me/zbl/template/method/Student.java b/template-method/src/main/java/me/zbl/template/method/Student.java new file mode 100644 index 0000000..36fad95 --- /dev/null +++ b/template-method/src/main/java/me/zbl/template/method/Student.java @@ -0,0 +1,55 @@ +/** + * 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.template.method; + +/** + * 学生 + */ +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; + } +} 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 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); +}