Struts 动作和组合优于继承
当我想应用 DRY 原则时,即针对不同用例(例如管理员角色和操作员角色)统一多个 Struts 操作的代码,一种选择是使用抽象基类“BaseAction”操作,然后使用“AdminAction extends BaseAction”和“OperatorAction extends BaseAction”。我会为抽象的 NewBaseAction、UpdateBaseAction、DeleteBaseAction、ListBaseAction 应用继承。
但有一个原则说“优先考虑组合而不是继承”(http://www.artima.com/lejava/articles/designprinciples4.html" rel="nofollow noreferrer">http://www. artima.com/lejava/articles/designprinciples4.html)。有没有一种方法可以通过使用接口以干净的方式实现这一点?
When I want to apply the DRY principle, i.e. to unify the code of multiple Struts action for different use-cases (for example the administrator role and the operator role ), one option would be to use an abstract base class "BaseAction" for the action and then use "AdminAction extends BaseAction" and "OperatorAction extends BaseAction". I would apply inheritance for an abstract NewBaseAction, UpdateBaseAction, DeleteBaseAction, ListBaseAction.
But there is a principle that says "favor composition over inheritance" (http://www.artima.com/lejava/articles/designprinciples4.html). Is there a way to implement this in a clean way by using interfaces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“优先考虑组合而不是继承”这句话通常是更好设计的线索。像 Struts 这样的框架引入了自己的编程模型。因此,您应该以遵循 Struts 最佳实践的方式编写 Struts 操作。
在你的情况下,编写基类还不错。问题是如何设计动作类层次结构,例如考虑使用 DispatchAction 作为您的基本操作类的部分功能。它将使您免于创建许多不必要的类。
查找 DRY 原则的“Struts 方式”用例。您可以在免费书籍 Struts 生存指南 中找到更多 Struts 最佳实践
Statement "favor composition over inheritance" it's a clue for better design in general. Frameworks like Struts introduce own programming model. So you should write Struts action in a way that they adhere to Struts best practices.
In your case writing base class isn't bad. The question is how to design action class hierarchy, e.g. consider using DispatchAction for part of your functionality as your base action class. It will save you from creating a lot unnecessary classes.
Find "Struts way" use cases of DRY principle. More Struts best practises you'll find in free book Struts Survival Guide
“优先考虑组合而不是继承”的解决方案是:
Action
使用的单独的非Action
类中,或者Action
类中,并且有一个可以使用这些行为中的任何行为的Action
。我使用 Struts 已经有几年了,但我认为对于 (2),您需要在
struts-config.xml
中使用一些技巧,配置多个Action
类的 code>,并且让Action
能够根据参数加载或选择不同的行为实现。这看起来有点非 Strutsy,因为它采用了通常位于struts-config.xml
中的一些控制逻辑,并将其隐藏在代码中。但根据您的开发文化,这实际上可能被认为是一件好事。
组合方法是否值得采用可能取决于您需要共享哪些代码,以及尝试将该代码与 Struts 样板文件隔离是否有意义。
我使用的最后一个 Struts 应用程序使用了继承。也许这是正确的做法,也许我们只是不知道更好。
The "favor composition over inheritance" solution would be to either:
Action
class used by all theAction
s in question, orAction
class, and have a singleAction
that can use any of these behaviors.It's been a few years since I did Struts, but I think for (2) you'll need a little trickery in the
struts-config.xml
, configuring several<action>
s of the sameAction
class, with different parameters, and having theAction
be able to load or select a different behavior implementation depending on the parameters. This seems a bit non-Strutsy, as it takes some of the control logic that would normally be right there in thestruts-config.xml
and hides it in code.But depending on your development culture that might actually be considered a good thing.
Whether the composition approach is worth doing probably depends on what code you need to share, and whether it makes sense to try to isolate that code from the Struts boilerplate.
The last Struts application I was on, we used inheritance. Might have been the right thing to do, might have been we just didn't know any better.
附带说明:
考虑到“new”和“update”通常是非常非常相似的操作,并且通常可以是具有单个 case 语句的相同 Action,而不是例如支持两个不同 JSP 的两个不同类。
As a side note:
Consider that "new" and "update" are often very, very similar operations, and can often be the same Action, with a single case statement, instead of two different classes supporting two different JSPs, for example.