IMPLANTER LE PARADIGME MVC AVEC RMI

doit notifier tous les contrôleurs – enregistrés comme l'observant. Pour cela, il maintient une liste de références de contrôleurs sous la forme d'Observer et leur ...
187KB taille 4 téléchargements 333 vues
1

IMPLANTER LE PARADIGME MVC AVEC RMI

™ RAPPELS : MVC en Java : L’API Java fournit une implémentation du paradigme MVC (Modèle Vues Contrôleurs) dans le package java.util. Ce package fournit : - pour les modèles : la classe java.util.Observable, - pour les vues : l’interface java.util.Observer. Par ailleurs, les contrôleurs sont en partie cachés dans la JVM et leurs parties visibles sont les {*}Listener liés aux traitements des évènements relatifs aux composants interactifs des vues. ™ Le PROBLÈME du MVC en RMI : Pour permettre aux vues de se mettre à jour d’un changement d’état du modèle, ce dernier doit notifier tous les contrôleurs – enregistrés comme l’observant. Pour cela, il maintient une liste de références de contrôleurs sous la forme d’Observer et leur envoie un message update. Or, dans un environnement RMI, cette communication « modèle vers contrôleurs » est de type OD-client vers OD-serveur. Et ce sont les contrôleurs qui doivent être des OD-objets (supportant une OD-méthode update). Or, tout OD-objet doit implémenter l’interface Remote et, de plus, cette interface doit être visible par l’OD-client au moment où il invoque une OD-méthode. Mais avec l’implémentation MVC des modèles, cela n’est pas possible puisque ceux-ci ne maintiennent une visibilité des contrôleurs qu’au travers de l’interface Observer qui n’a pas la qualité Remote ! Une solution serait de faire en sorte que les contrôleurs implémentent à la fois Oberver et Remote. L’OD-objet contrôleur serait donc bien un Remote mais comme le modèle ne le voit que comme un Observer, la qualité Remote ne lui est toujours pas accessible ! Pour tenter de palier à ce problème, il faudrait faire en sorte que l’interface Observer hérite de Remote mais cela n’est pas possible puisqu’elle est une interface de l’API Java. ™ La SOLUTION : Deux solutions : (1) encapsuler ou (2) redéfinir l’implémentation MVC de Java au travers de nouvelles classes et interfaces compatibles avec la technique RMI.

Java RMI

Y. Laborde

Cours LOO - TOA

2

™ L’IMPLÉMENTATION MVC actuelle de Java :

IMPLÉMENTATION MVC actuelle de Java MonContrôleur1 Observer MonModèle Observable

p

MonInterfaceModèle

o

ActionListener MonContrôleur2 Observer ItemListener MonContrôleur3 Observer

WhatChanged

TextListener

ACTIONS de l’UTILISATEUR

n

(souris / clavier)

q q

MaVue1 MaVue1’ MaVue2

MaVue3

n Action de l’utilisateur sur les composants actifs de l’interface graphique o Transmission au modèle (vu au travers de son interface) de l’action à effectuer p Notification à tous les contrôleurs inscrits d’un changement du modèle q Mise à jour des vues en fonction des modifications du modèle (par utilisation de WhatChanged)

Java RMI

Y. Laborde

Cours LOO - TOA

3

™ DOCUMENTATION Java de MVC depuis le JDK 1.0 : java.util

Class Observable java.lang.Object java.util.Observable public class Observable extends Object

This class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed. An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the Observable's notifyObservers method causes all of its observers to be notified of the change by a call to their update method. The order in which notifications will be delivered is unspecified. The default implementation provided in the Observable class will notify Observers in the order in which they registered interest, but subclasses may change this order, use no guaranteed order, deliver notifications on separate threads, or may guarantee that their subclass follows this order, as they choose. Note that this notification mechanism is has nothing to do with threads and is completely separate from the wait and notify mechanism of class Object. When an observable object is newly created, its set of observers is empty. Two observers are considered the same if and only if the equals method returns true for them. Since: JDK1.0 See Also: notifyObservers(), notifyObservers(java.lang.Object), Observer, Observer.update(java.util.Observable, java.lang.Object)

Constructor Summary Observable()

Construct an Observable with zero Observers.

Method Summary void addObserver(Observer o)

Adds an observer to the set of observers for this object, provided that it is not the same as some observer already in the set. protected

void clearChanged()

Indicates that this object has no longer changed, or that it has already notified all of its observers of its most recent change, so that the hasChanged

Java RMI

Y. Laborde

Cours LOO - TOA

4

method will now return false. int countObservers()

Returns the number of observers of this Observable object. void deleteObserver(Observer o)

Deletes an observer from the set of observers of this object. void deleteObservers()

Clears the observer list so that this object no longer has any observers. boolean hasChanged()

Tests if this object has changed. void notifyObservers()

If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed. void notifyObservers(Object arg)

If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed. protected

void setChanged()

Marks this Observable object as having been changed; the hasChanged method will now return true.

java.util

Interface Observer public interface Observer

A class can implement the Observer interface when it wants to be informed of changes in observable objects. Since: JDK1.0 See Also: Observable

Method Summary void update(Observable o, Object arg)

This method is called whenever the observed object is changed.

Java RMI

Y. Laborde

Cours LOO - TOA