There is no such thing as perfect programming, but we shouldn’t be discouraged because programming is a constant pursuit of perfection.

  1. Intents are what I think of as an event notification mechanism, where a change in the state of one object is told to other objects by observers
  2. The class diagram

  3. The instance
// Interface SimpleObserver{void update(SimpleObservable SimpleObservable, Object arg); } // The object class SimpleObservable{// Status flag private volatile Boolean changed = false; Protected synchronized void setChanged(){changed = true; } public synchronized boolean hasChanged(){ return changed; } protected synchronized void clearChanged(){ changed = false; Private Vector<SimpleObserver> observerVector; public SimpleObservable(){ observerVector = new Vector<>(); } public synchronized void addObserver(SimpleObserver simpleObserver){ if(null == simpleObserver){ throw new NullPointerException(); }else if(! observerVector.contains(simpleObserver)){ observerVector.addElement(simpleObserver); } } public synchronized void deleteObserver(SimpleObserver simpleObserver){ observerVector.removeElement(simpleObserver); } public synchronized void deleteObservers(){ observerVector.removeAllElements(); } // Note that state changes require sync, but observer.update does not require public void notifyObservers(Object arg){synchronized (this){if(! changed) return; clearChanged(); } observerVector.forEach(observer -> observer.update(this, arg)); } public void notifyObservers(){ notifyObservers(null); } public synchronized int countObservers(){ return observerVector.size(); } } class MySimpleObservable extends SimpleObservable{ private int state; public void setState(int state){ this.state = state; this.setChanged(); // Observers are about to change state, changing the state flag to true this.notifyObservers(state); } public int getState() {return state; } } class MySimpleObserver1 implements SimpleObserver{ private Client1 client; public MySimpleObserver1(Client1 client){ this.client = client; } @Override public void update(SimpleObservable simpleObservable, Object arg) { client.plus((int)arg); } } class MySimpleObserver2 implements SimpleObserver{ private Client2 client; public MySimpleObserver2(Client2 client){ this.client = client; } @Override public void update(SimpleObservable simpleObservable, Object arg) { client.multiply((int)arg); } } class Client1{ public void plus(int arg){ System.out.println("client1::plus "+ (20+arg)); } } class Client2{ public void multiply(int arg){ System.out.println("client2::multiply "+ (3*arg)); }}Copy the code
  1. test
Class testObserver{public static void main(String[] args) {MySimpleObserver1 MySimpleObserver1 = new MySimpleObserver1(new Client1()); MySimpleObserver2 mySimpleObserver2 = new MySimpleObserver2(new Client2()); MySimpleObservable MySimpleObservable = new MySimpleObservable(); MySimpleObservable = new MySimpleObservable(); mySimpleObservable.addObserver(mySimpleObserver1); mySimpleObservable.addObserver(mySimpleObserver2); / / state changes observed, observer to inform the client System. Out. The println (" observers count: "+ mySimpleObservable. CountObservers ()); System.out.println("state changed to "+ 1+ ":"); mySimpleObservable.setState(1); System.out.println("state changed to "+ 2+ ":"); mySimpleObservable.setState(2); }}Copy the code

Running results:

observers count: 2
state changed to 1:
client1::plus 21
client2::multiply 3
state changed to 2:
client1::plus 22
client2::multiply 6
Copy the code

Want to see more? Please visit:Design patterns