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 edi开发者_Go百科table 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.
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.
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.
精彩评论