I. Introduction:

Today, I encountered a small problem when making the player: there are three places that need the same set of data, and they belong to different places, how to synchronize? Of course, there are many ways to do this, but this article focuses on the observer mode, which is a bit of a throwback to it. Note that this article uses test code and only simulates the situation (the usage principle on Android is consistent, verified).


This simplifies the problem to the following seven classes: Requirements: Changes to data in a SongSubject can notify three observers and update the data simultaneously


Ii. Observer Mode:

One-to-many — One person provides information (Subject) and multiple people require information (Observer). When the information body (T) changes, the Subject reminds the Observer


Interface layer: (Observer)
Public interface Observer<T> {/** * update * @param T */ void update(T T); }Copy the code

2. Interface layer: Subject Interface: (observed)
Public interface Subject<T> {/** ** @param o */ void attach(Observer<T> o); /** * detach(Observer<T> o); /** * detach(Observer<T> o); /** * called when the body of information is updated to notify the observer */ void notifyObserver(); }Copy the code

3. Message Body (Song)
public class Song { private String title; private String singer; private long seek; public Song(String title, String singer, long seek) { this.title = title; this.singer = singer; this.seek = seek; } / /set, get, toString omit... }Copy the code

4. Information provider (observed implementation class) :
public class SongSubject implements Subject<Song>{ private List<Observer<Song>> mObservers = new ArrayList<>(); Private Song mSong; @Override public void attach(Observer<Song> observer) { mObservers.add(observer); } @Override public void detach(Observer<Song> observer) { mObservers.remove(observer); } @Override public voidnotifyObserver() {
        for(Observer<Song> o : mObservers) { o.update(mSong); } /** * set message -- notify all observers * @param song */ public voidsetSong(Song song) { mSong = song; this.notifyObserver(); }}Copy the code

Pop Observer implementation class
/** * Author: Zhang Fengjieteilie * Time: 2018/12/24 0024:21:27 * Email: [email protected] * Description: Pop */ PopPager implements Observer<Song> {@override public void update(Song Song) {//TODO: PopPager implements Observer<Song System.out.println("PopPager:"+ song); }}Copy the code

Fragment observer implementation class
/** * Author: Zhang Fengjieteilie * Time: 2018/12/24 0024:21:27 * Email: [email protected] * Description: Public class HomeListFragment implements Observer<Song> {@override public void update(Song Song) { //TODO: Update view render system.out.println ("HomeListFragment:"+ song); }}Copy the code

7. Home page observer implementation class

The main binding logic is in the mock onCreate, of course, in the MPV, depending on the situation

/** * Author: Zhang Fengjieteilie * Time: 2018/12/24 0024:21:27 * Email: [email protected] * Description: */ public class HomeActivity implements Observer<Song> {private int seek; public voidonCreate() { SongSubject songSubject = new SongSubject(); PopPager popPager = new PopPager(); // Common Pop HomeListFragment HomeListFragment = new HomeListFragment(); // Create Fragment songSubject. Attach (this); //HomeActivity observation message songsubject. attach(homeListFragment); Attach (popPager); // Attach (popPager); //popPager = new Song("Courage"."Ge Qiangli", seek); Timer = new Timer(); // Schedule (newTimerTask() {
            public void run() { song.setSeek(seek++); // Modify message songsubject.setsong (song); // Set the message to notify all observers}}, 0, 1000); } @override public void update(Song Song) {//TODO: render system.out.println ("HomeActivity:"+ song); }}Copy the code

8. Startup classes:
public class Boot { public static void main(String[] args) { HomeActivity activity = new HomeActivity(); activity.onCreate(); }}Copy the code

3. Observer model analysis

1. The following figure shows the running result


2. Analysis:

The results alone may seem unremarkable, but please note:

Each time you print three data from three different classes and when you use the Song object in the update method to control the view, all you have to do is ask the viewer to update the data and the information on the three screens will change in sync, and that's what's nice about the observer mode because in reality the Pop box, Activity and Fragement belong to different classes, and being able to unify changes in this way reduces couplingCopy the code

3. Summary

Design mode or in need of time to understand some clear, dry to see the total feel is also so recently in doing personal player to play, the whole system is very big, also more broken, should not be written, such as perfect hair source bar