Code Java du logiciel de dessin vectoriel (Pont, Singleton et Proxy)

/*
* Copyright (c) 2019
* ENSICAEN, École publique d'ingénieurs et centres de recherche, Caen, FRANCE.
* https://www.ensicaen.fr
*
* This file is licenced under the MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fr.ensicaen.gl2a.td.pont;

import java.util.ArrayList;
import java.util.List;

public final class DessinVectoriel {
public static void main( String[] args ) {
Canvas canvas = new Canvas();
Rectangle2 rectangle2 = new Rectangle2(new DessinX11Proxy(), 10, 10, 50, 50);
canvas.add(rectangle2);
Cercle2 cercle2 = new Cercle2(new DessinX11Proxy(), 100, 100, 10);
canvas.add(cercle2);
canvas.afficher();
canvas.remove(rectangle2);
canvas.remove(cercle2);
}
}

class Canvas {
private List<Forme> _formes = new ArrayList<>();

public void add( Forme forme ) {
_formes.add(forme);
}

public void afficher() {
for (Forme f : _formes) {
f.dessiner();
}
}

public void remove( Forme forme ) {
_formes.remove(forme);
forme.delete();
}
}

abstract class Forme {
private Dessin _pont;

public Forme( Dessin dessin ) {
_pont = dessin;
}

public abstract void dessiner();

public void delete() {
_pont.deleteInstance();
}

protected Dessin getPont() {
return _pont;
}
}

class Rectangle2 extends Forme {
private int _x1;
private int _y1;
private int _x2;
private int _y2;

public Rectangle2( Dessin dessin, int x1, int y1, int x2, int y2 ) {
super(dessin);
_x1 = x1;
_y1 = y1;
_x2 = x2;
_y2 = y2;
}

@Override
public void dessiner() {
getPont().dessinerLigne(_x1, _y1, _x1, _y2);
getPont().dessinerLigne(_x1, _y2, _x2, _y2);
getPont().dessinerLigne(_x2, _y2, _x2, _y1);
getPont().dessinerLigne(_x2, _y1, _x1, _y1);
}
}

class Cercle2 extends Forme {
private int _x;
private int _y;
private int _r;

public Cercle2( Dessin dessin, int x, int y, int r ) {
super(dessin);
_x = x;
_y = y;
_r = r;
}

@Override
public void dessiner() {
getPont().dessinerCercle(_x, _y, _r);
}
}

interface Dessin {
void dessinerCercle( int x, int y, int r );
void dessinerLigne( int x1, int y1, int x2, int y2 );
void deleteInstance();
}

class DessinX11Proxy implements Dessin {
private DessinX11Reel _dessinX11Reel;

public DessinX11Proxy() {
_dessinX11Reel = DessinX11Reel.getInstance();
}

public void deleteInstance() {
_dessinX11Reel.deleteInstance();
}

@Override
public void dessinerCercle( int x, int y, int r ) {
_dessinX11Reel.dessinerCercle(x, y, r);
}

@Override
public void dessinerLigne( int x1, int y1, int x2, int y2 ) {
_dessinX11Reel.dessinerLigne(x1, y1, x2, y2);
}
}

/*
 * This class is an adapter and a Singleton.
*/
class DessinX11Reel implements Dessin {
private static int _compteur_pointeur = 0;
private static DessinX11Reel _instance;
private X11 _x11;

private DessinX11Reel() {
_x11 = new X11();
}

public static synchronized DessinX11Reel getInstance() {
_compteur_pointeur++;
if (_instance == null) {
_instance = new DessinX11Reel();
}
return _instance;
}

@Override
public void dessinerCercle( int x, int y, int r ) {
_x11.draw_circle(x, y, r);
}

@Override
public void dessinerLigne( int x1, int y1, int x2, int y2 ) {
_x11.draw_line(x1, y1, x2, y2);
}

public void deleteInstance() {
if (--_compteur_pointeur == 0) {
System.out.println("Il n'y a plus d'instance de X11");
_x11=null;
}
}
}

class DessinMFCProxy implements Dessin {
private Dessin _dessin;

public DessinMFCProxy() {
_dessin = DessinMFCReel.getInstance();
}

public void deleteInstance() {
_dessin.deleteInstance();
}

@Override
public void dessinerCercle( int x, int y, int r ) {
_dessin.dessinerCercle(x, y, r);
}

@Override
public void dessinerLigne( int x1, int y1, int x2, int y2 ) {
_dessin.dessinerLigne(x1, y1, x2, y2);
}
}

/*
* This class is an adapter and a Singleton.
*/
class DessinMFCReel implements Dessin {
private static int _compteur_instance = 0;
private static DessinMFCReel _instance;
private MFC __mfc;

private DessinMFCReel() {
__mfc = new MFC();
}

public static synchronized DessinMFCReel getInstance() {
if (_instance == null) {
_instance = new DessinMFCReel();
}
_compteur_instance++;
return _instance;
}

@Override
public void dessinerCercle( int x, int y, int r ) {
__mfc.drawCircle(x, y, r);
}

@Override
public void dessinerLigne( int x1, int y1, int x2, int y2 ) {
__mfc.drawLine(x1, y1, x2, y2);
}

@Override
public void deleteInstance() {
if (--_compteur_instance == 0) {
System.out.println("Il n'y a plus d'instance de X11");
__mfc = null;
}
}
}

// Classe supposée non modifiable (issue d'une API)
class X11 {
public void draw_line( int x1, int y1, int x2, int y2 ) {
// Code pour dessiner une ligne avec X11
}

public void draw_circle( int x, int y, int r ) {
// Code pour dessiner un cercle avec X11
}
}

// Classe supposée non modifiable (issue d'une API)
class MFC {
public void drawLine( int x1, int y1, int x2, int y2 ) {
// Code pour dessiner une ligne avec MFC
}

public void drawCircle( int x, int y, int r ) {
// Code pour dessiner un cercle avec MFC
}
}
 

Modifié le: Tuesday 19 November 2019, 09:03