有状态命令模式
我想知道在域模型上下文中表示编辑表单的最佳方式是什么,最终我选择了命令设计模式。
据此:使用命令设计模式 命令应该是不可变的,这不是我所需要的 - 我需要具有可编辑参数的有状态命令(整个命令将在 ui/form 中编辑)
为什么认为有状态命令是不好的?
< strong>编辑:一段时间后,现在很清楚我正在寻找 ViewModel 模式。这是对任何 Web 应用程序屏幕进行建模的适当方法。基本上它是有状态的(每个视图实例)控制器。
I was wondering what is the best way to represent an edit form in the context of domain model and I ended up with the command design pattern.
According to this: Using Command Design pattern
commands should be immutable which is not what I need - I need stateful command with editable parameters (the whole command will be edited in ui/form)
Why is it considered to be bad to have stateful command?
EDIT: After some time it is now clear I was looking for the ViewModel pattern. That is the appropriate way to model any webapp screen. Basically it is stateful (per view instance) controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仍然希望它们是不可变的,因为它们可以用于从快照重新创建当前状态。但它们可以拥有您想要的所有参数,只要您确保它们在应用命令后不会更改即可。
You still want them to be immutable, because they can then be used to recreate the current state from a snapshot. But they can have all the parameters you want, as long as you make sure they cannot change after applying the command.
如果你希望它可变,我建议使用 FlyWeight 模式。您可以使其类似于命令模式,因为它是对象的散列,并且它会重用相同的对象(如果它们已经存在),从而保留对象状态。
您可以将 FlyWeight 视为一组可以动态调用的单例对象(通过哈希函数)。
因此,flyweightfactory 会将所有对象存储在哈希映射中,并且您可以使用该工厂来检索维护其状态的对象。
http://www.avajava.com/tutorials/lessons/flyweight-pattern.html -->供任何人参考。
if you want it mutable i would suggest using a FlyWeight Pattern instead. You can make it similar to a command pattern since its a hash of objects and it reuses the same objects if they exist already thus persisting the objects state.
You can think of FlyWeight as a group of singleton objects you can call on the fly (by a hash function).
So a flyweightfactory would house all your objects in a hash map and you could use the factory to retrieve the object maintaining its state.
http://www.avajava.com/tutorials/lessons/flyweight-pattern.html --> for anyone's reference.