Source de Prophete.java du package prophet_moins1
package prophet_moins1;
public class Prophete {
private String nom = null;
public String prophetie = null;
public Prophete (String sonNom) {
nom = sonNom;
prophetie = "";
}
public String getNom () {
return nom;
}
public String declame() {
return prophetie;
}
public void proclame(String nouveauDelire) {
prophetie = nouveauDelire;
}
public String toString () {
return "prophete "+nom+" dit : "+prophetie;
}
}
|
Source de Disciple.java du package prophet_moins1
package prophet_moins1;
public class Disciple {
private String nom = "";
public String pensee = "";
private Prophete prophete = null;
public Disciple (String sonNom, Prophete sonProphete) {
nom = sonNom;
prophete = sonProphete;
pensee = prophete.prophetie ;
}
public String getNom() {
return nom;
}
public Prophete adore() {
return prophete;
}
public void recoitLenseignement() {
pensee = prophete.declame();
}
public String toString() {
return "Disciple "+getNom()+" pense : "+pensee;
}
}
|
Source de Sondage.java du package prophet_moins1
package prophet_moins1;
public class Sondage {
public static void main (String args[]) {
Prophete prophete = new Prophete("Brian");
prophete.proclame("Peace and Love !");
System.out.println(prophete.toString());
Disciple disciple1 = new Disciple("Dupont", prophete);
Disciple disciple2 = new Disciple("Durand", prophete);
System.out.println(disciple1.toString());
System.out.println(disciple2.toString());
prophete.prophetie = "Alleluia !";
System.out.println(prophete.toString());
System.out.println(disciple1.toString());
System.out.println(disciple2.toString());
System.out.println("mise a jour de la pensee des disciples :");
disciple1.pensee = prophete.prophetie;
disciple2.recoitLenseignement();
System.out.println(disciple1.toString());
System.out.println(disciple2.toString());
}
}
|
COMPILATION
|
|
Source de Prophete.java du package prophet0
package prophet0;
public class Prophete {
private String nom = null,
prophetie = null;
public Prophete (String sonNom) {
nom = sonNom;
prophetie = "";
}
public String getNom () {
return nom;
}
public String getProphetie () {
return prophetie;
}
public void setProphetie (String nouveauDelire) {
prophetie = nouveauDelire;
}
public String toString () {
return "prophete "+nom+" dit : "+prophetie;
}
|
Source de Disciple.java
du package prophet0
package prophet0;
public class Disciple {
private String nom = "";
private String pensee = "";
private Prophete prophete = null;
public Disciple (String sonNom, Prophete sonProphete) {
nom = sonNom;
prophete = sonProphete;
pensee = prophete.getProphetie();
}
public String getNom() {
return nom;
}
public String getPensee() {
return pensee;
}
public Prophete getProphete() {
return prophete;
}
public String toString() {
return "Disciple "+getNom()+" pense : "+getPensee();
}
public void miseAJourPensee () {
pensee = prophete.getProphetie();
}
}
|
Source de Sondage.java
du package prophet0
package prophet0;
public class Sondage {
public static void main (String args[]) {
Prophete prophete = new Prophete("Brian");
prophete.setProphetie("Peace and Love !");
System.out.println(prophete.toString());
Disciple disciple1 = new Disciple("Dupont", prophete);
Disciple disciple2 = new Disciple("Durand", prophete);
System.out.println(disciple1.toString());
System.out.println(disciple2.toString());
prophete.setProphetie("Alleluia !");
System.out.println(prophete.toString());
System.out.println(disciple1.toString());
System.out.println(disciple2.toString());
System.out.println("mise a jour de la pensee des disciples :");
disciple1.miseAJourPensee();
disciple2.miseAJourPensee();
System.out.println(disciple1.toString());
System.out.println(disciple2.toString());
}
}
|
Nous
voudrions que le prophète avertisse ses disciples de son
changement de prophétie.
Source de Disciple.java du package prophet1
package prophet1;
public class Disciple
implements ProphetieChangeListener {
....
....
public void prophetieChanged (ProphetieChangedEvent evt) {
pensee = evt.getProphetie();
}
}
|
package prophet1;
public class ProphetieChangedEvent extends java.util.EventObject
{
protected String laProphetie;
public ProphetieChangedEvent(Object source, String uneProphetie)
{
super(source);
laProphetie = uneProphetie;
}
public String getProphetie()
{
return laProphetie;
}
}
|
package prophet1;
public interface ProphetieChangeListener extends java.util.EventListener
{
void prophetieChanged(ProphetieChangedEvent evt);
}
|
package prophet1;
import java.util.Vector;
public class Prophete {
private String nom = null;
private String prophetie = null;
private Vector prophetieChangeListeners = new Vector();
.....
.....
public void setProphetie (String nouveauDelire) {
if (! prophetie.equals(nouveauDelire)) {
prophetie = nouveauDelire;
this.notifyProphetieChange();
}
}
public void addProphetieChangeListener(ProphetieChangeListener l)
{
if (!prophetieChangeListeners.contains(l))
prophetieChangeListeners.addElement(l);
}
public void removeProphetieChangeListener(ProphetieChangeListener l)
{
if (prophetieChangeListeners.contains(l))
prophetieChangeListeners.removeElement(l);
}
protected void notifyProphetieChange()
{
ProphetieChangedEvent evt =
new ProphetieChangedEvent(this, this.getProphetie());
for (int i = 0; i < prophetieChangeListeners.size(); i++)
{
ProphetieChangeListener client =
(ProphetieChangeListener)prophetieChangeListeners.elementAt(i);
client.prophetieChanged(evt);
}
}
}
|
package prophet1;
public class Sondage {
public static void main (String args[]) {
Prophete prophete = new Prophete("Le Chat");
prophete.setProphetie("Et pourquoi n'a-t-on jamais les pieds qui sentent les chaussettes ?");
System.out.println(prophete.toString());
Disciple disciple1 = new Disciple("Dupont");
Disciple disciple2 = new Disciple("Durand");
System.out.println(disciple1.toString());
System.out.println(disciple2.toString());
prophete.setProphetie("\nSi les lentilles vous font peter, portez des lunettes !");
System.out.println(prophete.toString());
System.out.println(disciple1.toString());
System.out.println(disciple2.toString());
System.out.println("\nConnectons les disciples sur le prophete:");
prophete.addProphetieChangeListener(disciple1);
prophete.addProphetieChangeListener(disciple2);
prophete.setProphetie("la droite est un peu gauche, mais la gauche n'est pas tres adroite non plus !");
System.out.println(prophete.toString());
System.out.println(disciple1.toString());
System.out.println(disciple2.toString());
}
}
|
EXECUTION
|
|
Remarquons que la gestion des listeners n'est pas thread-safe : des threads parallèles peuvent ajouter, enlever et essayer de notifier la liste des écouteurs sana exclusion mutuelle !
Source de Prophete.java du package prophet6
package prophet6;
import java.util.Vector;
public class Prophete {
...
...
public synchronized void
addProphetieChangeListener(ProphetieChangeListener l) {
if (!prophetieChangeListeners.contains(l))
prophetieChangeListeners.addElement(l);
}
public synchronized void
removeProphetieChangeListener(ProphetieChangeListener l){
if (prophetieChangeListeners.contains(l))
prophetieChangeListeners.removeElement(l);
}
protected void notifyProphetieChange()
{
ProphetieChangedEvent evt =
new ProphetieChangedEvent(this, this.getProphetie());
Vector listeCopie;
synchronized(this) {
listeCopie = (Vector) prophetieChangeListeners.clone();
}
// desormais un client de listeCopie peut se "remover" de la liste
// officielle les listeners
int nbre = listeCopie.size();
for (int i = 0; i < nbre; i++) {
ProphetieChangeListener client =
(ProphetieChangeListener)listeCopie.elementAt(i);
client.prophetieChanged(evt);
}
}
}
|
Source de Prophete.java du package prophet7
package prophet7;
import java.io.*;
...
public class Prophete
implements Serializable {
|
package prophet7;
import java.io.*;
public class Disciple
implements ProphetieChangeListener, Serializable {
|
import prophet7.*;
import java.io.*;
import java.util.*;
public class Secte implements Serializable {
private Prophete prophete;
private Vector lesDisciples;
public static void main (String args[]) {
Secte secte = null;
File fichierSecte = new File("secte");
if (! fichierSecte.exists())
secte = new Secte();
else
secte = restauration();
if (secte != null) {
secte.sondage();
secte.saisieNouvelleprophetie();
secte.sondage();
secte.sauvegarde();
}
}
public Secte () {
prophete = new Prophete("Le Chat");
Disciple disciple1 = new Disciple("Dupont");
Disciple disciple2 = new Disciple("Dujeune");
prophete.addProphetieChangeListener(disciple1);
prophete.addProphetieChangeListener(disciple2);
prophete.setProphetie("La violence à la television\n"
+"ca donne envie aux jeunes de tout casser\n"
+"sauf, helas, la television");
lesDisciples = new Vector();
lesDisciples.add(disciple1);
lesDisciples.add(disciple2);
}
public void sondage() {
System.out.println(prophete.toString());
for (int i=0; i<lesDisciples.size(); i++)
System.out.println(lesDisciples.elementAt(i).toString());
}
public void saisieNouvelleprophetie() {
String encoreUne = null;
do {
System.out.println("quelle est la nouvelle prophetie ?");
encoreUne = Clavier.lireLigne().trim();
}
while ((encoreUne == null) && encoreUne.equals(""));
prophete.setProphetie(encoreUne);
}
private void sauvegarde () {
try {
ObjectOutputStream flotEcriture =
new ObjectOutputStream(
new FileOutputStream(new File("secte")));
flotEcriture.writeObject(this);
flotEcriture.close();
System.out.println("sauvegarde dans le fichier secte");
} catch (IOException e) {
System.out.println(" erreur :" + e.toString());
}
}
private static Secte restauration () {
try {
ObjectInputStream flotLecture =
new ObjectInputStream(
new FileInputStream(new File("secte")));
Object lu = flotLecture.readObject();
if (lu instanceof Secte)
return (Secte)lu;
else {
System.out.println("pas une secte");
return null ;
}
} catch (Exception e) {
System.out.println(" erreur :" + e.toString());
return null ;
}
}
}
|
EXECUTION
|
|
private transcient Vector prophetieChangeListeners = new Vector();
private void writeObject(ObjectOutputStream flot)
throws IOException {
flot.defaultWriteObject();
Vector listeCopie;
synchronized(this)
{
listeCopie = (Vector) prophetieChangeListeners.clone();
}
int cnt = listeCopie.size();
for (int i = 0; i < cnt; i++)
{
ProphetieChangeListener ecouteur = (ProphetieChangeListener)listeCopie.elementAt(i);
if (ecouteur instanceOf Serializable)
flot.writeObject(ecouteur);
}
flot.writeObject(null);
}
private void readObject(ObjectInputStream flot)
throws IOException {
flot.defaultReadObject();
try {
Object lu;
while ((lu = flot.readObject()) != null)
prophetieChangeListeners.add((ProphetieChangeListener)lu);
}
catch (ClassNotFoundException e) {
throw new IOException(e.getMessage());
}
}
|
Source de Prophete.java du package prophet9
|
|
Source de SerialXML.java du package prophet9
package prophet9;
import java.beans.*;
import java.io.*;
public class SerialXML {
public static void main(String[] args) {
try {
XMLEncoder e = new XMLEncoder(new BufferedOutputStream
(new FileOutputStream("prophet.xml")));
Prophete p = new Prophete("Le Chat");
p.setProphetie("I'm bo");
e.writeObject(p);
e.close();
} catch (Throwable t) {
System.out.println("erreur encodage");
t.printStackTrace();
System.exit(1);
}
System.out.println("encodage termine");
try {
XMLDecoder d = new XMLDecoder(new BufferedInputStream
(new FileInputStream("prophet.xml")));
Object o = d.readObject();
d.close();
System.out.println(o);
} catch (Throwable t) {
System.out.println("erreur decodage");
t.printStackTrace();
}
}
}
|
EXECUTION
|
|
Source de SerialXMLFrame.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.beans.*;
public class SerialXMLFrame {
public static void main(String[] args) {
try {
JFrame frame = new JFrame();
frame.setTitle("le titre");
Container c = frame.getContentPane();
JLabel label = new JLabel("un label");
c.add(label);
JButton bouton = new JButton("un bouton");
c.add(bouton);
XMLEncoder e = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("sauve.xml")));
e.writeObject(frame);
e.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
|
EXECUTION
|
|
Ecrire un bean "indicateur
de niveau" nommé NiveauBean1 :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TestNiveauBean1 extends JFrame
implements ChangeListener
{
private NiveauBean1 niveauBarre;
private JSpinner spinner;
public TestNiveauBean1()
{
super("TestNiveauBean1");
JFrame.setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
Container pane = getContentPane();
niveauBarre = new NiveauBean1();
pane.add("Center", niveauBarre);
spinner = new JSpinner(new SpinnerNumberModel(0,0,100,1));
pane.add("South", spinner);
spinner.addChangeListener(this);
pack();
setVisible(true);
}
public void stateChanged(ChangeEvent e) {
Integer i = (Integer)spinner.getValue();
niveauBarre.setPosition(i.intValue());
}
public static void main(String args[]) {
new TestNiveauBean1();
}
}
|