Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Decorator模式:是否要求所有Decorator都添加值,而不管初始化的顺序如何_Java_Oop_Design Patterns_Decorator - Fatal编程技术网

Java Decorator模式:是否要求所有Decorator都添加值,而不管初始化的顺序如何

Java Decorator模式:是否要求所有Decorator都添加值,而不管初始化的顺序如何,java,oop,design-patterns,decorator,Java,Oop,Design Patterns,Decorator,我有一个关于装饰师和他的初始化顺序的问题。 是否要求每个decorator都可以由其他decorator扩展,或者如果扩展decorator有限制,也可以这样做。例如: Subject subject = new Subject(); decorator = new ErrorHandlingDecorator(subject); //Extends for error handling, when error is detected it interupt the current

我有一个关于装饰师和他的初始化顺序的问题。 是否要求每个decorator都可以由其他decorator扩展,或者如果扩展decorator有限制,也可以这样做。例如:

Subject subject = new Subject();
decorator       = new ErrorHandlingDecorator(subject); //Extends for error handling, when error is detected it interupt the current thread.
decorator       = new ExecuteFunctionDecorator(decorator); //Execute a function and run the executeFunction() on his parent.

decorator.executeFunction();
这里,
ExecuteFunctionDecorator
可以将结果传递给
ErrorHandlingDecorator
,因为它首先执行函数。但是,当您像初始化
ErrorHandlingDecorator
下面的代码那样初始化它时,它是无用的,因为它首先检查错误,然后执行函数

Subject subject  = new Subject();
decorator        = new ExecuteFunctionDecorator(subject); //Execute a function and run the executeFunction() on his parent.
decorator        = new ErrorHandlingDecorator(decorator); //Extends for error handling, when error is detected it interupt the current thread.
所以我的问题是:这个示例仍然是一个装饰器,还是要求所有装饰器都添加值,不管初始化的顺序如何,或者装饰器在“不正确”的初始化之后可能没有意义

欢迎提供任何相关信息


在这方面,

对于装饰者来说,一个经典的例子(我认为它来自GOF书籍)是UI应用程序中的小部件或面板,可以通过进一步的样式(例如边框)来增强(或装饰)

想象两个不同的装饰器,一个用1像素大小的边框装饰矩形部件,另一个用5像素大小的破折号边框

您是否希望(1)
new-DashedBorder(new-Border(new-Panel())
的外观与(2)
new-Border(new-DashedBorder(new-Panel())
的外观相同?我不会的。在第一种情况下,我希望1像素大小的边框由5像素大小的虚线边框封装,在第二种情况下,结果相反


装饰师做他们所谓的工作。他们装饰你的物品。用两种不同的东西装饰一个对象可能会产生相同的结果,但它们不是必须的。

设计模式是指导原则,不要担心那些微小的调整,使其更适合您的模型。没有人会因为装饰器模式的理论模型有点不同就说你不应该做一些有意义的事情。为什么你需要一个
ExecuteFunctionDecorator
?对我来说,
executeFunction()
应该是
Subject
中的一个抽象方法。操作该对象的客户端代码不知道该对象已修饰,只会对其调用
executeFunction()
。对于原始抽象及其所有子类家族已经应该提供的行为,您不需要额外的装饰器。