Bonsoir,

J'ai ceci 2 classes A et B héritant d'une 3ème classe R :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package test.mapping.model;
 
public class A extends R {
	private String ma1;
	private String ma2;
	private String ma3;
 
	public String getMa1() {
		return ma1;
	}
 
	public void setMa1(String ma1) {
		this.ma1 = ma1;
	}
 
	public String getMa2() {
		return ma2;
	}
 
	public void setMa2(String ma2) {
		this.ma2 = ma2;
	}
 
	public String getMa3() {
		return ma3;
	}
 
	public void setMa3(String ma3) {
		this.ma3 = ma3;
	}
 
	@Override
	public String toString() {
		return "A [ma1=" + ma1 + ", ma2=" + ma2 + ", ma3=" + ma3 + ", " + super.toString() + "]";
	}
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package test.mapping.model;
 
public class B extends R {
	private String mb1;
	private String mb2;
	private String mb3;
 
	public String getMb1() {
		return mb1;
	}
 
	public void setMb1(String mb1) {
		this.mb1 = mb1;
	}
 
	public String getMb2() {
		return mb2;
	}
 
	public void setMb2(String mb2) {
		this.mb2 = mb2;
	}
 
	public String getMb3() {
		return mb3;
	}
 
	public void setMb3(String mb3) {
		this.mb3 = mb3;
	}
 
	@Override
	public String toString() {
		return "B [mb1=" + mb1 + ", mb2=" + mb2 + ", mb3=" + mb3 + ", " + super.toString() + "]";
	}
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package test.mapping.model;
 
public class R {
	private String mr1;
	private String mr2;
	private String mr3;
 
	public String getMr1() {
		return mr1;
	}
 
	public void setMr1(String mr1) {
		this.mr1 = mr1;
	}
 
	public String getMr2() {
		return mr2;
	}
 
	public void setMr2(String mr2) {
		this.mr2 = mr2;
	}
 
	public String getMr3() {
		return mr3;
	}
 
	public void setMr3(String mr3) {
		this.mr3 = mr3;
	}
 
	@Override
	public String toString() {
		return "R [mr1=" + mr1 + ", mr2=" + mr2 + ", mr3=" + mr3 + "]";
	}
}
Le programme instancie la classe R et la valorise.

Puis le programme instancie la classe A et la valorise avec des données propres à A et aussi avec des données issues de R.

Il fait de même avec la classe B.

Pour réaliser ceci, il utilise les classes utilitaires AMapper et BMapper.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
package test.mapping.mapper;
 
import test.mapping.model.A;
import test.mapping.model.R;
 
public class AMapper {
	public A mapRow(A instA, R instR) {
		instA.setMr1(instR.getMr1());
		instA.setMr2(instR.getMr2());
		instA.setMr3(instR.getMr3());
		return instA;
	}
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
package test.mapping.mapper;
 
import test.mapping.model.B;
import test.mapping.model.R;
 
public class BMapper {
	public B mapRow(B instB, R instR) {
		instB.setMr1(instR.getMr1());
		instB.setMr2(instR.getMr2());
		instB.setMr3(instR.getMr3());
		return instB;
	}
}
Le programme :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package test.mapping;
 
import test.mapping.mapper.AMapper;
import test.mapping.mapper.BMapper;
import test.mapping.model.A;
import test.mapping.model.B;
import test.mapping.model.R;
 
public class App {
 
	public static void main(String[] args) {
		R instR = createR();
		System.out.println(instR.toString());
 
		A instA = ceateA(instR);
		System.out.println(instA.toString());
 
		B instB = createB(instR);
		System.out.println(instB.toString());
	}
 
	private static R createR() {
		R instR = new R();
		instR.setMr1("rrr1");
		instR.setMr2("rrr2");
		instR.setMr3("rrr3");
		return instR;
	}
 
	private static A ceateA(R instR) {
		A instA = new A();
		instA.setMa1("aaa1");
		instA.setMa2("aaa2");
		instA.setMa3("aaa3");
 
		AMapper aMapper = new AMapper();
		instA = aMapper.mapRow(instA, instR);
		return instA;
	}
 
	private static B createB(R instR) {
		B instB = new B();
		instB.setMb1("bbb1");
		instB.setMb2("bbb2");
		instB.setMb3("bbb3");
 
		BMapper bMapper = new BMapper();
		instB = bMapper.mapRow(instB, instR);
		return instB;
	}
}
Le résultat :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
R [mr1=rrr1, mr2=rrr2, mr3=rrr3]
A [ma1=aaa1, ma2=aaa2, ma3=aaa3, R [mr1=rrr1, mr2=rrr2, mr3=rrr3]]
B [mb1=bbb1, mb2=bbb2, mb3=bbb3, R [mr1=rrr1, mr2=rrr2, mr3=rrr3]]
Ma question :

Comment ne faire qu'une seule classe Mapper, générique qui ne fait au fond que valoriser les mêmes attributs de la classe R dans la classe A et B ?

Merci pour votre aide.