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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| abstract class ComposantGraphique {
public LinkedList<Point> pointDIntersection(ComposantGraphique cg) {
return ManagerIntersections.pointDIntersection(this, cg);
}
}
class Cercle extends ComposantGraphique {
public Point getCentre() {
return null; //*osef
}
public int getRayon() {
return 0; // osef
}
}
class Ligne extends ComposantGraphique {
public Point getPoint1() {
return null; //*osef
}
public Point getPoint2() {
return null; // osef
}
}
interface CalculateurIntersection<A extends ComposantGraphique, B extends ComposantGraphique> {
LinkedList<Point> pointDIntersection(A a, B b);
}
class IntersectionCercles implements CalculateurIntersection<Cercle, Cercle> {
@Override
public LinkedList<Point> pointDIntersection(Cercle a, Cercle b) {
System.out.println("Calcul savant à partir des centres et rayons des cercles");
return null;
}
}
class IntersectionLignes implements CalculateurIntersection<Ligne, Ligne> {
@Override
public LinkedList<Point> pointDIntersection(Ligne a, Ligne b) {
System.out.println("Calcul savant à partir des bouts des lignes");
return null;
}
}
class IntersectionCercleLigne implements CalculateurIntersection<Cercle, Ligne> {
@Override
public LinkedList<Point> pointDIntersection(Cercle cercle, Ligne ligne) {
System.out.println("Calcul savant à partir du centre et du rayon du cercle, et des bouts de la ligne");
return null;
}
}
class ManagerIntersections {
private static final Map<Set<Class<?>>, CalculateurIntersection<?, ?>> map = new HashMap<>();
private static <A extends ComposantGraphique, B extends ComposantGraphique>
void enregistrerCalculateur(CalculateurIntersection<A, B> calculateur, Class<A> aClass, Class<B> bClass) {
Set<Class<?>> set = new HashSet<>();
set.add(aClass);
set.add(bClass);
map.put(set, calculateur);
}
static {
enregistrerCalculateur(new IntersectionCercles(), Cercle.class, Cercle.class);
enregistrerCalculateur(new IntersectionLignes(), Ligne.class, Ligne.class);
enregistrerCalculateur(new IntersectionCercleLigne(), Cercle.class, Ligne.class);
}
public static LinkedList<Point>
pointDIntersection(ComposantGraphique a, ComposantGraphique b) {
// récupérer le calculateur adéquat
Set<Class<?>> set = new HashSet<>();
Class<?> aClass = a.getClass();
Class<?> bClass = b.getClass();
set.add(aClass);
set.add(bClass);
@SuppressWarnings("unchecked")
CalculateurIntersection<ComposantGraphique, ComposantGraphique> calculateur =
(CalculateurIntersection<ComposantGraphique, ComposantGraphique>)map.get(set);
// Gérer le cas où il n'a pas été enregistré
if(calculateur == null) {
throw new IllegalArgumentException("Combinaison de composants inconnue");
}
// Déterminer dans quel ordre appeler les paramètres
ParameterizedType calculateurType = (ParameterizedType)calculateur.getClass().getGenericInterfaces()[0];
Type firstType = calculateurType.getActualTypeArguments()[0];
if(firstType == aClass) {
// appeler avec le paramètre a en premier
return calculateur.pointDIntersection(a, b);
} else {
// inverser les paramètres
return calculateur.pointDIntersection(b, a);
}
}
}
public class Test {
public static void main(String[] args) {
Cercle cercle = new Cercle();
Ligne ligne = new Ligne();
cercle.pointDIntersection(cercle);
ligne.pointDIntersection(ligne);
cercle.pointDIntersection(ligne);
ligne.pointDIntersection(cercle);
}
} |
Partager