ExempleCh04_10e_DPObserverAsynchronePushAndPushRetourTélécharger Exemple du DP Observer Cas : Asynchrone push end push ExempleCh04_10e.java // Exemple du DP Observer // Cas : Asynchrone push end push import java.util.*; public class ExempleCh04_10e { public static void main(String[] args) { App a = new App(); ProxyApp proxy = new ProxyApp(a); ObserverFileEvent observerU1 = new ObserverFileEvent(); ObservableThreadPull obsU1 = new ObservableThreadPull(observerU1); proxy.getObservable().addObserver(observerU1); Utilisateur u1 = new Utilisateur("U1",obsU1); Utilisateur u2 = new Utilisateur("U2",obsU1); traitement(proxy); } static public void traitement(AppInt app) { for(int i=0;i<10;i++) { if (i!=5) { app.setEtat1(i); System.out.println(String.format("%30s","setEtat1 de "+i)); app.setEtat2(i*100); System.out.println(String.format("%30s","setEtat2 de "+(i*100))); } app.setValide(true); try{Thread.sleep(1000);}catch(Exception ex){}; } } } // ------------------------------------ interface AppInt { public void setEtat1(int v); public void setEtat2(int v); public void setValide(boolean v); } // ------------------------------------ class ProxyApp implements AppInt { private ObservableApp obs; private App app; public ProxyApp(App a) { app=a; obs = new ObservableApp(); } public void setEtat1(int v) { app.setEtat1(v); obs.setChanged(); } public void setEtat2(int v) { app.setEtat2(v); obs.setChanged(); } public void setValide(boolean v) { app.setValide(v); if (app.getValide()) { String arg = app.getEtat1()+" "+app.getEtat2(); // On fait la notification en asynchrone new ThreadNotify(obs,arg); } } public Observable getObservable(){return obs;} } // ------------------------------------ class ThreadNotify extends Thread { private Observable obs; private Object arg; public ThreadNotify(Observable obs,Object arg) { this.obs = obs; this.arg = arg; start(); } public void run() { try{ obs.notifyObservers(arg); } catch(Exception ex){}; } } // ------------------------------------ class App implements AppInt { private int etat1; private int etat2; private boolean valide; public App() { etat1 = 1; etat2 = 100; valide = false; } public void setEtat1(int v) { etat1 = v; } public void setEtat2(int v) { etat2 = v; } public void setValide(boolean v) { valide = v; } public boolean getValide() {return valide;} public int getEtat1(){return etat1;} public int getEtat2(){return etat2;} } // ------------------------------------ class ObservableApp extends Observable { public void setChanged() // car setChanged est protected { super.setChanged(); } } // ------------------------------------ class ObserverFileEvent implements Observer { private ArrayList fileEvents; public ObserverFileEvent() { fileEvents = new ArrayList(); } public void update(Observable o,Object arg) { fileEvents.add(arg); } public Object nextEvent() { if (fileEvents.isEmpty()) return null; Object o = fileEvents.get(0); fileEvents.remove(0); return o; } } // ------------------------------------ class ObservableThreadPull extends Observable implements Runnable { ObserverFileEvent observerFile; public ObservableThreadPull(ObserverFileEvent observer) { observerFile = observer; Thread t = new Thread(this); t.start(); } public void run() { try{ while(true) { Object arg = observerFile.nextEvent(); if (arg!=null) { setChanged(); // On fait la notification en asynchrone new ThreadNotify(this,arg); } Thread.sleep(2000); } }catch(Exception ex){} } } // ------------------------------------ class ObserverApp implements Observer { Utilisateur util; public ObserverApp(Utilisateur util) { this.util = util; } public void update(Observable o,Object arg) { String s = (String)arg; util.traitement(s); // APPEL AU TRAITEMENT } } // ------------------------------------ class Utilisateur { private ObserverApp observer; private String ident; public Utilisateur(String ident,Observable observable) { this.ident = ident; observer = new ObserverApp(this); // this observable.addObserver(observer); } // traitement de utilisateur appelé public void traitement(String v) { System.out.println(ident+" "+v); // Pour simuler un cas de traitement long par l'utilisateur if ( v.contains("700") && ident.equals("U1") ) attente(10,250,"x"); } public static void attente(int nb,int ms,String trace) { for(int i=0;i<nb;i++) { try{Thread.sleep(ms);}catch(Exception ex){}; System.out.print(trace+" "); } System.out.println(); } } compil.bat mkdir bin del /f /s /q bin\*.class javac -d bin *.java pause run.bat cd bin java ExempleCh04_10e pause