Java Reference
In-Depth Information
To not decorate all classes of the same type, you need to create a custom qualii er and annotate
the delegate injection point and the classes that you want decorated. You'll create a Plate product
that implements the Product interface. Only this product must be discounted. To implement this
requirement, you annotate it with a custom qualii er, thus excluding the other product from being
decorated.
You create a custom qualii er and call it @ClearanceSale .
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, PARAMETER, TYPE})
public @interface ClearanceSale {}
In Listing 7‐11, you create the new implementation of the Product interface and annotate it with
your custom qualii er.
LISTING 7‐11: Class to be decorated is annotated with custom qualii er
@ClearanceSale
public class Plate implements Product {
private String label = "Plate";
private double price = 50.00;
public void setLabel(String label) {
this.label = label;
}
public void setPrice(double price) {
this.price = price;
}
public String getLabel() {
return label;
}
public double getPrice() {
return price;
}
public String generateLabel() {
return price + ", " + label;
}
}
Finally, you annotate the delegate injection point in the decorator that you want to invoke. In this
case, choose the PriceDiscountDecorator decorator.
@ClearanceSale
@Any
@Inject
@Delegate
private Product product;
Search WWH ::




Custom Search