File tree Expand file tree Collapse file tree
src/main/java/com/cpucode/pattern/behavior/observer/course/advice Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments