Intro COO
Intro COO
Au programme
Jean-Christophe Routier
Licence mention Informatique
Université Lille – Sciences et Technologies
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 1
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Présentation
cf. http://portail.fil.univ-lille1.fr/portail/ls4/poo
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 2
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
fonctionnement
équipe pédagogique
Bilel Derbel, Thomas Dupriez, Brandon Foubert, Jérémy Lictevout,
Clément Quinton, Jean-François Roos, Yves Roos, Romain Rouvoy,
Jean-Christophe Routier
1h30 de Cours (8 séances), 2h TD, 1h30 TP par semaine
préparer les TD, être acteur !
évaluation continue :
TP à rendre
première session : T P +sup(DS1+2∗DS2,3∗DS2)
4
seconde session : note TP conservée, T P +3×DS3
4
http://portail.fil.univ-lille1.fr/portail/ls5/coo
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 3
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 4
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Pré-requis objet
classes et objets
instances, constructeurs, méthodes, attributs
UML comme notation : diagrammes de classes
modélisation objet d’un problème
analyse d’un problème simple, identification des classes et leur
conception,
polymorphisme des objets
les interfaces, l’héritage,
mécanismes de lookup et liaison tardive (late-binding)
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 5
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Pré-requis java
class, this, private, public
toString, equals/==
static, enum
interface, extends, super
exceptions : capture et levée
try ... catch, throws/throw
java.util :
Collection<E>, Iterator, Iterable
List/ArrayList/LinkedList, Set,
Map/HashMap, hashCode
package, import
javac, java, jar, javadoc, classpath, tests (JUnit)
,→ “Le TP4” de POO à (re)faire (voir semainier)
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 6
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Player
- currentSquare : Square
- nbPoints :int Square
- nbLives : int - index : int
Game
- color : Color - players : List<Player>
- players : List<Player>
+ Player(c: Color) - color : Color
- allSquares : Square[]
+ getSquare() : Square + Square(idx :int, c : Color)
- currentPlayer : Player
+ setSquare(s : Square) + getIndex() : int
+ Game()
+ getPoints() : int + getColor() : Color
+ addPlayer(p: Player)
+ getColor() : Color + addPlayer(p : Player)
+ play()
+ getNbLives() : int + removePlayer(p : Player)
...
+ addPoints(pts : int) + consequence(p : Player)
+ receiveOnLife() ...
+ loseOneLife()
...
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 8
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Player
- currentSquare : Square
- nbPoints :int Square
- nbLives : int - index : int
Game
- color : Color - players : List<Player>
- players : List<Player>
+ Player(c: Color) - color : Color
- allSquares : Square[]
+ getSquare() : Square + Square(idx :int, c : Color)
- currentPlayer : Player
+ setSquare(s : Square) + getIndex() : int
+ Game()
+ getPoints() : int + getColor() : Color
+ addPlayer(p: Player)
+ getColor() : Color + addPlayer(p : Player)
+ play()
+ getNbLives() : int + removePlayer(p : Player)
...
+ addPoints(pts : int) + consequence(p : Player)
+ receiveOnLife() ...
+ loseOneLife()
...
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 8
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 9
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
arghhhh...
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 10
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
re arghhhh...
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 13
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
décomposer
il faut
dé-com-po-ser
responsabilité unique
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 14
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
// move player
fin ishe d =
}
// who i s the winner ?
}
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 16
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
et donc...
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 17
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
avec :
protected boolean isFinished () { responsabilité : déterminer si le jeu est fini
i f ( this . currentPlayer . getNbLives () < 0) {
this . players . remove( this . currentPlayer ) ;
}
return this . currentPlayer . getNbPoints () > WINNING POINTS | | this . players . s i z e () == 1;
}
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 18
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
cool...
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 19
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
consequence
lorsqu’un joueur s’arrête sur une case, la conséquence est qu’il marque
des points et la case produit un effet. Les points marqués sont doublés
si la couleur du joueur est la même que celle de la case. Le nombre de
points marqués et l’effet varient en fonction de la couleur de la case.
Certaines cases font marquer plus de points, d’autre perdre des vies,
etc.
Si la couleur de la case est :
bleu, le joueur marque 100 points et reçoit un bonus entre 10 et 50
points
jaune, le joueur marque 50 points et gagne une vie
orange, la conséquence est le double de celle d’une case jaune
rouge, le joueur marque -100 points et perd une vie
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 20
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
nextSquare.consequence(this.currentPlayer);
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 21
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
dans Square :
/∗∗ add points to the player and apply square e f f e c t . Points and e f f e c t depend
∗ on square color . Won points are d i s p l a i d and player i s n o t i f i e d i f ( s )he has
∗ lost a l l its lives .
∗ @param player the player reaching t h i s square
∗/
public void consequence( Player player ) {
// points management
int points = this . getNbPoints ( ) ;
i f ( player . getColor ( ) . equals ( this . color )) { // double points i f same color
points = 2 ∗ points ;
}
player . addPoints( points ) ;
System . out . p r i n t l n ( player+" gets "+points ) ;
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 22
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
arghhhh...
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 23
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
évolution
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 24
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
polymorphisme + late-binding
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 25
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Squares
Squares
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 27
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 28
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 29
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
public class YellowSquare extends Square { public class Orange Square extends Square {
private static f i n a l int YELLOW POINTS = 50; private static f i n a l int ORANGE POINTS = 100 ;
public void consequence( Player player ) { public void consequence( Player player ) {
// points management // points management
int points = YELLOW POINTS; int points = ORANGE POINTS;
i f ( player . getColor () == this . color ) { i f ( player . getColor () == this . color ) {
points = 2 ∗ points ; points = 2 ∗ points ;
} }
player . addPoints( points ) ; player . addPoints( points ) ;
System . out . p r i n t l n ( player+" gets "+points ) ; System . out . p r i n t l n ( player+" gets "+points ) ;
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 30
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
arghhhh...
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 31
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
utiliser super
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 32
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
respect du contrat :
« la conséquence d’une case orange est le double de celle d’une case jaune »
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 33
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
évolution
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 34
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 35
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Principe ouvert-fermé
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 36
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
late binding
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 37
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
late binding
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 37
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
late binding
for (A a : l i s t e A ) {
System . out . p r i n t l n ("- appel h() -" ) ;
a.h();
System . out . p r i n t l n ("- appel f() -" ) ;
a. f ();
}
}
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 38
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
lookup
this est une référence vers l’objet qui reçoit le message : celui qui
invoque la méthode
Recherche (“lookup”) :
méthode publique :
la recherche commence dans la classe de l’objet invoquant
si aucune définition de la méthode n’est trouvée, on continue la
recherche dans la super-classe
et ainsi de suite
la recherche de la méthode est donc dynamique
si le message porte sur une méthode privée de la classe du receveur :
on prend la définition de la méthode dans la classe où est réalisée l’envoi
de message. Dans ce cas le choix est statique.
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 39
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
super est une référence vers l’objet qui reçoit le message (“invoquant”)
super == this
Mais, recherche de méthode (lookup) avec super différente :
méthode publique :
la recherche commence dans la super-classe de la classe définissant
la méthode utilisant super
le processus de chaı̂nage reste ensuite le même à partir de cette classe
super ne fait pas commencer la recherche de méthode dans la
super-classe de l’objet
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 40
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
factorisation de code
public abstract class Square {
...
public abstract void consequence( Player player ) ;
}
public class YellowSquare extends Square { public class BlueSquare extends Square} {
... ...
public void consequence( Player player ) { public void consequence( Player player ) {
// points management // points management
int points = 50; int points = 100;
i f ( player . getColor ( ) . equals ( this . color )) { i f ( player . getColor ( ) . equals ( this . color )) {
points = 2 ∗ points ; points = 2 ∗ points ;
} }
player . addPoints( points ) ; player . addPoints( points ) ;
System . out . p r i n t l n ( player+" gets "+points ) ; System . out . p r i n t l n ( player+" gets "+points ) ;
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 42
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Comment ?
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 43
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Comment ? BlueSquare
public void consequence(Player player) {
// points management
int points = 100;
if (player.getColor().equals(this.color)) {
points = 2 * points;
}
player.addPoints(points);
System.out.println(player+" gets "+points);
// apply the square effect
player.addPoints(alea.nextInt(40)+10);;
// has the player lost all its lives ?
if (player.getNbLives() <= 0) {
System.out.println(player +" is ...");
}
}
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 43
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Comment ? YellowSquare
public void consequence(Player player) {
// points management
int points = 50;
if (player.getColor().equals(this.color)) {
points = 2 * points;
}
player.addPoints(points);
System.out.println(player+" gets "+points);
// apply the square effect
player.receiveOneLife();
// has the player lost all its lives ?
if (player.getNbLives() <= 0) {
System.out.println(player +" is ...");
}
}
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 43
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 43
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 43
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 45
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
tests
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 46
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
pour rappel
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 47
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Spécification :
l’effet appliqué par la conséquence d’une case jaune est que le joueur
gagne une vie
import static org . j u n i t . Assert . ∗ ;
import org . j u n i t . Test ;
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 48
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Spécification :
Après l’exécution de la méthode setSquare, la méthode
getSquare doit fournir la case affectée.
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 49
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Spécification :
Après l’exécution de la méthode setSquare, la méthode
getSquare doit fournir la case affectée.
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 49
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Spécification :
Après l’exécution de la méthode setSquare, la méthode
getSquare doit fournir la case affectée.
utilisation de « Mock »
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 49
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 50
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 50
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 50
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 50
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 51
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
utilisation de « Mock »
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 51
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 52
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
création d’exception
package squaregame ;
public class UnknownPlayerException extends Exception {
public UnknownPlayerException( String msg) {
super(msg) ;
}
}
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 53
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
removePlayer
throws
throw new ...
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 54
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 55
fil rouge décomposer polymorphisme OCP late-binding factorisation tests exceptions
tests et exceptions
@Test(expected=UnkownPlayerException.class)
Université Lille – Sciences et Technologies - Licence mention Informatique Conception Orientée Objet 56