Schema Beans. Langage temperature Schema temperature Publié par...

34
Schema Beans

Transcript of Schema Beans. Langage temperature Schema temperature Publié par...

Page 1: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Schema Beans

Page 2: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Langage temperature

Page 3: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Schema temperature

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace=”temperature" xmlns="temperature" elementFormDefault="qualified">

<xsd:element name="temperature" type="Temperature"/>

<xsd:complexType name="Temperature"> <xsd:sequence> <xsd:element name="min" type="xsd:int"/> <xsd:element name="max" type="xsd:int"/> </xsd:sequence> </xsd:complexType>

</xsd:schema>

Page 4: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Document instance XML

<temperature xmlns='temperature' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='temperature temperature.xsd'>

<min>3</min><max>12</max>

</temperature>

Page 5: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

•Une classe, tout comme un schéma est un modèle

•A partir d’une classe, on peut créer des objets instance, tout comme à partir d’un schéma on peut créer des documents instance

Classes

Page 6: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Structure d’une classe

ChampsConstructeurMéthodes Une classe Temperature doit se trouver

dans un fichier de nom Temperature.java

Page 7: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Classes

Champs

Méthodes

Le constructeur est une méthode servant à créer des objets à partir du modèle. Le nom du constructeur est celui de la classe

new

Page 8: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Objets

•Conformes au modèle décrit dans la classe

•Offrent les méthodes décrites dans la classe

•Ont les champs décrits dans la classe

Page 9: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Programme Java

JVM

Main new

Démarrage du programme par “java Main”. Java désigne la machine virtuelle

Page 10: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

•Nous allons voir 3 formes de classes équivalentes au schéma temperature

•Champs public avec accés direct

•Champs privés avec accés par méthodes get/set:

•Méthodes déclarées localement

•Méthodes publiées dans une interface

Classes Java

Page 11: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Champs public avec accés direct

Page 12: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Classe Temperature

public class Temperature {//champs public int min; public int max;//constructeur public Temperature { }}

Champs public: accés direct aux valeurs

Page 13: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Utilisationpublic class Main {

public Main() { }

public static void main(String args[]) { Temperature t = new Temperature(); t.min = 6 ; t.max = 22 ; double m = (t.min + t.max)/2.0; }

}

accés direct aux valeurs

utilisation du constructeur

Page 14: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Champs privés avec accés par méthodes get/set

méthodes déclarées localement

Page 15: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

get/set

| get sert à retirer une copie de la valeur| set sert à déposer une valeur

(effacement de l’ancienne)

get/set sont des méthodes permettant l’accés aux champs losrque ceux-ci ne sont pas public

Page 16: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Bean Temperaturepublic class Temperature {//champs private int min; private int max;//constructeur public Temperature { }//methodes d’acces public int getMin() { return min; } public int getMax() { return max; }//methodes de mutation public void setMin(int m) {min=m;} public void setMax(int m) {max=m;}}

Une bean est une classe qui a des méthodes set/get d’accés aux champs

Page 17: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Utilisationpublic class Main {

public Main() { }

public static void main(String args[]) { Temperature t = new Temperature(); t.setMin(6) ; t.setMax(22) ; double m = (t.getMin() + t.getMax())/2.0; }

}

Page 18: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Champs privés avec accés par méthodes get/set méthodes publiées

dans une interface

Page 19: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Interfaces| Une interface est un contrat | Une interface est un engagement

de fournir les méthodes dont la liste se trouve dans le contrat

Page 20: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Interface Temperature

public interface Temperature {

//methodes d’acces public int getMin(); public int getMax();

//methodes de mutation public void setMin(int m); public void setMax(int m);

}

Les interfaces sont des contrats entre un producteur de classes et un utilisateur de classes

Page 21: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Bean TemperatureImpl

public class TemperatureImpl implements Temperature { private int min; private int max; public TemperatureImpl() { }

//méthodes d’accés public int getMin() { return min; } public int getMax() { return max; }

//méthodes de mutation public void setMin(int m) { min = m; } public void setMax(int m) { max = m; } }

Con

tra

tre

mp

li

Page 22: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Utilisationpublic class Main {

public Main() { }

public static void main(String args[]) { Temperature t = new TemperatureImpl(); t.setMin(6) ; t.setMax(22) ; double m = (t.getMin() + t.getMax())/2.0; } }

Seule modification

Page 23: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Interfaces

•Evolution dans le temps

•Changement de l’implementation sans remise en cause du code client

•Masquage d’une implémentation

•Modularité

•une classe peut implémenter plusieurs interfaces

Page 24: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Beans & UML

Page 25: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Beans: get/set

•Les beans sont des classes Java particulières

•Containeurs de données

•Accés aux données par des méthodes de nom getNomDeLaDonnée

•Modification des valeurs par des méthodes de nom setNomDeLaDonnée

Page 26: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Représentation UML

- pour private

+ pour public

Cas d’une “bean” sans interface

Page 27: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Représentation UML

- pour private

+ pour public

Cas d’une “bean” avec interface

Page 28: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Equivalence Types simples

xsd:string java.lang.Stringxsd:integer java.math.BigIntegerxsd:int intxsd.long longxsd:short shortxsd:decimal java.math.BigDecimalxsd:float floatxsd:double doublexsd:boolean booleanxsd:byte bytexsd:QName javax.xml.namespace.QNamexsd:dateTime java.util.Calendarxsd:base64Binary byte[]xsd:hexBinary byte[]xsd:unsignedIn longxsd:unsignedShort intxsd:unsignedByte shortxsd:time java.util.Datexsd:date java.util.Datexsd:anySimpleType java.lang.String

Des types simples xsd deviennent des types complexes Java

Le nom d’un type complex Java (classe)

commence toujours par une majuscule

Page 29: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Documents & Objets

new

documents Objets

Page 30: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Document instance XML

<temperature xmlns='temperature' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='temperature temperature.xsd'>

<min>3</min><max>12</max>

</temperature>

Page 31: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

•Création directe par new

•Création indirecte par une fabrique d’objets

Création d’un objet

Page 32: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Création directepublic class Main {

public Main() { }

public static void main(String args[]) { Temperature t = new Temperature(); t.min = 6 ; t.max = 22 ; double m = (t.min + t.max)/2.0; }

}

Page 33: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Pattern ObjectFactory

•public class Main {• public static void main(String[] args) {

• Temperature t = ObjectFactory.createTemperature();

• t.setMin(3) ;• t.setMax(34) ;

• }•}// end class

Page 34: Schema Beans. Langage temperature Schema temperature                                  Publié par Lance Fontaine,  Modifié il y a 8 ans

Programme Java

JVM

Main

ObjectFactory

new

new

Démarrage du programme par “java Main”. Java désigne la machine virtuelle

Temperature