Java Reference
In-Depth Information
Figure 2.3. Parameterizing the behavior of filterApples and passing
different filter strategies
To make sure you feel comfortable with the idea of behavior parameterization, have a go at Quiz
2.1 !
Quiz 2.1: Write a flexible prettyPrintApple method
Write a prettyPrintApple method that takes a List of Apples and that can be parameterized with
multiple ways to generate a String output from an apple (a bit like multiple customized toString
methods). For example, you could tell your pretty-PrintApple method to print only the weight of
each apple. In addition, you could tell your prettyPrintApple method to print each apple
individually and mention whether it's heavy or light. The solution is similar to the filtering
examples we've explored so far. To help you get started, we provide a rough skeleton of the
prettyPrintApple method:
public static void prettyPrintApple(List<Apple> inventory, ???){
for(Apple apple: inventory) {
String output = ???.???(apple);
System.out.println(output);
Search WWH ::




Custom Search