-4

This is Observer Pattern

enter image description here

This is Command Pattern, with a highlight to make it similar to Observer Pattern

Modified UML of Command Pattern

The only differences are:

  • Receiver IS-A Invoker in Observer Pattern
  • Observer Pattern knows when to call the Execute() in Command Pattern

Correct?

2

The command pattern provides an abstraction for handling function pointers and closures in languages that do not directly support such features.

The observer pattern provides an event system (a subject notifying one or more listener functions of a change in state of the subject) for languages that do not directly support events.

Both use objects to provide a function pointers abstraction. There are similarities. But the one big difference between them is that the observer pattern handles calling multiple functions, whereas the command pattern handles just one.

  • For your last sentence: If the Invoker in command pattern stores those commands, it can also handling multiple Execution. Appreciate all other idea. – Niing Apr 4 at 9:39
0

I Would say the difference between the two patterns is that in the command pattern you have a command object. Which is passed, may be persisted and contains all the information required to execute.

eg: BuyProduct: Command { product, user }

With the observer pattern you have no object and are simply forwarding generic change events

eg CollectionChanged(List<Item> addedItems added, List<Item> removedItems)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.