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 :: Odd or even program in java using a mod operator 
Java :: what are variables in java 
Java :: Read File and Resource in JUnit Test into Stream 
Java :: how to store date in java 
Java :: java integer division to float 
Java :: java gmt zoneid 
Java :: java new string 
Java :: javafx get button id 
Java :: java session timeout 
Java :: getresources in adapter android 
Java :: Implement the static keyword – static variable, static block, static function and static class with following conditions 
Java :: java system.out.println shortcut 
Java :: java program to convert milliseconds to minutes and seconds 
Java :: csv compare java 
Java :: Java comment vérifier une égalité de String 
Java :: Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8 
Java :: Java reduce() Method 
Java :: java swing on close 
Java :: java take and save screenshot 
Java :: stream filter java 8 
Java :: radiogroup get selected item android 
Java :: Java void Keyword 
Java :: convert string to int in java 
Java :: precision java 
Java :: java array quick sort 
Java :: get selected item spinner 
Java :: set up a tree in java 
Java :: How to solve the knapsack problem, in Java? 
Java :: Java char Keyword 
Java :: variable might not have been initialized error 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =