Ruby on Rails 模式 - 装饰者与展示者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7860301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:13:16  来源:igfitidea点击:

Ruby on Rails patterns - decorator vs presenter

ruby-on-railsdesign-patternsdecoratorpresenter

提问by keruilin

There is all sorts of talk lately in the Ruby on Rails community about decoratorsand presenters.

最近 Ruby on Rails 社区有各种各样关于装饰器和展示器的讨论。

What is the essential difference between the two? If there is, what are the clues that tell me which one to use over the other? Or perhaps to use the two in conjunction?

两者的本质区别是什么?如果有,有什么线索可以告诉我使用哪一个而不是另一个?或者也许将两者结合使用?

采纳答案by Dave Newton

A decorator is more of a "let's add some functionality to this entity". A presenter is more of a "let's build a bridge between the model/backend and view". The presenter pattern has several interpretations.

装饰器更像是“让我们向该实体添加一些功能”。演示者更像是“让我们在模型/后端和视图之间架起一座桥梁”。演示者模式有多种解释。

Decorators are generic/general purpose. Presenters have a narrower range of responsibilities/uses. Decorators are used across domains, presenters are almost always related to view-like functionality.

装饰器是通用的/通用的。演示者的职责/用途范围更窄。装饰器跨域使用,演示者几乎总是与类似视图的功能相关。

回答by dpaluy

I suggest you to check this - Exhibit vs Presenter.

我建议你检查这个 - Exhibit vs Presenter

Decorator is a design pattern that is used to extend the functionality of a specific object by wrapping it, without effecting other instances of that object. In general, the decorator pattern is an example of the open/close principle(the class is closed for modifications, but available for extensions).

装饰器是一种设计模式,用于通过包装特定对象来扩展特定对象的功能,而不影响该对象的其他实例。一般来说,装饰器模式是打开/关闭原则的一个例子 (类对于修改是关闭的,但可用于扩展)。

Both the exhibit and presenter patterns are a kind of the decorator pattern.

展示者模式和展示者模式都是装饰者模式的一种。