2

Observer mode

1. Definition and Type

  • Definition: Defines one-to-many dependencies between objects, allowing multiple observer objects to monitor a certain subject object at the same time. When the subject object changes, all its dependencies will be notified and updated.
  • Type: Behavioral

2. Applicable scenarios

  • Associate behavior scenarios and establish a trigger mechanism

3. Advantages

  • Establish an abstract coupling between the observer and the observed
  • Observer mode supports broadcast communication

4. Disadvantages

  • There is too much detail dependence between observers, which increases time consumption and program complexity
  • Use it properly and avoid loop calls

5.Coding

  • Create a problem class
public class Teacher implements Observer {
    private String  teacherName;
    public Teacher(String teacherName){
        this.teacherName = teacherName;
    }

    @Override
    public void update(Observable o, Object arg) {
        Course course = (Course) o;
        Question question = (Question) arg;
        System.out.println(teacherName+"老师的"+course.getCourseName()+",收到一个来自"+question.getStudentName()+"提出的问题:"+question.getQuestionContent());
    }
}
  • Create a course class

    • Inherit the Observable class, which means that it is an observable

image-20211017102445481

  • The method can be overridden. The setChange() flag tells the observer that there is a change; notifyObservers() informs the observer
public class Course extends Observable {

    private String courseName;

    public Course(String courseName) {
        this.courseName = courseName;
    }

    public String getCourseName() {
        return courseName;
    }
    public void produceQuestion(Course course,Question question){
        System.out.println(question.getStudentName()+"在"+course.getCourseName()+"课程中,提出了问题:"+question.getQuestionContent());
        setChanged();
        notifyObservers(question);
    }
}
  • Create teacher class

    • Implement the Observer interface, which means it is an observer
    • Override the update method to write business logic based on the parameters passed by the observer
public class Teacher implements Observer {
    private String  teacherName;
    public Teacher(String teacherName){
        this.teacherName = teacherName;
    }

    @Override
    public void update(Observable o, Object arg) {
        Course course = (Course) o;
        Question question = (Question) arg;
        System.out.println(teacherName+"老师的"+course.getCourseName()+",收到一个来自"+question.getStudentName()+"提出的问题:"+question.getQuestionContent());
    }
}
  • Test class
public class Test {
    public static void main(String[] args) {
        // 创建被观察者
        Course course = new Course("如何让富婆爱上我~");
        // 创建观察者
        Teacher teacher = new Teacher("HHHH");
        // 绑定观察者和被观察者
        course.addObserver(teacher);
        
        Question question = new Question();
        question.setStudentName("Root");
        question.setQuestionContent("有富婆的联系方式  吗?");
        
        course.produceQuestion(course,question);

    }
}
  • Console output

image-20211017102755507

6. Summary

  • Two structures: Observer and Observed
  • When the observed person changes, the observer can be notified
  • The observer can accept the parameters passed in by the observed change and encode

Root
4 声望4 粉丝