You are on page 1of 1

interface Cafe {

String getDescription();
double getCout();
}
class CafeSimple implements Cafe {
public String getDescription() {
return "Café simple";
}
public double getCout() {
return 1.0;
}
public static void main (String []arg) {
Cafe cafeaulait = new Lait(new CafeSimple());
System.out.println(cafeaulait.getDescription());
System.out.println("Coût total : " + cafeaulait.getCout());
Cafe cafesucre = new Sucre(new CafeSimple());
System.out.println(cafesucre.getDescription());
System.out.println("Coût total : " + cafesucre.getCout());
Cafe cafeaulaitsucre = new Lait(new Sucre(new CafeSimple()));
System.out.println(cafeaulaitsucre.getDescription());
System.out.println("Coût total : " + cafeaulaitsucre.getCout());
}
}
public abstract class DecorateurCafe implements Cafe {
protected Cafe cafe;
public DecorateurCafe(Cafe cafe) {
this.cafe = cafe;
}
public String getDescription() {
return cafe.getDescription();
}
public double getCout() {
return cafe.getCout();
}
}
class Lait extends DecorateurCafe {
public Lait(Cafe cafe) {
super(cafe);
}
public String getDescription() {
return super.getDescription() + " + Lait";
}
public double getCout() {
return super.getCout() + 0.5;
}
}
class Sucre extends DecorateurCafe {
public Sucre(Cafe cafe) {
super(cafe);
}
public String getDescription() {
return super.getDescription() + " + Sucre";
}
public double getCout() {
return super.getCout() + 0.2;
}
}

You might also like