Reputation:
I am reading the decorative design pattern and could not understand few things I have four classes
public class Computer
{
public Computer()
{
}
public String description()
{
return "computer";
}
}
public abstract class ComponentDecorator extends Computer
{
public abstract String description();
}
public class Disk extends ComponentDecorator
{
Computer computer;
public Disk(Computer c)
{
computer = c;
}
public String description()
{
return computer.description() + " and a disk";
}
}
public class Monitor extends ComponentDecorator
{
Computer computer;
public Monitor(Computer c)
{
computer = c;
}
public String description()
{
return computer.description() + " and a monitor";
}
}
This is the final test class
public class Test
{
public static void main(String args[])
{
Computer computer = new Computer();
computer = new Disk(computer);
computer = new Monitor(computer);
computer = new CD(computer);
computer = new CD(computer);
System.out.println("You're getting a " + computer.description()
+ ".");
}
}
Now the final output is
computer and a disk and a monitor and a cd
The thing which is confusing me is that
1)Why he has taken the same object name computer, why not computer 1 , computer 2)If computer obj is same don't it mean that only last declaration will be valid and other will be overwritten
in my thinking the output should be
computer and a CD
Upvotes: 0
Views: 497
Reputation: 95528
This is what the decorator pattern does. It "decorates" an existing object with new attributes.
So the final decorated object that you have is a CD
object, who's private member computer
is of type Computer
. But this computer
object is the one created by Monitor
, who also has a private member called computer
. This pattern repeats itself until you reach the original Computer
object.
Now when you call description()
, you're doing computer.description()
plus some text for the current class. That first call goes all the way up the chain until you reach the first computer object, which prints computer
, then and a disk
from the Disk
object, and then and a monitor
from the Monitor
object, and finally and a CD
from the CD
object.
You could use a new variable each time and pass that object into each successive object. It depends on what you're looking for. Here, you're simply reusing the variable.
This ASCII art might help you understand the relationship between the objects. The computer
in each box refers to the private member of each class.
CD Monitor Disk Computer
__________ ___________ ___________ ___________
| computer-|---|->computer-|---|->computer-|---|->computer |
|__________| |___________| |___________| |___________|
Now in the following ASCII art you see what each "box" prints. The arrow on top shows the order of execution of the return statement in each description()
method. The arrow in between the boxes shows the order in which each description()
method is called
order of print
<-------------------------------------------------------------------+
CD Monitor Disk Computer |
|
call from Main __________ _______________ ____________ __________ |
----------------> | and a cd |-->| and a monitor |-->| and a disk |-->| computer |----+
|__________| |_______________| |____________| |__________|
Hopefully my crappy ASCII art helps :)
Upvotes: 3
Reputation: 532505
The point of the decorator pattern is to layer a new behavior on top of an existing behavior. In this case the point is that you build up a computer by adding new peripherals. One thing that might be confusing you is that the names of the classes are not particularly well-suited to the behavior. For example, I'd probably name it DiskAddition
(or even DiskDecorator
once you understand the pattern) rather than simply Disk
because the intent is that you take a computer and add a disk to it, not that you create a new disk from a computer. Reusing the same variable for the resultant object is probably reasonable, but not particularly instructive in a teaching context. If I were to rewrite it as follows it might help you understand what is going on.
public class Test
{
public static void main(String args[])
{
Computer computer = new Computer();
Computer computerWithDisk = new DiskAddition(computer);
Computer computerWithDiskAndMonitor = new MonitorAddition(computerWithDisk);
Computer computerWithDiskMonitorAndCD = new CDAddition(computerWithDiskAndMonitor);
Computer computerWithDiskMonitorAnd2CDs = new CDAddition(computerWithDiskMonitorAndCD);
System.out.println("You're getting a " + computer.description()
+ ".");
}
}
Upvotes: 1
Reputation: 2641
It could have been:
Computer computer1 = new Computer();
Computer computer2 = new Disk(computer1);
Computer computer3 = new Monitor(computer2);
Computer computer4 = new CD(computer3);
Computer computer5 = new CD(computer4);
The variable computer
is just being reused
Upvotes: 0
Reputation: 4408
Each time he creates a new object, he passes in the existing one, and that new object saves the old one. So each new one is saving the previous, and in its description function, it also calls its saved component's description.
Upvotes: 0