mapping uml - java - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/mapping_uml_java_fr.pdf ·...

24
Analyse et conception Cours de 1 e année ingénieur [email protected] http://www.fromeo.fr

Upload: trinhthu

Post on 02-Apr-2018

238 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

Analyse et conception

Cours de 1e année ingénieur

[email protected]

http://www.fromeo.fr

Page 2: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

Mapping UML - JAVA

Page 3: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 3

Plan

• Introduction– Mapping UML-Java

– Génération de code vs. reverse engineering (outils)

• UML et Java : concepts OO– Classe, classe abstraite, interface, héritage, …

• Attributs et getter&setter– Mapping UML-Java : pas forcément uniforme dans les 2 sens

– Setter -> Contraintes OCL

• Associations– Pas de concept d’association en Java

– 1-1 ; 1-* ; qualifier ; classe d’association

• Composition et agrégation• Diagramme de séquences• Machine à états

Page 4: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 4

Introduction• Mapping UML – Java

– à la main (1e année) : passage de la conception à l’implémentation

– utilisation d’outils (2e année MDA/IDM)

UML

Java

Générationde code

Rétro-ingénierie

• Génération de code

– Plusieurs choix de mapping possibles (plusieurs interprétations)

• Rétro-ingénierie

– Le modèle retrouvé peut ne pas être identique au modèle de départ

UML

Java

ImplémentationTest

Page 5: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 5

UML et Java

• UML = conception orientée objet

• Java = programmation orientée objet

• UML n’est pas lié à un langage de programmation en particulier et n’impose même pas d’utiliser un langage orienté objet

• Parce qu’ils sont orientés objets, UML et Java ont des concepts en commun qui facilitent le mapping– Classe, classe abstraite, interface, héritage, …

• Mais tous les concepts d’UML ne se retrouve pas forcément dans Java

Page 6: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 6

UML et Java (classes)

A

AbstractA

public class A {

}

public abstract class AbstractA {

}

A

B

public class B extends A {

}

Page 7: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 7

UML et Java (interfaces)

IA<<interface>> public interface IA {

}

A

IA<<interface>>

A

IA

public class A implements IA {

}

IA<<interface>>

IB<<interface>>

ou

public interface IB extends IA {

}

Page 8: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 8

Attributs et getter&setter

Person

+firstname+lastname+age

Person

-firstname-lastname-age

+getFirstname()+setFirstname()+getLastname()+setLastname()+getAge()+setAge()

public class Person {

public String firstname;

public String lastname;

public int age;

}

public class Person {

private String firstname;

private String lastname;

private int age;

public String getFirstname() {

return this.firstname;

}

public void setFirstname(String firstname) {

this.firstname = firstname;

}

// ...

}

Page 9: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 9

Attributs et getter&setter et OCL

Person

+firstname+lastname+age

public class Person {

private String firstname;

private String lastname;

private int age;

public int getAge() {

return this.age;

}

public void setAge(int age) {

if(age >= 0) {

this.age = age;

} else {

throw new InvalidAgeException();

}

}

// ...

}

{

context Person

inv: age >= 0

}

Page 10: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 10

Association

• Le concept d’association d’UML n’existe pas en Java

public class A association B { //???

}

BA

1

Page 11: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 11

UML sans association

• Un modèle UML utilisant des associations

peut se traduire en un modèle sans association

• Le mapping vers Java peut alors s’effectuer

BA

1

A

-b: B

B

public class A {

private B b;

}

public class B {

}

Page 12: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 12

Association avec rôle

• Même convention qu’en OCL,– quand il n’y a pas de rôle : nom de la classe

avec première lettre en minuscule

– quand il y a un rôle : nom du rôle

B

public class A {

private B role;

}

public class B {

}

BA +role

1

A

-role: B

Page 13: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 13

Associations et cardinalités

BA

1

BA

*

BA

*{ordered}

public class A {

private B b;

}

public class A {

private Set<B> b;

}

public class A {

private List<B> b;

}

self.b : B

self.b : Set(B)

self.b : OrderedSet(B)

OCL

OCL

OCL

Page 14: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 14

Associations [1]

BA

1

public class A {

private B b;

public A() {

b = new B();

}

public B getB() {

return b;

}

}

self.b : BOCL

public class A {

private B b;

public A(B b) {

this.b = b;

}

public B getB() {

return b;

}

}

public class A {

private B b;

public A() {}

public setB(B b) {

this.b = b;

}

public B getB() {

return b;

}

}

Page 15: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 15

Associations [*]public class A {

private Set<B> b;

public A() {

b = new HashSet<B>();

// b = new HashSet<B>();

// b = new CopyOnWriteArraySet<B>();

// b = new … implements Set<>

}

public boolean add(B b) {

return this.b.add(b);

}

public boolean addAll(B... b) {

return this.b.addAll(Arrays.asList(b));

}

public boolean addAll(Collection<B> b) {

return this.b.addAll(b);

}

}

BA

*

self.b : Set(B)OCL

Page 16: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 16

Associations [*]public class A {

private Set<B> b;

public A(Set<B> b) {

this.b = b;

}

public A(B... b) {

this.b = new HashSet<B>(Arrays.asList(b));

}

}

BA

*

self.b : Set(B)OCL

Page 17: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 17

Associations [*]public class A {

private Set<B> b;

public A() {

b = new HashSet<B>();

// b = new HashSet<B>();

// b = new CopyOnWriteArraySet<B>();

// b = new … implements Set<>

}

public Set<B> getB() {

return this.b;

}

}

BA

*

self.b : Set(B)OCL

main() {

A a = new A();

a.getB().add(new B());

a.getB().addAll(...);

}

Page 18: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 18

Associations [*] {ordered}public class A {

private List<B> b;

public A() {

b = new ArrayList<B>();

// b = new LinkedList<B>();

// b = new CopyOnWriteArrayList<B>();

// b = new … implements List<>

}

public List<B> getB() {

return this.b;

}

}

BA

*{ordered}

self.b : OrderedSet(B)OCL

main() {

A a = new A();

a.getB().add(new B());

a.getB().addAll(...);

}

Page 19: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 19

Associations qualifiées [0..1]

BA

0..1+id : Type+id : Type

A

B

Tuple

K

*

+key

1

+value

1

public class A {

private Map<K,B> b;

}

Page 20: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 20

Associations qualifiées [*]

public class A {

private Map<K,Set<B>> b;

}

BA

*+id : Type+id : Type

A

B

Tuple

K

*

+key

1

+value

*

Page 21: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 21

Association, agrégation,

composition

• ToDo en TD

Page 22: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 22

Classes d’association

• ToDo en TD

Page 23: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 23

Mapping Sequence Diagram - Java

[Fowler2003]

public class Order {

public void dispatch() {

for(LineItem li : lineItems) {

if(product.value > 10000) {

careful.dispatch();

} else {

regular.dispatch();

}

}

if(needsConfirmation) {

messenger.confirm();

}

}

List<LineItem> lineItems;

Distributor careful;

Distributor regular;

Messenger messenger;

boolean needsConfirmation;

}

Page 24: Mapping UML - JAVA - fromeo.free.frfromeo.free.fr/pub/ens/2008/uml/Mapping_UML_JAVA_FR.pdf · fabien.romeo@fromeo.fr 3 Plan • Introduction – Mapping UML-Java – Génération

[email protected] 24

public class MessageParser {

public boolean put(char c) {switch (state) {case Waiting:if (c == '<') {state = GettingToken;token = new StringBuffer();body = new StringBuffer();}break;

case GettingToken :if (c == '>') state = GettingBody;else token.append(c);break;

case GettingBody :if (c == ';') state = Waiting;else body.append(c);return true;

}return false;}public StringBuffer getToken() { return token; }public StringBuffer getBody() { return body; }

private final static int Waiting = 0;private final static int GettingToken = 1;private final static int GettingBody = 2;private int state = Waiting;private StringBuffer token, body;

}

Mapping

State Machines - Java

[BRJ2005]