2

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.

8
  • Have you tried implementing mutable commands? Did you hit unsolvable problems in the process? Commented Feb 6, 2011 at 15:05
  • No - I'm going to implement it I'm just wondering if there are no gotchas... Commented Feb 6, 2011 at 15:08
  • @kamil, is this so you can do undo? Commented Feb 6, 2011 at 15:10
  • 2
    @kamil, my point was do you want to use the pattern so you can do undo? If so, you are using the wrong pattern. Use Mememto Pattern Commented Feb 6, 2011 at 15:19
  • 2
    That is a new command, generated by starting with a copy of an existing one. Commented Feb 10, 2011 at 23:31

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.