Skip to content

Commit 181b973

Browse files
committed
课程提问
1 parent ebc3da4 commit 181b973

5 files changed

Lines changed: 127 additions & 2 deletions

File tree

pattern/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@
126126
- [x] [课程模板模式](src/main/java/com/cpucode/pattern/behavior/template/course/NetworkCourseTest.java)
127127
- [x] [Jdbc模板模式](src/main/java/com/cpucode/pattern/behavior/template/jdbc/MemberDaoTest.java)
128128

129-
## [观察者模式]()
129+
## [观察者模式](src/main/java/com/cpucode/pattern/behavior/observer)
130130

131-
- [] []()
131+
- [x] [课程提问](src/main/java/com/cpucode/pattern/behavior/observer/course/advice/ObserverTest.java)
132132

133133
---------------
134134

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.cpucode.pattern.behavior.observer.course.advice;
2+
3+
import java.util.Observable;
4+
5+
/**
6+
* @author : cpucode
7+
* @date : 2021/7/11
8+
* @time : 23:26
9+
* @github : https://github.com/CPU-Code
10+
* @csdn : https://blog.csdn.net/qq_44226094
11+
*/
12+
public class Course extends Observable {
13+
private String name = "课程";
14+
private static Course course = null;
15+
16+
private Course(){}
17+
18+
public static Course getInstance(){
19+
if (null == course){
20+
course = new Course();
21+
}
22+
23+
return course;
24+
}
25+
26+
27+
public String getName(){
28+
return name;
29+
}
30+
31+
public void publishQuestion(Question question){
32+
System.out.println(question.getUserName() + "在" + this.name + "上提交了一个问题。");
33+
34+
setChanged();
35+
notifyObservers(question);
36+
}
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.cpucode.pattern.behavior.observer.course.advice;
2+
3+
/**
4+
* @author : cpucode
5+
* @date : 2021/7/11
6+
* @time : 23:37
7+
* @github : https://github.com/CPU-Code
8+
* @csdn : https://blog.csdn.net/qq_44226094
9+
*/
10+
public class ObserverTest {
11+
public static void main(String[] args){
12+
Course course = Course.getInstance();
13+
Teacher cpucode = new Teacher("cpucode");
14+
Teacher tom = new Teacher("tom");
15+
16+
Question question = new Question();
17+
question.setUserName("露露");
18+
question.setContent("观察者设计模式适用于哪些场景?");
19+
20+
course.addObserver(cpucode);
21+
course.addObserver(tom);
22+
course.publishQuestion(question);
23+
}
24+
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.cpucode.pattern.behavior.observer.course.advice;
2+
3+
/**
4+
* @author : cpucode
5+
* @date : 2021/7/11
6+
* @time : 23:29
7+
* @github : https://github.com/CPU-Code
8+
* @csdn : https://blog.csdn.net/qq_44226094
9+
*/
10+
public class Question {
11+
private String userName;
12+
private String content;
13+
14+
public String getUserName(){
15+
return userName;
16+
}
17+
18+
public void setUserName(String userName){
19+
this.userName = userName;
20+
}
21+
22+
public String getContent(){
23+
return content;
24+
}
25+
26+
public void setContent(String content){
27+
this.content = content;
28+
}
29+
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.cpucode.pattern.behavior.observer.course.advice;
2+
3+
import java.util.Observable;
4+
import java.util.Observer;
5+
import java.util.Queue;
6+
7+
/**
8+
* @author : cpucode
9+
* @date : 2021/7/11
10+
* @time : 23:33
11+
* @github : https://github.com/CPU-Code
12+
* @csdn : https://blog.csdn.net/qq_44226094
13+
*/
14+
public class Teacher implements Observer {
15+
private String name;
16+
17+
public Teacher(String name){
18+
this.name = name;
19+
}
20+
21+
@Override
22+
public void update(Observable o, Object arg) {
23+
Course course = (Course) o;
24+
Question question = (Question) arg;
25+
26+
System.out.println("=========================");
27+
System.out.println(name + "老师,你好!\\n 您收到了一个来自“" +
28+
course.getName() + "”的提问,希望您解答,问题内容如下:\n" +
29+
question.getContent() + "\n 提问者:" +
30+
question.getUserName());
31+
32+
}
33+
}

0 commit comments

Comments
 (0)