Skip to content
Prev Previous commit
Next Next commit
完成 模板方法模式 Sample
  • Loading branch information
JamesZBL committed Nov 29, 2017
commit 93762a07d2e9641c4da3e2631dd6e03b50e7d10f
42 changes: 42 additions & 0 deletions template-method/src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* 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:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* 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("认证听讲", "老师");
}
}
68 changes: 68 additions & 0 deletions template-method/src/main/java/LearningMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* 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:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* 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);
}
}
51 changes: 51 additions & 0 deletions template-method/src/main/java/NegativeLearinngMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* 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:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* 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);
}
}
}
51 changes: 51 additions & 0 deletions template-method/src/main/java/PositiveLearningMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* 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:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* 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);
}
}
}
54 changes: 54 additions & 0 deletions template-method/src/main/java/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* 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:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* 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;
}
}