需要一些有关包装类模式的帮助
我正在阅读装饰设计图案,但有些事情无法理解 我有四个类
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";
}
}
这是最终的测试类
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()
+ ".");
}
}
现在最终的输出是
computer and a disk and a monitor and a cd
让我困惑的是
1)为什么他采用相同的对象名称计算机,为什么不是计算机1,计算机2)如果计算机对象相同则不要'这意味着只有最后一个声明有效,其他声明将被覆盖,
在我看来,输出应该是
computer and a CD
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是装饰器模式的作用。它用新属性“装饰”现有对象。
因此,您拥有的最终装饰对象是一个
CD
对象,其私有成员computer
的类型为Computer
。但这个computer
对象是由Monitor
创建的,它还有一个名为computer
的私有成员。此模式会不断重复,直到到达原始Computer
对象。现在,当您调用
description()
时,您正在执行computer.description()
以及当前类的一些文本。第一个调用沿着链一直向上,直到到达第一个计算机对象,该对象打印computer
,然后打印Disk
中的和磁盘
对象,然后是来自Monitor
对象的和监视器
,最后是来自CD
对象的和 CD
。您可以每次使用一个新变量并将该对象传递到每个连续的对象中。这取决于您要寻找什么。在这里,您只是重用该变量。
这种 ASCII 艺术可能会帮助您理解对象之间的关系。每个框中的
computer
指的是每个类的私有成员。现在,在下面的 ASCII 艺术中,您可以看到每个“框”打印的内容。顶部的箭头显示每个
description()
方法中 return 语句的执行顺序。框之间的箭头显示了每个description()
方法的调用顺序希望我蹩脚的 ASCII 艺术能有所帮助:)
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 membercomputer
is of typeComputer
. But thiscomputer
object is the one created byMonitor
, who also has a private member calledcomputer
. This pattern repeats itself until you reach the originalComputer
object.Now when you call
description()
, you're doingcomputer.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 printscomputer
, thenand a disk
from theDisk
object, and thenand a monitor
from theMonitor
object, and finallyand a CD
from theCD
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.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 eachdescription()
method is calledHopefully my crappy ASCII art helps :)
装饰器模式的要点是将新行为置于现有行为之上。在这种情况下,重点是通过添加新的外围设备来构建计算机。可能让您感到困惑的一件事是类的名称并不是特别适合该行为。例如,我可能会将其命名为
DiskAddition
(一旦您了解了该模式,甚至可以将其命名为DiskDecorator
),而不是简单地命名为Disk
,因为其目的是您拿一台计算机并向其添加磁盘,而不是从计算机创建新磁盘。对结果对象重复使用相同的变量可能是合理的,但在教学环境中并不是特别有指导意义。如果我将其重写如下,可能会帮助您理解发生了什么。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 evenDiskDecorator
once you understand the pattern) rather than simplyDisk
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.每次他创建一个新对象时,他都会传入现有对象,并且该新对象会保存旧对象。因此,每个新组件都会保存前一个组件,并且在其描述函数中,它还会调用其已保存组件的描述。
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.
可能是:
变量
computer
刚刚被重用It could have been:
The variable
computer
is just being reused