Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

observer pattern java

class Signal extends Observable {

  void setData(byte[] lbData){
    setChanged(); // Positionne son indicateur de changement
    notifyObservers(); // (1) notification
  }
}
Comment

observer pattern java

class JPanelSignal extends JPanel implements Observer {

  void init(Signal lSigAObserver) {
    lSigAObserver.addObserver(this); // (2) ajout d'observateur
  }

  void update(Observable observable, Object objectConcerne) {
    repaint();  // (3) traitement de l'observation
  }
}
Comment

Java Observer pattern

// Java Observer pattern (Simple version)
public interface Subject {
	public void registerObserver(Observer o);
	public void removeObserver(Observer o);
	public void notifyObservers();
}

public interface Observer {
	public void update(int value);
}

public class SimpleSubject implements Subject {
	private List<Observer> observers;
	private int value = 0;
	
	public SimpleSubject() {observers = new ArrayList<Observer>();}
	
	public void registerObserver(Observer o) {observers.add(o);}
	
	public void removeObserver(Observer o) {observers.remove(o);}
	
	public void notifyObservers() {
		for (Observer observer : observers) {
			observer.update(value);
		}
	}
	
	public void setValue(int value) {
		this.value = value;
		notifyObservers();
	}
}

public class SimpleObserver implements Observer {
	private int value;
	private Subject simpleSubject;  // who to subscribe
	
	public SimpleObserver(Subject simpleSubject) {
		this.simpleSubject = simpleSubject;
		simpleSubject.registerObserver(this);
	}
	
	public void update(int value) {
		this.value = value;
		display();
	}
	
	public void display() {System.out.println("Value: " + value);}
}

public class Example {
	public static void main(String[] args) {
		SimpleSubject simpleSubject = new SimpleSubject();
		SimpleObserver simpleObserver = new SimpleObserver(simpleSubject);
		simpleSubject.setValue(80);		// output: 80
		simpleSubject.removeObserver(simpleObserver);
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: static int java 
Java :: how to detect device javascirpt 
Java :: write input stream to file java 
Java :: set remove element java 
Java :: initialize class java 
Java :: java sealed class 
Java :: multiplication of two numbers in java using scanner 
Java :: how to get the average from a list in java 
Java :: how to add input in array java 
Java :: nested for each loop java 
Java :: java print 2d char array 
Java :: java to check if its a number scanner 
Java :: android play sound file from assets 
Java :: java thread class sleep 
Java :: java distinct by key 
Java :: overloading + operator in java 
Java :: core java tutorial 
Java :: how to convert milliseconds into second in android studio 
Java :: java protected 
Java :: create new empty list java 
Java :: fileinputstream 
Java :: find first element of list java 
Java :: map string string to java object 
Java :: array.sort in java 
Java :: convert javascritp getTime() to datetime 
Java :: empty set java 
Java :: java get relative path 
Java :: volley library 
Java :: get string of radio button in android 
Java :: send action bar message bukkit 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =