Académique Documents
Professionnel Documents
Culture Documents
Association unidirectionnelle:
class Cercle{
Point centre;
double rayon;
Cercle(int x, int y, double r){
this.centre = new Point (x, y);
rayon = r;
}
}
La classe Point ne connaît pas l'existence de la classe class Point{
Cercle (unidirectionnelle). int x;
Un point peut être partagé (à un même moment il
int y;
peut être lié à plusieurs instances d'objets
(éventuellement d'autres classes). Point(int x, int y){
Or, on voulait que Le Point représentant le centre this.x = x;
n’est pas partagé (à un même moment, une this.y = y;
instance de Point ne peut être lié qu'à un seul }
Cercle) }
9
0..1 0..1
10
Exemple 1
public class person {
telephone outil;
String nom;
person(String nom){
this.nom = nom;
}
public void addOutil(telephone outil) {
if( outil != null ){
if (outil.getPropri() !=null ){ //si outil est déjà connecté à une autre Person
outil.getPropri().setOutil(null); // cette autre Person doit se déconnecter
}
this.setOutil(outil);
outil.setPropri(this );
}
}
Exemple 1
public class telephone {
person propri;
String marque;
telephone (String s){
marque =s;
}
public void addPropri( person propri ) {
if( propri != null ) {
if (propri.getOutil() != null){//si proprio est déjà connecté à un autre telephone
propri.getOutil().setPropri( null ); // cet autre telephone doit se déconnecter
}
this.setPropri( propri );
propri.setOutil( this );
}
}
public void setPropri(person propri){ this.propri=propri; }
public person getPropri(){ return(propri); }
}
12
Exemple 1
public class test{
public static void main (String [] args) {
telephone t = new telephone("nokia");
if (p2.getOutil() == t)
System.out.println("t est le téléphone de Iyed");
if (p.getOutil() == t)
System.out.println("t est le téléphone de Salah");
System.out.println(p2.getOutil().marque); //renvoie une exception car p2.outil est null
} // p2 (Iyed n’a plus de téléphone)
}
13
Exemple 2
public class Personne { void CréerCompte(int n) {
cpt = new Compte(n,this);
String nom; cpt.setprop(this);
String prenom; }
float revenu; void afficher(){
Compte cpt;
Personne(String n,String p,float r){ }
nom = n; Compte getCompte(){
prenom = p; return cpt;
revenu = r; }
} }
15
Exemple 2
void créditer(float s){
public class Compte { solde+=s;
int numero; }
float solde; void débiter(float s){
Personne proprietaire; solde-=s;
Compte(int num, Personne propri){ }
numero = num; void afficher(){
solde = 0;
if (propri!=null) { }
void setprop (Personne p) {
if (propri.getCompte() != null) { proprietaire = p;
//si proprio est déjà connecté à un autre compte }
Personne getprop () {
propri.getCompte().setprop(null); return proprietaire ;
// cet autre compte doit se déconnecter }
} void fermerCompte(){
setprop (propri); // this.finalize();
} }
}
}
16
Exemple 2
Exercice
À partir des classes représentées au format UML, écrire le
code en java.
19
Exercice (suite)
public class Crayon {
private char type;
private String couleur;
public Crayon (char leType , String laCouleur) {
this.type = leType;
this.couleur = laCouleur;
}
public char getType() {
return this.type;
}
public String toString() {
return "Type:"+this.type+" Couleur:"+this.couleur;
}
}
20
Exercice (suite)
public class Etudiant {
private String nom;
private String prenom;
// Constructeur
public Etudiant (String unNom , String unPrenom){
this.nom = unNom;
this.prenom = unPrenom;
}
// toString
public String toString(){
return this.prenom+" "+this.nom;
}
}
21
Exercice (suite)
class Test{
public static void main(String args[]){
Crayon c1 = new Crayon('F' , "Bleu");
System.out.println(c1.toString());
Etudiant e1 = new Etudiant("Friedman", "Milton");
System.out.println(e1.toString());
}
}
22
Exercice (suite)
• Les 2 classes sont indépendantes. Il existe des crayons; il existe des
étudiants mais les étudiants sont indépendants des crayons.
• Nous allons faire évoluer notre cas pour dire qu’un étudiant possède
un crayon.
23
Exercice (suite)
public class Etudiant {
private String nom;
private String prenom;
Comment faire en Java ? private Crayon leCrayon;
//…
}
Exercice (suite)
public class Etudiant {
private String nom;
private String prenom;
private Crayon leCrayon;
public Etudiant (String unNom , String unPrenom){
this.nom = unNom;
this.prenom = unPrenom;
}
public String toString(){
return this.prenom+" "+this.nom;
}
// Méthode qui ajoute un crayon à un étudiant
public void ajouteCrayon(Crayon unCrayon){
this.leCrayon = unCrayon;
}
}
25
Exercice (suite)
Exercice (suite)
public class Etudiant {
private String nom;
private String prenom;
private Crayon leCrayon;
public Etudiant (String unNom , String unPrenom){
this.nom = unNom;
this.prenom = unPrenom;
}
public String toString(){ // Méthode toString pour un meilleur affichage
String message =this.prenom+" "+this.nom;
if (leCrayon !=null)
message=message+" possède le crayon "+leCrayon.toString();
else
message+=" ne possède pas de crayon";
return message;
}
public void ajouteCrayon(Crayon unCrayon){
this.leCrayon = unCrayon;
}
// Méthode qui retire un crayon de l’étudiant
public void perdCrayon(){
this.leCrayon = null;
}
}
27
Exercice (suite)
Exercice (suite)
public class Etudiant {
private String nom;
private String prenom;
private ArrayList trousse;
public Etudiant (String unNom , String unPrenom){ //Constructeur
this.nom = unNom;
this.prenom = unPrenom;
this.trousse = new ArrayList();}
private int nbCrayons(){
return this.trousse.size();}
public String toString(){ //toString
String message =this.prenom+" "+this.nom;
message+=nbCrayons()+" crayons.";
for (int i=0; i<nbCrayons();i++){
message+="\n\t"+((Crayon)(trousse.get(i))).toString();
}
return message;}
public void ajouteCrayon(Crayon unCrayon){//Ajouter un crayon à la trousse
this.trousse.add(unCrayon);}
public void perdCrayon(Crayon unCrayon){
this.trousse.remove(unCrayon);}
}
29
Exercice (suite)
public static void main(String args[]){
Crayon c1 = new Crayon('F' , "Bleu");
System.out.println(c1.toString());
Etudiant e1 = new Etudiant("Friedman", "Milton");
System.out.println(e1.toString());
// Ajoute à l'étudiant e1 le crayon c1
e1.ajouteCrayon(c1);
System.out.println(e1.toString());
// Second crayon
e1.ajouteCrayon(new Crayon('B',"Rouge"));
System.out.println(e1.toString());
// Il perd le premier crayon
e1.perdCrayon(c1);
System.out.println(e1.toString());
}
30
Agrégation
• Les critères suivants impliquent une agrégation:
o une classe fait partie d’une autre classe;
o une partie peut exister indépendamment du tout
Tout Partie
Une agrégation
A B
Agrégation
class Entreprise {
class Service {
Service S;
Entrprise E;
Service getService() {
Entrprise getEntrprise(){
return S;
return E;
}
}
void setService(Service S){
void setEntreprise(Entrprise E){
this.S = S;
this.E = E;
S.setEntreprise(this);
}
}
}
}
33
Agrégation
Composition
Composite Composant
Composition
Voiture Moteur
1 1
Composition
La cohérence et le couplage
40
Cohérence vs incohérence
Incohérence et classe
• Une classe est incohérente si on peut la découper
en plusieurs classes indépendantes.
3
2
1
42
Exemple de LCOM
• Trois paires: {m1, m2}, {m1, m3} et {m2, m3}
• P=2 et Q=1
• P - Q = 1, donc il faut couper la classe!
a1 a2 a3 a4
m1 m2 m3
46
Couplage et classe/interface
Une classe/interface est couplée à une autre, si
elle a besoin d’elle pour être compilée.
▫ Appel de méthodes.
▫ Typage de propriétés.
▫ Héritage.
▫ Implémentation.
47
Couplé vs isolé
48
Un objet couplé à beaucoup d’objets Une chaîne: Plus elle est grande,
Objet trop couplé plus c’est difficile de tester un objet
Objets mal couplés
49
class Moteur {
public int puissance = 0;
public int getPuissance() {
return puissance;
}
public void setPuissance(int p) {
puissance = p;
}}