Observer mode of 23 design modes

scene

  • Here's a list. For example, there are many anchors on the Internet, such as big Sima, 55 Kai, big Quail in the northeast, etc. Take these anchors for example. Dashima has a lot of fans. Everyone can follow him or cancel his attention. He sent a message to all his fans that I will go online at 8 o'clock tonight

The first expression: define the interface between the observer and the observed

First of all, define the fan interface, which will receive the host information in real time. This is the object that must be the host

public interface Observer {
    void update(Subject subject);

}

Then define the anchor class. The anchor will have a lot of fans, and can be followed or cancelled, and send messages

public class Subject {
    //Subscribers and customers call them observers
    private List<Observer> list = new ArrayList<>();

    public void register(Observer observer) {
        list.add(observer);
    }

    public void cancel(Observer observer) {
        list.remove(observer);
    }

    //Notify all subscribers to update status
    public void notifyAllObserver() {
        for (Observer observer : list) {
            observer.update(this);
        }
    }

}

Now define a specific anchor, such as dashima

public class ConcreteSubject extends Subject {
    private String message;

    public String getMessage() {
        return message;
    }

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

Define a specific fan: the message of a fan can only accept the message sent by the host

public class ObserverA implements Observer {
    private String myMessage;

    @Override
    public void update(Subject subject) {
        myMessage = ((ConcreteSubject) subject).getMessage();
    }

    public String getMyMessage() {
        return myMessage;
    }

    public void setMyMessage(String myMessage) {
        this.myMessage = myMessage;
    }
}

test

   //DSM anchor
        ConcreteSubject subject = new ConcreteSubject();

        //Create multiple observers
        ObserverA obs1 = new ObserverA();
        ObserverA obs2 = new ObserverA();
        ObserverA obs3 = new ObserverA();

        //Add these three observers to the observer team of the subject object
        subject.register(obs1);
        subject.register(obs2);
        subject.register(obs3);
        //Change the status of the subject
        subject.setMessage("The anchor will be on at 8 this evening");
        System.out.println("########################");
        //Let's see if the state of the observer has changed
        System.out.println(obs1.getMyMessage());
        System.out.println(obs2.getMyMessage());
        System.out.println(obs3.getMyMessage());

        //Change the status of the subject
        subject.setMessage("I'll send you 50000 yuan red envelopes tonight");
        System.out.println("########################");
        //Let's see if the state of the observer has changed
        System.out.println(obs1.getMyMessage());
        System.out.println(obs2.getMyMessage());
        System.out.println(obs3.getMyMessage());

The second expression: java SE provides java.util.Observable and java.util.Observer

Anchor: observed

public class ConcreteSubject extends Observable {
    private String message;

    public void setMessage(String message) {
        this.message = message;
        setChanged();//Indicates that the target object has changed
        notifyObservers(message); //Notify all observers
    }

    public String getMessage() {
        return message;
    }
}

Fans: Observers

public class ObserverA implements Observer {
    private String myMessage;

    @Override
    public void update(Observable o, Object arg) {
        myMessage = ((ConcreteSubject) o).getMessage();

    }

    public String getMyMessage() {
        return myMessage;
    }

    public void setMyMessage(String myMessage) {
        this.myMessage = myMessage;
    }
}

Tags: Java

Posted on Tue, 05 May 2020 16:17:12 -0400 by altexis