开发者

stateful command pattern

开发者 https://www.devze.com 2023-02-08 16:38 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号