Android design patterns - Observer pattern

Loading, please credit: https://www.cnblogs.com/tangZH/p/11175120.html 

 

Observer Pattern

To put it plainly, it is a changes to occur, but also changed it all depends on the object, which is to-many relationship.

Such as object A, object B, the object C. B and C is dependent on A, then changes A, B and C will change. At this time, A is the observed, B and C is an observer.

 

The observer pattern is also known as publish / subscribe model, mainly to allow the observer and the observed decoupling between.

 

UML diagram:

 

 

 Role Description:

Subject (abstract topic) : is an abstract class observer, it will refer to all observers are stored in a collection. Abstract topic provides an interface, you can add and delete objects observer.

ConcreteSubject (specific topics) : concrete is an observer. When the specific changes of state observer, will give each registered observer notification is sent.

Observer (abstract observer) : All the specific viewer an abstract class that defines an interface for all of the specific observer: get notified when updates its theme.

ConcrereObserver (specific observer) : abstract observer of implementation.

 

We look at a specific example:

School bell rang, the teacher and the students of different reactions.

1, the definition of an abstract theme:

    The abstract theme defines some common methods, namely the need to achieve a specific theme inside.

// abstractions viewer 
public  interface Observable {
     // add the observer 
    void the addObserver (the Observer Observer); 

    // remove observer 
    void deleteObserver (the Observer Observer); 

    // notify observers 
    void notifyObserver (String MSG); 
}

 

2, create a particular theme (school bell):

public class AlarmClock implements Observable {
    //保存观察者对象
    List<Observer> list = new ArrayList<>();

    @Override
    public void addObserver(Observer observer) {
        list.add(observer);
    }

    @Override
    public void deleteObserver(Observer observer) {
        list.remove(observer);
    }

    /**
     * 通知观察者
     * @param msg
     */
    @Override
    public void notifyObserver(String msg) {
        for (Observer observer : list) {
            observer.action(msg);
        }
    }
}

 

3, create an abstract observer:

     It defines the methods required to achieve all the specific observer, after hearing the ringing behavior

// abstract viewer 
public  interface the Observer {
     void Action (String MSG); 
}

 

4. Create a specific observer:

public class Students implements Observer {

    String name;

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

    @Override
    public void action(String msg) {
        System.out.println(msg + name + "开始听课");
    }
}
public class Teacher implements Observer {
    @Override
    public void action(String msg) {
        System.out.print(msg + "老师开始讲课");
    }
}

 

6, to achieve:

AlarmClock = Observable new new AlarmClock (); 

the Observer student1 = new new Students ( "kid actor" ); 
the Observer STUDENT2 = new new Students ( "big fart child" ); 
the Observer Teacher = new new Teacher (); 

// Register observer 
alarmClock.addObserver (student1); 
alarmClock.addObserver (STUDENT2); 
alarmClock.addObserver (Teacher); 

// is informed observers have been registered observers 
alarmClock.notifyObserver ( "school bell has been ringing");

 

7 results:

 

Here we will achieve the Observer pattern.

 

Scenarios

  • When you change an object needs to notify other objects change, and it does not know exactly how many objects there is time to be changed.
  • When an object must notify other objects, but it can not be assumed other objects who are
  • The message exchange system across a scene, such as the message queue, the event handling mechanism bus.

advantage

  • Decoupled between the observer and the subject matter. Let both sides rely on abstract coupling, rather than relying on concrete. So that the respective change will not affect the change on the other side.
  • Easy to extend without having to modify the existing code when new observer on the same topic.

 Shortcoming

  • Dependency is not fully lifted, still rely on abstract theme abstract observer.
  • To consider when using the Observer pattern about the problem of development efficiency and operational efficiency, including a program to be an observer, multiple observers, develop, debug and so would be more complicated, and is generally in the order of execution of Java in the notification message, so an observer Caton, will affect the overall efficiency of the implementation, in this case, usually asynchronous implementation.
  • It may cause excess data notification.

Internal JDK also built Observable (abstract to be an observer), Observer (abstract observer) of these two classes, we can also be directly used, specifically to look at the source.
 
Android, there are many places to use the observer pattern:
  • Click event
  • listView refresh
  • Broadcast, etc.
 

Guess you like

Origin www.cnblogs.com/tangZH/p/11175120.html