Patrons de conception
Les patrons de conception (design patterns) apportent des solutions algorithmiques et d'implémentation aux problèmes courants rencontrés dans le cadre de la conception orientée objet.
Le Singleton (Singleton)
Le pattern Singleton apporte une solution aux cas où une unique instance d'une classe doit exister dans un programme (pour une gestion d'un pool de connexions, une gestion du cache, une instance d'application…)
Diagramme de classes
Implémentation en java
public final class Singleton {
private static Singleton instance;
private Singleton() {
// constructeur
}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
instance = new Singleton();
}
}
return instance;
}
@Override
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}
Utilisation
Il est ensuite possible de faire appel à la méthode getInstance qui retournera toujours l'instance unique de singleton.
Singleton.getInstance();
La Fabrique (Factory)
Objectifs
La fabrique est une classe de création d'objets, de différents types, héritant d'une classe de base, ou implémentant une interface. Elle permet la création d'objets dynamiquement en fonction de paramètres passés.
Caractéristiques
- La fabrique étant souvent unique dans le programme, elle peut utiliser le pattern Singleton
- Les paramètres passés à la fabrique déterminent le type d'instance à créer
Diagramme de classes
Implémentation en java
Les différents types à créer (héritant d'un type générique)
public abstract class Animal{
public abstract void myName();
}
public class Chat extends Animal{
public void myName(){
System.out.println("Je suis un Chat");
}
}
public class Chien extends Animal{
public void myName(){
System.out.println("Je suis un Chien");
}
}
La fabrique :
public class FabriqueAnimal{
private static FabriqueAnimal instance = new FabriqueAnimal();
private FabriqueAnimal(){}
public static FabriqueAnimal getFabriqueAnimalInstance(){
return instance ;
}
public Animal getAnimal(String typeAnimal) throws ExceptionCreation{
Animal result=null;
switch(typeAnimal){
case "chat":
result= new Chat();
break;
case "chien":
result=new Chien();
break;
default:
throw new ExceptionCreation("Impossible de créer un " + typeAnimal);
break;
}
return result;
}
}
Utilisation :
public class DemoFabrique{
public static void main(String [] args){
FabriqueAnimal fabrique = FabriqueAnimal.getFabriqueAnimalInstance();
Animal animal = FabriqueAnimal.getAnimal("chat");
animal.myName();
}
}
La Fabrique abstraite (Abstract Factory)
Objectifs
La fabrique est un créateur d'objets, de différents types, héritant d'une classe de base, ou implémentant une interface. Elle permet d'isoler la création des objets de leur utilisation.
On peut ainsi ajouter de nouveaux objets dérivés sans modifier le code qui utilise l'objet de base.
Implémentation en java
Les différents types d'objet à créer et leur classe de base :
public abstract class Button{
private String caption;
public abstract void paint();
public String getCaption(){
return caption;
}
public void setCaption(String caption){
this.caption = caption;
}
}
class WinButton extends Button{
public void paint(){
System.out.println("I'm a WinButton: "+ getCaption());
}
}
class OSXButton extends Button{
public void paint(){
System.out.println("I'm a OSXButton: "+ getCaption());
}
}
Les factories concrètes et leur classe abstraite :
/*
* GUIFactory example
*/
public abstract class GUIFactory{
public static GUIFactory getFactory(){
int sys = readFromConfigFile("OS_TYPE");
if (sys == 0)
return(new WinFactory());
else
return(new OSXFactory());
}
public abstract Button createButton();
}
class WinFactory extends GUIFactory{
public Button createButton(){
return(new WinButton());
}
}
class OSXFactory extends GUIFactory{
public Button createButton(){
return(new OSXButton());
}
}
Utilisation :
public class DemoAbstractFactory{
public static void main(String[] args){
GUIFactory aFactory = GUIFactory.getFactory();
Button aButton = aFactory.createButton();
aButton.setCaption("Jouer");
aButton.paint();
}
}
Limites
- Introduit une certaine complexité dans le développement, parfois à éviter
Le poids mouche (Flyweight)
Le poids mouche permet de factoriser le nombre d'instances à créer d'une classe, en permettant la réutilisation d'instance existantes.
Diagramme de classes
Implémentation en java
Source : http://www.journaldev.com/1562/flyweight-pattern-in-java-example-tutorial
Création de formes (Shape), de types concrets (Line, oval…)
Models
package models;
import java.awt.Color;
import java.awt.Graphics;
public interface Shape {
public void draw(Graphics g, int x, int y, int width, int height, Color color);
}
package models;
import java.awt.Color;
import java.awt.Graphics;
public class Line implements Shape {
@Override
public void draw(Graphics g, int x, int y, int width, int height, Color color) {
g.setColor(color);
g.drawLine(x, y, width, height);
}
}
package models;
import java.awt.Color;
import java.awt.Graphics;
public class Oval implements Shape {
private boolean fill;
public Oval(boolean fill) {
this.fill = fill;
}
@Override
public void draw(Graphics g, int x, int y, int width, int height, Color color) {
g.setColor(color);
g.drawOval(x, y, width, height);
if (fill) {
g.fillOval(x, y, width, height);
}
}
}
Factory
package models;
import java.util.HashMap;
public class ShapeFactory {
private static final HashMap<ShapeType, Shape> shapes = new HashMap<ShapeType, Shape>();
public static Shape getShape(ShapeType type) {
Shape shapeImpl = shapes.get(type);
if (shapeImpl == null) {
switch (type) {
case OVAL_FILL:
shapeImpl = new Oval(true);
break;
case OVAL:
shapeImpl = new Oval(false);
break;
case LINE:
shapeImpl = new Line();
break;
}
shapes.put(type, shapeImpl);
}
return shapeImpl;
}
public static enum ShapeType {
OVAL_FILL, OVAL, LINE;
}
}
Utilisation
public class DemoFlyweight extends JFrame {
private static final long serialVersionUID = -1350200437285282550L;
private final int WIDTH;
private final int HEIGHT;
private static final ShapeType shapes[] = { ShapeType.LINE, ShapeType.OVAL_FILL, ShapeType.OVAL };
private static final Color colors[] = { Color.RED, Color.GREEN, Color.YELLOW };
public DemoFlyweight(int width, int height) {
this.WIDTH = width;
this.HEIGHT = height;
Container contentPane = getContentPane();
JButton startButton = new JButton("Draw");
final JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(startButton, BorderLayout.SOUTH);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Graphics g = panel.getGraphics();
for (int i = 0; i < 40; ++i) {
Shape shape = ShapeFactory.getShape(getRandomShape());
shape.draw(g, getRandomX(), getRandomY(), getRandomWidth(), getRandomHeight(), getRandomColor());
}
}
});
}
private ShapeType getRandomShape() {
return shapes[(int)
- sio/bloc2/poo/designpattern.txt
- Dernière modification : il y a 23 mois
- de
jcheron


