Design mode Observer mode Observer

Keywords: Android Mobile iOS

Observer mode, also known as publish / subscribe mode, defines 1-to-many one-way dependency between objects, so that when the target object changes, the observer object can receive messages and process them immediately.

One, four elements

1. Target object interface
2. Target object
3. Observer interface
4. Observer object

II. Core principles

The observer pattern decouples the object relationship completely by the way of one-way dependence. When the object changes, it does not need to care about the observer object instance. It conforms to the principle of dependency inversion and the principle of interface isolation.

III. example code

1. Target object interface
/**
 * Created by AdminFun
 * Email: 614484070@qq.com
 * Creation: January 8, 2019
 * Revised: January 8, 2019
 * Version: v1.0.0
 * Description: observer interface, used for registration / deregistration / notification.
 */
public interface ISubject {

    void registerObserver(IObserver observer);

    void unRegisterObserver(IObserver observer);

    void notifyDataByChanged(Object obj);
}
2. Observer interface
/**
 * Created by AdminFun
 * Email: 614484070@qq.com
 * Creation: January 8, 2019
 * Revised: January 8, 2019
 * Version: v1.0.0
 * Description: observer interface
 */
public interface IObserver {

    void update(Object obj);
}
3. Target object
/**
 * Created by AdminFun
 * Email: 614484070@qq.com
 * Creation: January 8, 2019
 * Revised: January 8, 2019
 * Version: v1.0.0
 * Description: observed entity = e.g. PUSH Tool
 */
public class PushSubject implements ISubject {

    private List<IObserver> observers;

    @Override
    public void registerObserver(IObserver observer) {
        if (observers == null) {
            observers = new ArrayList<>();
        }
        observers.add(observer);
    }

    @Override
    public void unRegisterObserver(IObserver observer) {
        if (observers != null) {
            observers.remove(observer);
        }
    }

    @Override
    public void notifyDataByChanged(Object obj) {
        if (observers == null) {
            return;
        }
        for (IObserver i : observers) {
            i.update(obj);
        }
    }

    public void ReceiveMsg(String message) {
        // Receive push message, need to inform other places to process message
        this.notifyDataByChanged(message);
    }
}
4. Observer object
/**
 * Created by AdminFun
 * Email: 614484070@qq.com
 * Creation: January 8, 2019
 * Revised: January 8, 2019
 * Version: v1.0.0
 * Description: observer / subscriber: android mobile terminal
 */
public class AndroidOb implements IObserver {

    @Override
    public void update(Object obj) {
        Log.d("common", "Android The phone has been notified:" + obj.toString());
    }
}

/**
 * Created by AdminFun
 * Email: 614484070@qq.com
 * Creation: January 8, 2019
 * Revised: January 8, 2019
 * Version: v1.0.0
 * Description: observer / subscriber / iOS
 */
public class IosOb implements IObserver {

    @Override
    public void update(Object obj) {
        Log.d("common", "Ios The phone has been notified:" + obj.toString());
    }
}
5, use
/**
 * Created by AdminFun
 * Email: 614484070@qq.com
 * Creation: January 8, 2019
 * Revised: January 8, 2019
 * Version: v1.0.0
 * Description: observer mode, also known as publish / subscribe mode
 * Definition: define 1-to-many one-way dependency between objects. When the target object changes, the observer object can receive messages.
 * Roles: target object interface, target object, observer interface, observer object
 * Objective: to decouple the object relationship completely by means of one-way dependence. When the object changes, there is no need to care about the observer object instance.
 * Comply with the principle of dependency inversion and interface isolation
 */
public class ObserverTest {

    public static void main() {
        PushSubject observered = new PushSubject();
        observered.registerObserver(new AndroidOb());
        observered.registerObserver(new IosOb());
        observered.registerObserver(new IObserver() {

            @Override
            public void update(Object obj) {
                Log.d("common", "The test class also received a message:" + obj.toString());
            }
        });

        // Test received push message
        observered.ReceiveMsg("It's one month to celebrate the new year.");
    }
}
Print results

//Android mobile received a notice: there is still one month to go for the new year.
//Ios mobile received a notice: there is still one month to celebrate the new year.
//The test class also received a message: there is still one month left for the new year.

Posted by jswinkelman on Thu, 05 Dec 2019 06:52:55 -0800