Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is MVVM itself a Observer pattern?

I getting confuse with concepts MVVM and observer pattern. Is MVVM itself follows the observer pattern. Or they are totally different from each other?

Can anyone please explain me in simple word.

Thanks in advance.

like image 928
gofor.net Avatar asked May 12 '14 12:05

gofor.net


People also ask

What type of design pattern is MVVM?

Model-View-ViewModel (MVVM) is a software design pattern that is structured to separate program logic and user interface controls. MVVM is also known as model-view-binder and was created by Microsoft architects Ken Cooper and John Gossman.

What is Observer in MVVM?

The observe() method takes a LifecycleOwner object. This subscribes the Observer object to the LiveData object so that it is notified of changes. You usually attach the Observer object in a UI controller, such as an activity or fragment.

Is MVC an observer pattern?

2 The Subject-Observer Pattern in MVC. The relationship between the model and viewport is actually defined by another design pattern. The subject-observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

What type of pattern is observer pattern?

Observer is a behavioral design pattern. It specifies communication between objects: observable and observers. An observable is an object which notifies observers about the changes in its state.


2 Answers

MVVM- and Observable- Patterns are different Patterns and you will find many great examples. Assuming you are implementing a MVVM Phone App, these two patterns work great in combination:

  • Your ViewModel (MVVM) has Properties which you want to display / update in your XAML-VIEW (MVVM). Anytime you set (or update) a Property value (in your ViewModel) you trigger something like ()=> PropertyChanged("PropertyName);
  • The Observer is now in your MVVM Framework (Or Base Class of ViewModel) this component observes these changes and manages the update with the VIEW.
like image 65
flo scheiwiller Avatar answered Nov 07 '22 09:11

flo scheiwiller


MVVM is basically an UI pattern. It's main purpose is to separate view from logic. This helps to structure applications and make them more testable because it introduces a clear separation of concerns and allows you to develop extensible applications (if done right).

It is a guidline which can also take advantage of other patterns and works very well with the Binding Engine of XAML-Related development technologies.

Since MVVM is an guidance to structure your application code it can surely take advantage of the observer pattern. For example there is an framework out there which is called ReactiveUI http://www.reactiveui.net/ it implements the MVVM pattern with observeable technologies (RX).

As well with the MVVM basics like notifying the view from the view model via NotifiedChangedProperty and ObservableCollections.

HTH

like image 2
silverfighter Avatar answered Nov 07 '22 10:11

silverfighter