Département de Mathématiques et Informatique
Elément : Génie logiciel
Génie Informatique 2
Année universitaire : 2022/2023
TP No 4
Objectif : Design Pattern (Abstract Factory)
IV. Implémentation de la classe Abstract Factory via un exemple
public enum ShapeType {
carre, cercle, rectangle
}
public class ShapeFactory extends AbstractFactory{
public Shape createShape(ShapeType type) {
if(type==[Link])
return new Carre();
else if(type==[Link])
return new Rectangle();
else if(type==[Link])
return new Cercle();
else
return null;
@Override
public Color createColor(ColorType type) {
// TODO Auto-generated method stub
return null;
}
}
public abstract class Shape {
GI2 -Génie Logiciel- 1
public abstract void afficherShape();
}
public class Carre extends Shape {
@Override
public void afficherShape() {
// TODO Auto-generated method stub
[Link]("Carre");
}
public class Cercle extends Shape{
@Override
public void afficherShape() {
// TODO Auto-generated method stub
[Link]("Cercle");
public class Rectangle extends Shape{
@Override
public void afficherShape() {
// TODO Auto-generated method stub
[Link]("Rectangle");
}
public enum ColorType {
red, blue,green
GI2 -Génie Logiciel- 2
}
public class ColorFactory extends AbstractFactory{
public Color createColor(ColorType type) {
if(type==[Link])
return new Red();
else if(type==[Link])
return new Blue();
else if(type==[Link])
return new Green();
else return null;
}
@Override
public Shape createShape(ShapeType type) {
// TODO Auto-generated method stub
return null;
}
public abstract class Color {
public abstract void afficherColor();
}
public class Red extends Color{
@Override
public void afficherColor() {
// TODO Auto-generated method stub
[Link]("Rouge");
}
public class Blue extends Color{
@Override
public void afficherColor() {
// TODO Auto-generated method stub
[Link]("Bleu");
}
public class Green extends Color{
@Override
public void afficherColor() {
// TODO Auto-generated method stub
[Link]("Vert");
}
GI2 -Génie Logiciel- 3
public abstract class AbstractFactory {
public abstract Shape createShape(ShapeType type);
public abstract Color createColor(ColorType type);
}
Créer une classe teste et exécuter le code suivant dans la méthode principale :
public static void main(String[] args)
{
Scanner reader = new Scanner([Link]);
AbstractFactory objet;
[Link]("Voulez-vous utiliser des formes (1) ou des couleur (2) :");
String choix = [Link]();
if ([Link]("1"))
objet= new ShapeFactory();
else
objet= new ColorFactory();
Dans la méthode Main essayer d’appeler la méthode afficherColor et afficherShape en accedant à ces deux
méthodes via l’objet objet
import [Link];
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner([Link]);
AbstractFactory objet;
[Link]("Voulez-vous utiliser des formes (1) ou des couleur (2) :");
String choix = [Link]();
if ([Link]("1"))
{
objet= new ShapeFactory();
[Link]([Link]).afficherShape();
GI2 -Génie Logiciel- 4
else
{
objet= new ColorFactory();
[Link]([Link]).afficherColor();
}
GI2 -Génie Logiciel- 5