--- layout: pattern title: Model-View-ViewModel folder: model-view-viewmodel permalink: /patterns/model-view-viewmodel/ categories: Architectural language: en tags: - Decoupling --- ## Also known as Model–View–Binder ## Intent To apply "[Separation of Concerns](https://java-design-patterns.com/principles/#separation-of-concerns)" to separate the logic from the UI components and allow developers to work on UI without affecting the logic and vice versa. ## Explanation Wikipedia says > Model–view–viewmodel (MVVM) is a software architectural pattern that facilitates the separation of the development of the graphical user interface (the view) – be it via a markup language or GUI code – from the development of the business logic or back-end logic (the model) so that the view is not dependent on any specific model platform. **Programmatic Example** Zkoss implementation: > ViewModel will hold the business logic and expose the data from model to View ```java public class BookViewModel { @WireVariable private List bookList; private Book selectedBook; private BookService bookService = new BookServiceImpl(); public Book getSelectedBook() { return selectedBook; } @NotifyChange("selectedBook") public void setSelectedBook(Book selectedBook) { this.selectedBook = selectedBook; } public List getBookList() { return bookService.load(); } /** Deleting a book. */ @Command @NotifyChange({"selectedBook","bookList"}) public void deleteBook() { if (selectedBook != null) { getBookList().remove(selectedBook); selectedBook = null; } } ``` > View will have no logic, only UI elements ```xml