Basic design pattern -- observer pattern

Posted by Cynthia on Sat, 12 Feb 2022 01:46:03 +0100

theme: channing-cyan
highlight: androidstudio

"This is the 18th day of my participation in 2022's first update challenge. For details: 2022 first revision challenge

preface

Nothing. Today, relax a little. I accidentally brushed a small video about the implementation of Spring observer mode when I visited Zhihu. I feel it's a little interesting. Let's explain what observer mode is.

Instead of explaining what this mode is, let's talk about what functions we want to achieve. I want such a function. A can provide services for B and C at the same time, but if B or C does not meet a certain condition, I will not provide services for it. For example, WeChat official account can provide services for multiple users simultaneously, but if B is not concerned, the user B can not enjoy services. Or like a teacher giving a class to a student, a teacher can give a class to multiple students together, but if the students are "invited" out by the teacher, they can't have a class together. There are two states here. For a student, if he listens well, he can continue his class. If he doesn't listen well, he can't.

We call the design pattern that can realize such a function observer pattern.

Project structure

It's easy to implement this pattern.

realization

We have defined two interfaces. Of course, this design pattern is actually a little like the proxy pattern, which controls the other party through the other party.

The two interfaces are simple.

package com.huterox.observe;

//import java.util.Observer;

public interface Observable {
    void registerObserver(Observer o);
    void removeObserver(Observer o);
    void notifyObserver();
}
package com.huterox.observe;

public interface Observer {
    void update(String message);
}

However, it is worth mentioning that our java actually implements an Observer interface.

Next is our student class

package com.huterox.observe;

public class Students implements Observer{


    private String name;
    private String message;
    public Students(){

    }
    public Students(String name) {
        this.name = name;
    }

    @Override
    public void update(String message) {
        this.message = message;
        this.quite();
    }

    public void quite(){
        System.out.println("student"+this.name+"received:"+this.message+"--Wise teacher!");
    }
}

Teacher class

package com.huterox.observe;

import java.util.ArrayList;
import java.util.List;

public class Teacher implements Observable{

    private String message;
    private String name;
    private List<Object> studentpool = new ArrayList<>();


    @Override
    public void registerObserver(Observer o) {
        studentpool.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        studentpool.remove(o);
    }

    @Override
    public void notifyObserver() {
        for(int i = 0; i < studentpool.size(); i++) {
            Observer observer = (Observer) studentpool.get(i);
            observer.update(message);
        }
    }

    public void setMessage(String message){
        this.message = message;
        this.notifyObserver();
    }
}

Finally, the test class

package com.huterox.observe;

public class Test {
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        Students xiaoming = new Students("Xiao Ming");
        Students xiaogang = new Students("Xiao Gang");
        teacher.registerObserver(xiaoming);
        teacher.registerObserver(xiaogang);
        teacher.setMessage("attend class;class begins");

        System.out.println("Xiao Gang doesn't hand in his homework. Get out!");
        teacher.removeObserver(xiaogang);
        teacher.setMessage("attend class;class begins");
    }
}

effect

Finally, let's take a look at the effect demonstration

Topics: Java