Observer mode of GOF23 design mode

There are many scenarios for observer mode, such as monitoring dom events of js, such as chat programs, such as the implementation of serlet's listener, such as mail subscription;

Its core: when the state of an object changes, it needs to inform a series of objects to make them react;

There are two ways: 1. Push to the client actively
2. Active pull by client

Here is the main way to realize push

**************************

Specific code

Receive object interface

package observer;

public interface Observer {
	void update(Subject s);

}

Receive object implementation class

package observer;

public class Observer1 implements Observer {
	int state; 
	@Override
	public void update(Subject s) {
		// TODO Auto-generated method stub
		this.setState(((ConcreateObserver)s).getState());

	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}

}

Broadcaster main class

package observer;

import java.util.ArrayList;
import java.util.List;

public class Subject {
	private List<Observer> list = new ArrayList<Observer>();
	public void registerObserver(Observer o) {
		list.add(o);
	}
	public void removeObserver(Observer o) {
		list.remove(o);
	}
	public void noticeAllObservers() {
		for (Observer observer : list) {
			observer.update(this);
		}
		
	}
}

Broadcaster subclass

package observer;

public class ConcreateObserver extends Subject{
	private int state;
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state= state;
		this.noticeAllObservers();
	}
	
}

Client call

package observer;

public class Client {
	public static void main(String[] args) throws CloneNotSupportedException {
		ConcreateObserver concreateObserver= new ConcreateObserver();
		Observer1 o1= new Observer1();
		Observer1 o2= new Observer1();
		Observer1 o3= new Observer1();
		concreateObserver.registerObserver(o1);
		concreateObserver.registerObserver(o2);
		concreateObserver.registerObserver(o3);
		
		concreateObserver.setState(1000);
		
		System.out.println(o1.getState());
		System.out.println(o2.getState());
		System.out.println(o3.getState());
		
		concreateObserver.setState(2000);
		
		System.out.println(o1.getState());
		System.out.println(o2.getState());
		System.out.println(o3.getState());
	}
	
	
}

************************

Observer pattern of java internal implementation

Broadcasting class

package observer;

import java.util.Observable;

public class ConCreateObserver1 extends  Observable{
	private int state;
	
	public void set(int s) {
		state = s;
		
		
		setChanged();
		
		notifyObservers();
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

}

Receiving class

package observer;

import java.util.Observable;
import java.util.Observer;

public class Observer2 implements Observer {

	private int myState;
	
	public int getMyState() {
		return myState;
	}
	
	public void setMyState(int myState) {
		this.myState = myState;
	}
	
	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		this.setMyState( ((ConCreateObserver1)o).getState());
	}
	

}

Code implementation

package observer;

public class Client {
	public static void main(String[] args) throws CloneNotSupportedException {
//		ConcreateObserver concreateObserver= new ConcreateObserver();
//		Observer1 o1= new Observer1();
//		Observer1 o2= new Observer1();
//		Observer1 o3= new Observer1();
//		concreateObserver.registerObserver(o1);
//		concreateObserver.registerObserver(o2);
//		concreateObserver.registerObserver(o3);
//		
//		concreateObserver.setState(1000);
//		
//		System.out.println(o1.getState());
//		System.out.println(o2.getState());
//		System.out.println(o3.getState());
//		
//		concreateObserver.setState(2000);
//		
//		System.out.println(o1.getState());
//		System.out.println(o2.getState());
//		System.out.println(o3.getState());
		
		ConCreateObserver1 concreateObserver= new ConCreateObserver1();
		Observer2 o1= new Observer2();
		Observer2 o2= new Observer2();
		Observer2 o3= new Observer2();
		concreateObserver.addObserver(o1);
		concreateObserver.addObserver(o2);
		concreateObserver.addObserver(o3);
		
		concreateObserver.set(1000);
		
		System.out.println(o1.getMyState());
		System.out.println(o2.getMyState());
		System.out.println(o3.getMyState());
		
		concreateObserver.set(2000);
		
		System.out.println(o1.getMyState());
		System.out.println(o2.getMyState());
		System.out.println(o3.getMyState());
	}
	
	
}

 

Published 194 original articles, won 10 praises and 60000 visitors+
Private letter follow

Keywords: Java React

Added by llirik on Fri, 31 Jan 2020 18:32:53 +0200