需要一些有关包装类模式的帮助

发布于 2024-10-12 23:11:50 字数 1380 浏览 2 评论 0原文

我正在阅读装饰设计图案,但有些事情无法理解 我有四个类

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

反话 2024-10-19 23:11:50

这就是装饰器模式的作用。它用新属性“装饰”现有对象。

因此,您拥有的最终装饰对象是一个 CD 对象,其私有成员 computer 的类型为 Computer。但这个 computer 对象是由 Monitor 创建的,它还有一个名为 computer 的私有成员。此模式会不断重复,直到到达原始 Computer 对象。

现在,当您调用 description() 时,您正在执行 computer.description() 以及当前类的一些文本。第一个调用沿着链一直向上,直到到达第一个计算机对象,该对象打印 computer,然后打印 Disk 中的 和磁盘对象,然后是来自 Monitor 对象的和监视器,最后是来自 CD 对象的和 CD

可以每次使用一个新变量并将该对象传递到每个连续的对象中。这取决于您要寻找什么。在这里,您只是重用该变量。

这种 ASCII 艺术可能会帮助您理解对象之间的关系。每个框中的computer 指的是每个类的私有成员。

     CD           Monitor          Disk          Computer
 __________     ___________     ___________     ___________
| computer-|---|->computer-|---|->computer-|---|->computer |
|__________|   |___________|   |___________|   |___________|

现在,在下面的 ASCII 艺术中,您可以看到每个“框”打印的内容。顶部的箭头显示每个 description() 方法中 return 语句的执行顺序。框之间的箭头显示了每个 description() 方法的调用顺序

                           order of print
                  <-------------------------------------------------------------------+
                      CD             Monitor             Disk           Computer      |
                                                                                      |
 call from Main    __________     _______________     ____________     __________     |
----------------> | and a cd |-->| and a monitor |-->| and a disk |-->| computer |----+
                  |__________|   |_______________|   |____________|   |__________|

希望我蹩脚的 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 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 :)

悲念泪 2024-10-19 23:11:50

装饰器模式的要点是将新行为置于现有行为之上。在这种情况下,重点是通过添加新的外围设备来构建计算机。可能让您感到困惑的一件事是类的名称并不是特别适合该行为。例如,我可能会将其命名为 DiskAddition(一旦您了解了该模式,甚至可以将其命名为 DiskDecorator),而不是简单地命名为 Disk,因为其目的是您拿一台计算机并向其添加磁盘,而不是从计算机创建新磁盘。对结果对象重复使用相同的变量可能是合理的,但在教学环境中并不是特别有指导意义。如果我将其重写如下,可能会帮助您理解发生了什么。

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() 
      + ".");
  }
}

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() 
      + ".");
  }
}
兰花执着 2024-10-19 23:11:50

每次他创建一个新对象时,他都会传入现有对象,并且该新对象会保存旧对象。因此,每个新组件都会保存前一个组件,并且在其描述函数中,它还会调用其已保存组件的描述。

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.

污味仙女 2024-10-19 23:11:50

可能是:

Computer computer1 = new Computer();
Computer computer2 = new Disk(computer1);
Computer computer3 = new Monitor(computer2);
Computer computer4 = new CD(computer3);
Computer computer5 = new CD(computer4);

变量 computer 刚刚被重用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文