R
R
Roman2016-04-11 20:46:07
Java
Roman, 2016-04-11 20:46:07

What is the Decorator pattern and why is it needed?

I can’t understand the essence of the Decorator template and therefore I can’t figure out how to add the code:

//TODO1: Add abstract class Component
abstract class Component {
    
}

class SalesTicket extends Component {
    public void prtTicket() {
          System.out.println("My ticket");
    }
}
//TODO2: Add abstract class TicketDecorator
//It has Component member, constructor and callTrailer method
abstract class TicketDecorator {
    Component myComponent;
    public TicketDecorator(Component myComponent){
        
    }
    public void callTrailer() {
        
    };    
}
 
 
class Header1 extends TicketDecorator {
    public Header1 (Component myComponent) {
        super(myComponent);
    }
    public void prtTicket () {
        System.out.println("*** Header1 ***");
        super.callTrailer();
    }
}
class Header2 extends TicketDecorator {
    public Header2 (Component myComponent) {
        super(myComponent);
    }
    public void prtTicket () {
        System.out.println("*** Header2 ***");
        super.callTrailer();
    }
}
class Footer1 extends TicketDecorator {
    public Footer1 (Component myComponent) {
        super(myComponent);
    }
    public void prtTicket() {
        super.callTrailer();
        System.out.println("*** Footer1 ***");
    }
}
class Footer2 extends TicketDecorator {
    public Footer2 (Component myComponent) {
        super(myComponent);
    }
    public void prtTicket() {
        super.callTrailer();
        System.out.println("*** Footer2 ***");
   }
}
class Client {
    public static void main(String[] args) {
       //TODO3: Create a decorated sales ticket and print it       
   }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-04-11
Protko @Fesor

What you have implemented is not a decorator. Decorators are interfaces, not abstract classes.
A decorator is a pattern in which we "extend" the behavior of an object without changing it. At the same time, an important component - the interface of the object does not change.
Decorations have a significant advantage over inheritance, namely the ability to create endless chains of decorators with the ability to arbitrarily change their places without making changes directly to the code.
For example, when using a dependency container, we can wrap some service in a decorator temporarily, and in the decorator log the arguments and the result of the execution of methods. Let's say this is only needed for quick debugging. We quickly make a decorator, slip it instead of a real implementation (usually this is one line in the di-config) and voila. We did not make changes to the code, and therefore could not break anything by accident. The history of changes will look nice. Yes, and testing such things is much easier.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question