- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经结合外观和构建器模式进行了练习。 Root 是一个简单的 LoginService 接口(interface)
public interface LoginService {
void addUser(int id, String username, String password, String mail);
boolean login(String username, String password);
void logout();
}
其他类是 LoginServiceDecorator 和一些具体的装饰器。最后是一个 Builder,通过这种方式进行测试:
service = new LoginServiceBuilder().withValidation().withEncoding().withLogging().toService();
有一些测试用例。
一切都很好,直到实现了 toService()
方法,我不知道如何实现。我表明我被困住了:
LoginServiceBuilder
public class LoginServiceBuilder {
/*
* Note that the decorators must be connected in reverse order of the
* builder method invocations. So we use a stack to hold all decorator
* instances and at the end of the building process, we connect the
* decorators in the right order.
*/
private Stack<LoginServiceDecorator> stack = new Stack<LoginServiceDecorator>();
public LoginServiceBuilder() {
}
public LoginServiceBuilder withValidation() {
stack.push(new ValidationDecorator(new LoginServiceImpl()));
return this;
}
public LoginServiceBuilder withEncoding() {
stack.push(new EncodingDecorator(new LoginServiceImpl()));
return this;
}
public LoginServiceBuilder withLogging() {
stack.push(new LoggingDecorator(new LoginServiceImpl()));
return this;
}
public LoginService toService() {
// Here I stucked
}
好吧,最后我放弃了,看了一下解决方案:
public LoginService toService() {
LoginService service = new LoginServiceImpl();
while (!stack.isEmpty()) {
LoginServiceDecorator decorator = stack.pop();
decorator.setService(service); // just to set the private member there (Type of the Interface as shown at beginning)
service = decorator;
}
return service;
}
为什么我还在挠头:对我来说,看起来当堆栈为空时,服务很简单,它捕获的最后一个服务很简单。也许有人可以用温和的语言向我解释为什么我现在应该拥有所有装饰器。
提前致谢
最佳答案
装饰器的作用是动态地在对象上添加行为。这就是他们实现相同接口(interface)的原因。
您应该使用一个装饰器来添加一种行为。这就是你创建一堆装饰器的原因
decorator.anOperation()
应该执行 decoratedObject.anOperation()
通过使用继承,您可以替换被装饰的对象而不是被装饰的对象。这就是为什么要有 service = Decorator
的原因。在您的示例中,您用关联的装饰器替换您的服务(装饰对象),然后在装饰对象上应用下一个装饰器。
在堆栈的末尾,您有一个“fullDecolatedObject”。
有关更多解释,我发现这个网站很有用 http://www.dofactory.com 。该实现是用 C# 语言实现的,但可以轻松地转换为 Java 语言。观看此页面:http://www.dofactory.com/net/decorator-design-pattern
希望这有帮助
关于java - 幕后的外观和构建器模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36483457/
对于要执行的JS代码,由解析器逐行解析,如果代码无效,则显示错误信息。如果一切正确,那么解析器会生成一个称为抽象语法树的数据结构。然后使用此抽象语法树为解释器生成字节码以供执行。 以上快速分析可以总结
似乎如果我在 () 中包装一个字符串、 bool 值或数字原始值,我会得到一个包装原始值的字符串、 bool 值、数字对象。这个结论正确吗? 此外,似乎 () 对于字符串和 bool 值是可选的,但对
我有几个关于 Java 中的嵌套类的问题。 关于内存分配,嵌套类是如何“隐藏”的? 您不能在嵌套类中声明静态变量(我认为确切的错误是静态属性只能在顶级类中声明)。为什么会这样?嵌套类还有哪些其他限制?
对于没有使用 Lambda Expresstions 经验的人,下面的代码让它看起来很神奇: int totalScore = File.ReadLines(@"c:/names.txt")
一个朴素的类型系统会将对象存储为指向其类型的指针(其中包含许多有用的信息,如 vtable、对象大小等),然后是其数据。如果.Net 有这样的类型系统,object在 32 位系统上占用 4 个字节,
我有以下用于字符串加密和解密的JAVA代码: public class AES { private SecretKeySpec setKey(String myKey) {
我试图了解 React 中的“组件”。 我有几个问题,所以我认为社区是提出问题的最佳场所。 1 - 当我们这样做时: var foo = React.createClass ({ ... }); Co
我想知道 ref 和 out 关键字在幕后是如何工作的?例如,如果我们在方法上使用它,它会把这个值类型变量放入某个类中以便像使用引用类型一样使用它吗? 最佳答案 in order to work wi
我对 Rails ActiveRecord、Doctrine for PHP(以及类似的 ORM)背后的一些设计很感兴趣。 ORM 如何设法实现链式访问器等功能,它们通常需要多深的工作? ORM 如何
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: C# “as” cast vs classic cast 我想知道当我做类似的事情时,.Net CLR 的底
好吧,这似乎是一个菜鸟问题,但我认识的许多 Web 开发人员都没有完全理解这个问题。 基本上,如何使用 FileUpload 控件的上传事件将文件从网页文件输入框上传到网络服务器(例如托管 .net
我很熟悉,按下返回键会导致 activity 被“销毁”,或者当开发人员调用函数 finish() 时,或者当系统需要时内存等... 并且还熟悉我们需要在 onDestroy 中执行清理过程,例如 u
我正在使用 GameViewController 和 GameScene。这个链接到 GameScene.sks。在 GameViewController 中,我将 aspect radio 设置为
关于 EF 的另一个问题: 我想知道在遍历查询结果时幕后发生了什么。 例如,查看以下代码: var activeSources = from e in entitiesContext.Sources
你好,我有一个关于 d3 的性质的问题,我认为这是关于 d3 的非常深入的细节。 据我了解, d3 中的变量声明,如 var svg = d3.select('boby').append('svg'
我是一名优秀的程序员,十分优秀!