# MVC vs MVVM

## MVC

### Flow



<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F2NEQwa0JFnbvLHwqHea1%2Fimage.png?alt=media&#x26;token=a594bbc9-6e18-44f0-a364-054a647ed026" alt=""><figcaption></figcaption></figure>

### Example (React)

```javascript
// Model/Data
const data = {
  counter: 0,
};

export default data;
```

```javascript
// View
import React from 'react';

const View = ({ counter, incrementCounter }) => (
  <div>
    <h1>Counter: {counter}</h1>
    <button onClick={incrementCounter}>Increment</button>
  </div>
);

export default View;
```

```javascript
// Controller
import React, { useState } from 'react';
import View from './View';
import data from './data';

const Controller = () => {
  const [counter, setCounter] = useState(data.counter);

  const incrementCounter = () => {
    setCounter(counter + 1);
  };

  return <View counter={counter} incrementCounter={incrementCounter} />;
};

export default Controller;
```

### Example (Angular)

```typescript
// Model/Data
export class DataModel {
  counter: number;
}
```

```typescript
<!-- View/Template -->
<div>
  <h1>Counter: {{ counter }}</h1>
  <button (click)="incrementCounter()">Increment</button>
</div>
```

```typescript
// Controller
import { Component } from '@angular/core';
import { DataModel } from './data.model';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  counter: number;

  constructor(private dataModel: DataModel) {
    this.counter = dataModel.counter;
  }

  incrementCounter(): void {
    this.counter++;
  }
}
```

## MVVM

### Flow

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FlHMu0HAEpI2LetAAD9KD%2Fimage.png?alt=media&#x26;token=8fae3ee8-6a88-498b-bfd2-17685b448149" alt=""><figcaption></figcaption></figure>

### Example (React)

```javascript
// Model/Data
const data = {
  counter: 0,
};

export default data;
```

```javascript
// ViewModel/Logic
import { useState } from 'react';
import data from './data';

const ViewModel = () => {
  const [counter, setCounter] = useState(data.counter);

  const incrementCounter = () => {
    setCounter(counter + 1);
  };

  return {
    counter,
    incrementCounter,
  };
};

export default ViewModel;
```

```javascript
// View/Presentation
import React from 'react';
import ViewModel from './ViewModel';

const View = () => {
  const viewModel = ViewModel();

  return (
    <div>
      <h1>Counter: {viewModel.counter}</h1>
      <button onClick={viewModel.incrementCounter}>Increment</button>
    </div>
  );
};

export default View;
```

### Example (Angular)

```typescript
// Model/Data
export class DataModel {
  counter: number;
}
```

```typescript
// ViewModel/Logic
import { Injectable } from '@angular/core';
import { DataModel } from './data.model';

@Injectable()
export class DataViewModel {
  private dataModel: DataModel;

  constructor() {
    this.dataModel = new DataModel();
    this.dataModel.counter = 0;
  }

  get counter(): number {
    return this.dataModel.counter;
  }

  incrementCounter(): void {
    this.dataModel.counter++;
  }
}
```

```html
<!-- View/Template -->
<div>
  <h1>Counter: {{ viewModel.counter }}</h1>
  <button (click)="viewModel.incrementCounter()">Increment</button>
</div>
```

## Similarity

* Both acts as a bridge between model and view
* Controller and view model both have one-to-many relationship to the view

## Comparison

* ViewModel supports data binding, which allows for automatic synchronization of data between the ViewModel and the View. The ViewModel exposes properties that the View binds to, and any changes in the ViewModel automatically update the corresponding elements in the View.
* Controller typically does not directly support data binding. It is responsible for explicitly updating the View when there are changes in the Model. The Controller retrieves data from the Model and explicitly render the View to reflect those changes.
* ViewModel does not have direct knowledge of the specific View instances that bind to it. The ViewModel is designed to be independent of the View implementation and easier for testing
* Controller has direct knowledge of the View and interacts with it to update its state. The Controller may have references to specific View instances and can manipulate the View directly to reflect changes in the Model.
* The entry point of MVC is controller while MVVM is view

##
