is Decorator Pattern same as Calling super method?

Go To StackoverFlow.com

2

This is an example of Decorator Pattern.

public class Computer {
public String Description(){
    return "Computer";
}
}

public abstract class Decorator extends Computer{
public abstract String description();
}

public class Monitor extends Decorator{

Computer computer;
public Monitor(Computer c){
    computer = c;
}

public String description() {
    return computer.Description() + " and Monitor";
}
}

public class main {
public static void main(String args[]){
    Computer c = new Computer();
    Monitor m = new Monitor(c);
    System.out.println(m.description());
}
}

Is this the same with calling super method of the super class?? like when monitor inherited from computer then calling the super.Description() in the computer class inside the Description method in monitor class?

2012-04-04 07:19
by user1293258


5

With calling the super method, you need to make a new subclass for every possibility. For instance, What if you want a computer with a monitor and a mouse? You can make a subclass which inherits Monitor. Now say that you want a computer without a monitor and with a mouse. You'll need another subclass for that one as well, which inherits Computer. There are only two devices, and you already need three subclasses (Monitor, Mouse and MonitorAndMouse). What if your computer can also have a printer, a scanner and a set of speakers?

With the decorator pattern, you can 'decorate' the computer with a new device. Like you did with the Monitor, you can make a different decorator for each device. To create a computer with all devices, you can now do something like

Computer = new Speakers(
    new Scanner(
        new Printer(
            new Mouse(
                new Monitor(
                    new Computer()
                )
            )
        )
    )
);
computer.description();

Now description() returns Computer and Monitor and Mouse and Printer and Scanner and Speakers. You can mix and match the devices with the computer, without the need to create a new subclass for each combination.

On comment:

how about in my example I remove the decorator class then monitor didnt inherit from computer class, instead I just put instance of computer class inside the monitor class then do the same.

This way, you can only go one level deep, you can only add one device to a computer. In essence, it is a non-extendable decorator. The decorator pattern is a way to favor composition over inheritance, because of the immense growth of possibilities you get when adding multiple devices. With your solution, you are favoring composition over inheritance by not making a subclass of the Computer but composing the Monitor with a Computer. It is almost a decorator pattern, only you have taken a step back.

2012-04-04 07:43
by Jesse van Assen
how about in my example I remove the decorator class then monitor didnt inherit from computer class, instead I just put instance of computer class inside the monitor class then do the same - user1293258 2012-04-04 07:53


1

To answer your question in short: No, it is not the same.

A Decorator uses an existing Object to modify the behauviour of it, but for the outside world it looks as if it where a typ of the class it decorates.

To explain this in your example you have a Computer and your Computer has some properties. Somewhere in your Code you are creating an instance of this Computer. You are filling it with all the needed data.

Now comes the Decorator into place. You need a class which decorates this instance of the computer you just created! Exactly this one.

I will take your Computer and add a method to turn it off and on

public class Computer {
    private boolean isOn = true;
    private String description = "Computer";        

    public vod turnOn(){
        this.isOn=true;
    }

    public void turnOff(){
        this.isOn = false;
    }

    public void setDescription(String description){
        this.description = description;
    }

    public String Description(){
        return this.description;
    }
}

Now i am creating a decorator, where you cannot turn your computer off.

public class ComputerDecorator1 extends Computer {
    private Computer computer;

    public ComputerDecorator1(Computer computer){
       this.computer = computer;
    }

    public vod turnOn(){
        this.computer.turnOn();
    }

    public void turnOff(){
        System.out.println("You cannot turn me off!!!!");
    }

    public void setDescription(String description){
        this.computer.setDescrition(descritption);
    }

    public String Description(){
        return this.computer.Description();
    }
}

As you can see you have to give him a Computer instance in the constructor. This is the computer which we are decorating. Every method call will be handed down to this class. Normally you have to overwrite all method an pass them to the decorated object. In this case the Computer.

You can use this to modify the behaviour of an object, whitout creating a new class of it. The benefit is that when you get an instance of a computer from somewhere else you can add this filter, so that the rest of your programm is unable to turn it off.

This is how you use a Decorator in your Program:

public void getMyComputerAndTurnItOff(){
    Computer myComputer = getMyComputer();
    myComputer.turnOff();
}

public Computer getMyComputer(){
    //Here you will load a Computer from an other class
    Computer computer = this.otherClass.loadComputerFromDB();

    //Now we could return it, but what if you shouldn't be able to turn it off?
    //easy add our decorator
    return new ComputerDecorator1(computer);
}

If you want to use a Decorator you need to modify the behaviour, otherwise it is useless! In you example with Computer and Monitor from a logical point a monitor cannot be decorator for a Computer. For me these are two different objects.

I hope i could make it a little bit clearer what a Decorator is!

2012-04-04 08:16
by Adi
Ads