Java Reference
In-Depth Information
work. The coni guration might seem disappointing because you have already annotated your
decorator; nevertheless, the coni guration is simple and necessary so that you can dei ne the order of
execution of the decorators (if more than one). Add the following lines to beans.xml :
<class> com.devchronicles.decorator.PriceDiscountDecorator </class>
Your work is done. You can now use your decorator.
Product product;
public void createPriceList(){
System.out.println("Label: " + product.generateLabel());
An instance of Table is injected into the Product member variable, and the generateLabel method
is called. The output to the console will be as follows:
Label: 12.5, Dining Table (Discounted)
When a call is made to the generateLabel method of any Product instance, the container
intercepts it. The call is delegated to the appropriate method of the PriceDiscountDecorator
decorator, where it discounts the product's price and passes the call onto the original destination by
calling the generateLabel method of the Table object.
A call chain is set up that includes all the decorators that are declared to decorate classes that
implement the Product interface. The order in which the decorators are called is determined by the
order in which they are declared in the bean.xml deployment descriptor.
You are going to see this in action in Listing 7‐10, where you dei ne another decorator. You create
the BlackFridayDiscountDecorator decorator, implement the Product interface, and add the
@Decorator and @Delegate annotations.
LISTING 7‐10: The BlackFridayDecorator decorator
public class BlackFridayDiscountDecorator extends AbstractDiscountDecorator {
private Product product;
public String generateLabel() {
product.setPrice(product.getPrice() * 0.25);
return product.generateLabel();
// Not all methods shown
Search WWH ::

Custom Search