Android Design Patterns - Observer Patterns

Posted by poscribes on Fri, 17 May 2019 04:37:45 +0200

What is the observer model?

Observer mode is also called model view mode, source listener mode, or subordinate mode. A one-to-many dependency is defined between objects. When the state of an object (the observer) changes, the dependent object (the observer) receives notification and updates automatically.

What are you having?

  • The role of abstract observer
    An Abstract topic, which stores references to all observer objects in a collection, has any number of observers for each topic. The abstract topic provides an interface to increase the deletion of observer roles.
  • The role of abstract observer
    Define an interface for all specific observers, and update yourself as you get the theme
  • Specific Observed Role
    That is to say, a specific topic, when the internal state of the subject changes, all registrants send notifications.
  • Specific Observer Role
    Implementing interface updates required for abstract observer roles.

How do you do it?

The observer model is implemented mainly through the following steps.

  1. Define a topic interface
public interface Observerable{
    public void registerObserver(Observer o);
    public void removeObserver(Observer 0);
    public void notifyObserver();
}
  1. Define an abstract observer interface
public interface Observer{
    public void update (String msg);
}
  1. Observers are defined to implement observerable interface, and three interfaces of observerable are implemented concretely. Colleagues have a List set to save registered observers. They can traverse the set when they need to notify the observers.
public class MyObserservable implement Observerable{
    private List<Observer> list;
    private String msg;
    public MyObserverable(){
        list=new ArrayList<Observer>();
    }
    public void registerObserver(Observer o){
        list.add(o);
    }
    public void removeObserver(Observer o){
        if(!list.isEmpty()){
            list.remove(o);
        }
    } 
    public void notifyObserver(){
        for(int i=0;i<list.size();i++){
            Observer observer=list.get(i);
            observer.update(msg);
        }
    }
    public void setInfomation(Stirng s){
        this.msg=s;
        notifyObserver();
    }
}

4. Define specific observers

public MyObserver implement Observer{
private msg=msg;
    public void update (String msg){
        this.msg=msg;
          System.out.println(name + " Received the message: " + message);
    }
}
public MyObserver2 implement Observer{
private msg=msg;
    public void update (String msg){
        this.msg=msg;
          System.out.println(name + " os2 Received the message: " + message);
    }
}
  1. test
public class Test{
public static void mian(String args []){
    MyObserver o1=new MyObserver();
    MyObserver1 o2 =new MyObserver1();
    
    MyObserservable os=new MyObserservable();
    os.regiest(o1);
    os.regiest(o2);
    os.setInfomation("2222");
}
}

When to use it

1. When an object change needs to be notified to other objects, it does not need to specify how many objects need to be notified.
2. An object must notify other objects without knowing who they are.
3. An abstract model has two aspects, one of which depends on the other. These aspects are encapsulated in separate objects so that they can be changed and reused independently.
4. We need to create a trigger chain in the system. The behavior of object A will affect object B, and that of object B will affect object C... The observer pattern can be used to create a chain trigger mechanism.

Advantages and disadvantages

Advantages: 1. The observer and the observee are abstractly coupled. 2. Establish a trigger mechanism.

Disadvantages: 1. If a subject has many direct and indirect observers, it will take a lot of time to notify all observers. 2. If there is a cyclic dependence between the observer and the observer, the observer will trigger a cyclic call between them, which may lead to system crash. 3. The observer model has no corresponding mechanism to let the observer know how the observed object changes, but only to know that the observed object has changed.