Design mode: observer mode

Keywords: Design Pattern

reference resources
Design mode (V) observer mode

Observer mode

Type: behavioral

A one to many dependency is defined, which allows multiple observer objects to listen to a topic object at the same time. When the state of the subject object changes, it will notify all observer objects so that they can update themselves automatically.

UML

role

  • Subject: abstract topic (Abstract observer). The abstract topic role saves all observer objects in a collection. Each topic can have any number of observers. The abstract topic provides an interface to add and delete observer objects.

  • ConcreteSubject: a specific subject (specific observer). This role stores the relevant status in the specific observer object. When the internal status of a specific subject changes, it sends a notice to all registered observers.

  • Observer: Abstract observer is an abstract class of observer. It defines an update interface to update itself when notified of topic change.

  • Concreteobserver: a concrete observer, which implements the update interface defined by the abstract observer, so as to update its own status when notified of the subject change.

Package structure

subject

Interface

package Observer mode.subject;

import Observer mode.observer.Student;

public interface Subject{
    void addStu(Student student);
    void deleteStu(Student student);
    void notify(String msg);
}

Specific subclass

package Observer mode.subject;

import Observer mode.observer.Student;

import java.util.LinkedList;
import java.util.List;

public class Teacher implements Subject{
    private List<Student> studentList = new LinkedList<>();

    @Override
    public void addStu(Student student) {
        studentList.add(student);
    }

    @Override
    public void deleteStu(Student student) {
        studentList.remove(student);
    }

    @Override
    public void notify(String msg) {
        for(Student student:studentList){
            student.remind(msg);
        }
    }
}

observer

Interface

package Observer mode.observer;

public interface Observer {
  void remind(String msg);
}

Specific subclass

package Observer mode.observer;

public class Student implements Observer{
    private String name;
    public Student (String name){
        this.name=name;
    }
    @Override
    public void remind(String msg) {
        System.out.println(name+"Informed message:"+msg);
    }
}

client

package Observer mode.client;

import Observer mode.observer.Student;
import Observer mode.subject.Teacher;

public class Client {
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        System.out.println("-------------------------------");
        Student zhang =new Student("Zhang San");
        teacher.addStu(zhang);
        teacher.notify("Zhang San joined our class");
        System.out.println("-------------------------------");
        Student li =new Student("Li Si");
        teacher.addStu(li);
        teacher.notify("Li Si has come to our class");
        System.out.println("-------------------------------");
        teacher.notify("The teacher is going to draw someone to answer the question");
        System.out.println("-------------------------------");
        teacher.deleteStu(li);
        teacher.notify("Li Si quit our class");
        System.out.println("-------------------------------");
    }
}

effect

-------------------------------
Zhang San was told that Zhang San had joined our class
-------------------------------
Zhang San was told that Li Si had come to our class
 Li Si was told that Li Si had come to our class
-------------------------------
Zhang San was told that the teacher was going to draw people to answer questions
 Li Si was told that the teacher was going to draw someone to answer the question
-------------------------------
Zhang San was told that Li Si quit our class
-------------------------------

Process finished with exit code 0

advantage

Decouple, so that both sides of the coupling rely on abstraction, so that their transformation will not affect the transformation on the other side.

shortcoming

When applying the observer mode, we need to consider the development efficiency and operation efficiency. The program includes one observer and multiple observers. The development and debugging contents will be more complex. Moreover, in Java, the notification of messages is generally executed in sequence. If an observer is stuck, it will affect the overall execution efficiency. In this case, asynchronous implementation is generally adopted.

Posted by Mr_Mako on Fri, 19 Nov 2021 14:31:42 -0800