34
ITI 1521. Introduction à l’informatique II Interface utilisateur graphique : Modèle-Vue-Contrôleur by Marcel Turcotte Version du 21 février 2020

ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

ITI 1521. Introduction à l’informatique IIInterface utilisateur graphique : Modèle-Vue-Contrôleur

by

Marcel Turcotte

Version du 21 février 2020

Page 2: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Préambule

Page 3: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Préambule

Aperçu

Page 4: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Aperçu

Interface utilisateur graphique : Modèle-Vue-Contrôleur

En développement logiciel, un patron de conception est un arrangement spécifique declasses. C’est une façon standard de résoudre des problèmes bien connus. Lesprogrammeurs de l’industrie sont familiers avec ces patrons. Cette semaine, nousdécouvrons le patron de conception Modèle-Vue-Contrôleur (MVC) qui sert dans laréalisation d’interfaces utilisateur graphiques.

Objectif général :Cette semaine, vous serez en mesure de concevoir l’interface utilisateur graphiqued’une application simple en appliquant le patron de conception Modèle-Vue-Contrôleur.

1 26

Page 5: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Préambule

Objectifs d’apprentissage

Page 6: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Objectifs d’apprentissage

Décrire en vos propres mots le patron de conception Modèle-Vue-Contrôleur.Utiliser le patron de conception Modèle-Vue-Contrôleur afin de produire le renduvisuel d’une interface utilisateur graphique.

Lectures :https://fr.wikipedia.org/wiki/Modèle-vue-contrôleur

http://heim.ifi.uio.no/~trygver/2007/MVC_Originals.pdf

2 26

Page 7: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Préambule

Plan du module

Page 8: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Plan

1 Préambule

2 Théorie

3 Exemple

4 Prologue

3 26

Page 9: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Théorie

Page 10: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Patron de conception («design pattern»)

En développement logiciel, un patron de conception est un arrangement spécifiquede classes.C’est une façon standard de résoudre des problèmes bien connus.Les programmeurs de l’industrie sont familiers avec ces patrons.

4 26

Page 11: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Modèle-Vue-Contrôleur(«Model-View-Controller»)

ViewController

Model

1

3

2 4

MVC permet de séparer les données, la vue et la logique de l’application.

5 26

Page 12: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Avantages

Permet de modifier ou adapter chacune des parties de l’application de façonindépendante ;Favorise l’implémentation de plusieurs vues ;L’association entre le modèle et la vue se fait dynamiquement au moment del’exécution (et non au moment de la compilation), ce qui permet de changer la vue aumoment de l’exécution.

6 26

Page 13: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Définitions

Modèle – l’implémentation, état : attributs et comportements ;Vue (View) – l’interface en sortie, une représentation du modèle pour le mondeextérieur ;Contrôleur (Controller) – l’interface en entrée, achemine les requêtes de l’usager afinde mettre-à-jour le modèle.

7 26

Page 14: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Modèle

Contient les données de l’application et les méthodes pour transformer les données ;Possède une connaissance minimale de l’interface graphique (parfois aucuneconnaissance) ;La vue et le modèle sont très différents.

8 26

Page 15: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Vue

Une représentation (graphique, textuelle, vocale, etc.) du modèle pour l’utilisateur.

9 26

Page 16: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Contrôleur

Un contrôleur est un objet permettant à l’utilisateur de transformer les données oula représentation ;Connaît très bien le modèle.

10 26

Page 17: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Exemple

Page 18: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Counter

ViewController

Model

update()

getValue()

actionPerformed()

increment()reset()

Une interface graphique pour la classe Counter.

11 26

Page 19: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Counter : Modèle

pub l i c c l a s s Counter {p r i v a t e i n t v a l u e ;pub l i c Counter ( ) {

v a l u e = 0 ;}pub l i c vo id i n c r ement ( ) {

v a l u e++;}pub l i c i n t ge tVa lue ( ) {

re tu rn v a l u e ;}pub l i c vo id r e s e t ( ) {

v a l u e = 0 ;}pub l i c S t r i n g t o S t r i n g ( ) {

re tu rn " Counter : { v a l u e="+v a l u e+"}" ;}

}

12 26

Page 20: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

View

pub l i c i n t e r f a c e View {vo id update ( ) ;

}

Pour faciliter le développement de plusieurs vues, nous créons l’interface View.Notre exemple aura deux vues : GraphicalView et TextView.

13 26

Page 21: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

pub l i c c l a s s TextView implements View {

p r i v a t e Counter model ;

pub l i c TextView ( Counter model ) {t h i s . model = model ;

}

pub l i c vo id update ( ) {System . out . p r i n t l n ( model . t o S t r i n g ( ) ) ;

}

}

Page 22: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

pub l i c c l a s s Graph i ca lV i ew extends JFrame implements View {p r i v a t e JLabe l i n p u t ;p r i v a t e Counter model ;pub l i c Graph i ca lV i ew ( Counter model , C o n t r o l l e r c o n t r o l l e r ) {

s e tLayou t (new Gr idLayout ( 1 , 3 ) ) ;t h i s . model = model ;JButton button ;but ton = new JButton ( " Inc r ement " ) ;but ton . a d d A c t i o n L i s t e n e r ( c o n t r o l l e r ) ;add ( but ton ) ;JButton r e s e t ;r e s e t = new JButton ( " Reset " ) ;r e s e t . a d d A c t i o n L i s t e n e r ( c o n t r o l l e r ) ;add ( r e s e t ) ;i n p u t = new JLabe l ( ) ;add ( i n p u t ) ;

}pub l i c vo id update ( ) {

i n p u t . s e tTex t ( I n t e g e r . t o S t r i n g ( model . ge tVa lue ( ) ) ) ;}

}

Page 23: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

pub l i c c l a s s C o n t r o l l e r implements A c t i o n L i s t e n e r {

p r i v a t e Counter model ;

p r i v a t e View [ ] v i ews ;p r i v a t e i n t numberOfViews ;

pub l i c C o n t r o l l e r ( ) {

v i ews = new View [ 2 ] ;numberOfViews = 0 ;

model = new Counter ( ) ;

r e g i s t e r (new Graph i ca lV i ew ( model , t h i s ) ) ;r e g i s t e r (new TextView ( model ) ) ;

update ( ) ;

}

Page 24: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

p r i v a t e vo id r e g i s t e r ( View v iew ) {v i ews [ numberOfViews ] = v iew ;numberOfViews++;

}

p r i v a t e vo id update ( ) {f o r ( i n t i =0; i <numberOfViews ; i ++) {

v i ews [ i ] . update ( ) ;}

}

Page 25: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

pub l i c vo id ac t i onPe r f o rmed ( Act ionEvent e ) {

i f ( e . getActionCommand ( ) . e q u a l s ( " Inc r ement " ) ) {model . i n c r ement ( ) ;

} e l s e {model . r e s e t ( ) ;

}

update ( ) ;}

}

Page 26: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Application Counter

ViewController

Model

update()

getValue()

actionPerformed()

increment()reset()

19 26

Page 27: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

pub l i c c l a s s App {

pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

C o n t r o l l e r c o n t r o l l e r ;c o n t r o l l e r = new C o n t r o l l e r ( ) ;

}

}

Page 28: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Counter

L’application Counter est ses deux vues : textuelle et graphique.

21 26

Page 29: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Prologue

Page 30: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Résumé

MVC permet de séparer clairement les données, les vues, et la logique d’uneapplication.

22 26

Page 31: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Excercises

Implémentez chacune des applications suivantes à l’aide du patron de conceptionModèle-Vue-Contrôleur (MVC).

Un jeu de tic-tac-toeUn jeu de bataille navaleUn jeu de mémoireJeu 2048

23 26

Page 32: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Prochain module

-

24 26

Page 33: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

References I

E. B. Koffman and Wolfgang P. A. T.Data Structures : Abstraction and Design Using Java.John Wiley & Sons, 3e edition, 2016.

25 26

Page 34: ITI 1521. Introduction à l'informatique II - subtitleturcotte/teaching/iti-1521/lectures/24/handout… · ITI1521.Introductionàl’informatiqueII Interface utilisateur graphique

Marcel [email protected]

École de science informatique et de génie électrique (SIGE)Université d’Ottawa

26 / 26