mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zanninso
ec30312c08
|
1 year ago | |
---|---|---|
.. | ||
README.md | 1 year ago |
README.md
Decorator
Instructions
Now let's implement the Decorator Design Pattern
classDiagram
class Raclette {
<<interface>>
+getCalories()* String
+getIngredients()* String
}
class BaseRaclette {
+getCalories() String
+getIngredients() String
}
class RacletteDecorator {
<<abstract>>
-Raclette raclette
+RacletteDecorator(Raclette raclette)
+getCalories() String
+getIngredients() String
}
class WithPickles {
-Raclette raclette
+RacletteDecorator(Raclette raclette)
+getCalories() String
+getIngredients() String
}
class WithColdMeats {
-Raclette decoratedRaclette
+RacletteDecorator(Raclette raclette)
+getCalories() String
+getIngredients() String
}
Raclette <|.. BaseRaclette
Raclette <|.. RacletteDecorator
RacletteDecorator <|-- WithPickles
RacletteDecorator <|-- WithColdMeats
Here is the matching class diagram. Create the matching classes in the matching files.
Here is the description :
BaseRaclette
:getCalories
should return 1000getIngredients
should return "Patate, fromage à raclette"
RacletteDecorator
:getCalories
should return thegetCalories
of thedecoratedRaclette
attributegetIngredients
should return thegetIngredients
of thedecoratedRaclette
attribute
WithPickles
:getCalories
should return the addition of 50 and thegetCalories
of thedecoratedRaclette
attributegetIngredients
should return the concatenation ofgetIngredients
of thedecoratedRaclette
attribute and ", cornichons"
WithColdMeats
:getCalories
should return the addition of 350 and thegetCalories
of thedecoratedRaclette
attributegetIngredients
should return the concatenation ofgetIngredients
of thedecoratedRaclette
attribute and ", charcuterie"
For each class, implement the toString
method to return "getIngredients()
pour getCalories()
calories".
Usage
Here is a possible ExerciseRunner.java to test your function :
public class ExerciseRunner {
public static void main(String[] args) {
Raclette r = new BaseRaclette();
System.out.println(r);
r = new WithPickles(r);
System.out.println(r);
r = new WithColdMeats(r);
System.out.println(r);
}
}
and its output :
$ javac *.java -d build
$ java -cp build ExerciseRunner
Patate, fromage à raclette pour 1000 calories
Patate, fromage à raclette, cornichons pour 1050 calories
Patate, fromage à raclette, cornichons, charcuterie pour 1400 calories
$