Commerce électronique (Stratégie, Décorateur, Observateur)

/*
 * 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.td;

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

public final class CommerceElectronique {

    public static void main( String[] args ) {
        System.out.println("Calcul des taxes selon le pays");
        CommandeClient commandeClient = new CommandeClient();
        commandeClient.setCountryTaxes(new TaxeFR());
        commandeClient.setCommande(new Commande(1));
        System.out.println("Prix = " + commandeClient.calculeTaxe());

        commandeClient.setCountryTaxes(new TaxeB());
        commandeClient.setCommande(new Commande(1));
        System.out.println("Prix = " + commandeClient.calculeTaxe());

        System.out.println("Imprimer un recu selon les options");
        Recu c = new Entete(new PiedPage(new BaseRecu()));
        c.imprime();

        System.out.println("Ajout de fonctionnalités liées à la création d'un client");
        Client client = new Client();
        client.addObserver(new LettreBienvenue());
        client.addObserver(new VerifieAdresse());
        client.valideClient();
        client.addObserver(new ImpressionFicheClient());
        client.valideClient();
    }
}

final class Commande {
    private int _index;

    public Commande( int i ) {
        _index = i;
    }
}

final class CommandeClient {
    private Commande _commande;
    private CalculTaxe _calculTaxe;

    public void setCountryTaxes( CalculTaxe calculTaxe ) {
        _calculTaxe = calculTaxe;
    }

    public void setCommande( Commande commande ) {
        _commande = commande;
    }

    public double calculeTaxe() {
        return _calculTaxe.calculeTaxe(_commande);
    }
}

/**
 * 1. Utilisation du patron Stratégie.
 */

interface CalculTaxe {
    public double calculeTaxe( Commande commande );
}

final class TaxeFR implements CalculTaxe {
    public double calculeTaxe( Commande commande ) {
        System.out.println("Calcul des taxes pour le France");
        return 100;
    }
}

final class TaxeB implements CalculTaxe {
    public double calculeTaxe( Commande commande ) {
        System.out.println("Calcul des taxes pour la Belgique");
        return 10;
    }
}

/**
 * 2. Utilisation du patron Décorateur pour construire le reçu
 * avec toutes les options.
 */
abstract class Recu {
    public abstract void imprime();
}

final class BaseRecu extends Recu {
    @Override
    public void imprime() {
        System.out.println("Recu");
    }
}

abstract class Decorator extends Recu {
    private Recu _monComposant;

    public Decorator( Recu composant ) {
        _monComposant = composant;
    }

    @Override
    public void imprime() {
        if (_monComposant != null) {
            _monComposant.imprime();
        }
    }
}

class Entete extends Decorator {

    public Entete( Recu c ) {
        super(c);
    }

    @Override
    public void imprime() {
        // code specifique de l'entete
        System.out.println("Entete");
        super.imprime();
    }
}

class PiedPage extends Decorator {

    public PiedPage( Recu c ) {
        super(c);
    }

    @Override
    public void imprime() {
        // code specifique de l'entete
        System.out.println("Pied de page");
        super.imprime();
    }
}

/**
 * 3. Le patron Observateur pour ajouter à souhait bdes fonctionnalités
 * lors de la création d'un client dans le système.
 */
abstract class Observable {
    private List _listeners = new ArrayList();

    public void addObserver( Observer o ) {
        _listeners.add(o);
    }

    public void notifyObservers() {
        for (Observer l : _listeners) {
            l.update(this);
        }
    }

    public void deleteObserver( Observer o ) {
        _listeners.remove(o);
    }
}

final class Client extends Observable {
    void valideClient() {
        notifyObservers();
    }
}

interface Observer {
    void update( Observable o ); // Version en flux tirée (pull)
}

final class LettreBienvenue implements Observer {
    public void update( Observable o ) {
        System.out.println("Envoie d'une lettre de bienvenue");
    }
}

final class VerifieAdresse implements Observer {
    public void update( Observable o ) {
        System.out.println("Verification de l'adresse");
    }
}

final class ImpressionFicheClient implements Observer {
    public void update( Observable o ) {
        System.out.println("Impression de la fiche client");
    }
}
Modifié le: Monday 27 November 2017, 08:57