There are many places in the Android framework that use callback mode, such as the life cycle of Activity, button click event, etc.
The following is the basic model of callback:
- public class A {
- private CallBack callback;
- //Register an event
- public void register(CallBack callback){
- this.callback = callback;
- }
- //Call back when needed
- public void call(){
- callback.oncall();
- }
- }
- public interface CallBack {
- public void oncall();
- }
- public static void main(String[] args) {
- A a = new A();
- a.register(new CallBack() {
- @Override
- public void oncall() {
- System.out.println("Callback function called");
- }
- });
- a.call();
- }
If you change class A to Button, CallBack to OnClickListener, and register to setOnclickListener, it is the same as setting click event in android. callback.oncall(); it is called only after clicking the event.
Observer mode:
Define one to many dependencies between objects. When the state of an object changes, all objects that depend on it are notified and updated automatically.
Objectives:
- public class Subject {
- List<Observer> lists = new ArrayList<Observer>();
- //Register an event
- public void register(Observer observer){
- lists.add(observer);
- }
- public void _notify(){
- for (Observer observer : lists) {
- observer.update();
- }
- }
- public void unRegister(Observer observer){
- lists.remove(observer);
- }
- }
Observer abstract interface
- public interface Observer {
- public void update();
- }
Observer 1
- public class ConcreteObserver1 implements Observer{
- public void update() {
- System.out.println("ConcreteObserver1 Get updates");
- }
- }
Observer 2
- public class ConcreteObserver2 implements Observer{
- public void update() {
- System.out.println("ConcreteObserver2 Get updates");
- }
- }
- public static void main(String[] args) {
- Observer observer1 = new ConcreteObserver1();
- Observer observer2 = new ConcreteObserver2();
- Subject subject = new Subject();
- subject.register(observer1);
- subject.register(observer2);
- subject._notify();
- //Unregister observer 1
- subject.unRegister(observer1);
- subject._notify();
- }
The target object holds the references of each observer and sends notifications when they are needed.
In fact, callbacks are a simple form of the observer pattern. The observer pattern is to raise the callback to the theoretical level of the design pattern.
Change the main method in the callback example to
- public static void main(String[] args) {
- CallBack callback = new CallBackImp();
- A a = new A();
- a.register(callback);
- a.call();
- }
Add the implementation class CallBackImp of CallBack
- public class CallBackImp implements CallBack{
- @Override
- public void oncall() {
- System.out.println("Callback function called");
- }
- }
In this way, it is consistent. The difference is that the target class in observer mode maintains all observer references, while the callback only maintains one reference.
Original link: http://blog.csdn.net/cauchyweistrass/article/details/44593807