Code de l'adaptateur

/*
 * Copyright (c) Régis Clouard
 * 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;

import java.util.ArrayList;
import java.util.Collection;

public final class DessinVectoriel {
    public static void main( String argv[] ) {
        Canvas c = new Canvas();
        c.add(new Square());
        c.add(new Point());
        c.add(new Circle());
        c.paint();
    }
}

final class Canvas {
    private final Collection _shapes = new ArrayList<>();

    public Canvas() {
    }

    public void add( Shape f ) {
        _shapes.add(f);
    }

    public void paint() {
        System.out.println("Liste des éléments dans le canvas (version 1).");
        for (Shape f : _shapes) {
            f.paint();
        }
        System.out.println("Liste des éléments dans le canvas (version 2).");
        _shapes.forEach(Shape::paint);
    }
}

abstract class Shape {
    private static int _max = 0;
    private int _index;

    public Shape() {
        _index = ++_max;
    }

    public int getIndex() {
        return _index;
    }

    public abstract void paint();
}

final class Point extends Shape {
    public void paint() {
        System.out.println("Point #" + getIndex());
    }
}

final class Square extends Shape {
    public void paint() {
        System.out.println("Square #" + getIndex());
    }
}

/**
 * Utilisation du patron Adaptateur Cercle pour adapter CercleXX
 * à la hiérarchie des formes.
 */
class CercleXX {
    public void afficherCercle() {
        System.out.println("A circle.");
    }
}

final class Circle extends Shape {
    private CercleXX _cercleXX;

    public Circle() {
        _cercleXX = new CercleXX();
    }

    @Override
    public void paint() {
        _cercleXX.afficherCercle();
    }
}
Modifié le: Wednesday 8 November 2017, 18:11