Files
java-design-patterns/model-view-presenter
Ilkka Seppälä 6cd2d0353a docs: Content SEO updates (#2990)
* update yaml frontmatter format

* update abstract document

* update abstract factory

* use the new pattern template

* acyclic visitor seo

* adapter seo

* ambassador seo

* acl seo

* aaa seo

* async method invocation seo

* balking seo

* bridge seo

* builder seo

* business delegate and bytecode seo

* caching seo

* callback seo

* chain seo

* update headings

* circuit breaker seo

* client session + collecting parameter seo

* collection pipeline seo

* combinator SEO

* command seo

* cqrs seo

* commander seo

* component seo

* composite seo

* composite entity seo

* composite view seo

* context object seo

* converter seo

* crtp seo

* currying seo

* dao seo

* data bus seo

* data locality seo

* data mapper seo

* dto seo

* decorator seo

* delegation seo

* di seo

* dirty flag seo

* domain model seo

* double buffer seo

* double checked locking seo

* double dispatch seo

* dynamic proxy seo

* event aggregator seo

* event-based asynchronous seo

* eda seo

* event queue seo

* event sourcing seo

* execute around seo

* extension objects seo

* facade seo

* factory seo

* factory kit seo

* factory method seo

* fanout/fanin seo

* feature toggle seo

* filterer seo

* fluent interface seo

* flux seo

* flyweight seo

* front controller seo

* function composition seo

* game loop seo

* gateway seo

* guarded suspension seo

* half-sync/half-async seo

* health check seo

* hexagonal seo

* identity map seo

* intercepting filter seo

* interpreter seo

* iterator seo

* layers seo

* lazy loading seo

* leader election seo

* leader/followers seo

* lockable object seo

* rename and add seo for marker interface

* master-worker seo

* mediator seo

* memento seo

* metadata mapping seo

* microservice aggregator seo

* api gw seo

* microservices log aggregration seo

* mvc seo

* mvi seo

* mvp seo

* mvvm seo

* monad seo

* monitor seo

* monostate seo

* multiton seo

* mute idiom seo

* naked objects & notification seo

* null object seo

* object mother seo

* object pool seo

* observer seo

* optimistic locking seo

* page controller seo

* page object seo

* parameter object seo

* partial response seo

* pipeline seo

* poison pill seo

* presentation model seo

* private class data seo

* producer-consumer seo

* promise seo

* property seo

* prototype seo

* proxy seo

* queue-based load leveling seo

* reactor seo

* registry seo

* repository seo

* RAII seo

* retry seo

* role object seo

* saga seo

* separated interface seo

* serialized entity seo

* serialized lob seo

* servant seo

* server session seo

* service layer seo

* service locator seo

* service to worker seo

* sharding seo

* single table inheritance seo

* singleton seo

* spatial partition seo

* special case seo

* specification seo

* state seo

* step builder seo

* strangler seo

* strategy seo

* subclass sandbox seo

* table module seo

* template method seo

* throttling seo

* tolerant reader seo

* trampoline seo

* transaction script seo

* twin seo

* type object seo

* unit of work seo

* update method seo

* value object seo

* version number seo

* virtual proxy seo

* visitor seo

* seo enhancements

* seo improvements

* SEO enhancements

* SEO improvements

* SEO additions

* SEO improvements

* more SEO improvements

* rename hexagonal + SEO improvements

* SEO improvements

* more SEO stuff

* SEO improvements

* SEO optimizations

* SEO enhancements

* enchance SEO

* improve SEO

* SEO improvements

* update headers
2024-06-08 19:54:44 +03:00
..
2024-05-23 18:40:52 +03:00
2022-09-14 23:22:24 +05:30
2024-06-08 19:54:44 +03:00

title, shortTitle, description, category, language, tag
title shortTitle description category language tag
Model-View-Presenter Pattern in Java: Enhancing UI Logic Separation for Cleaner Code Model-View-Presenter (MVP) Discover the Model-View-Presenter (MVP) pattern in Java. Learn how it separates user interface, business logic, and data interaction to enhance testability and maintainability. Architectural en
Architecture
Client-server
Decoupling
Enterprise patterns
Interface
Presentation

Also known as

  • MVP

Intent of Model-View-Presenter Design Pattern

MVP aims to separate the user interface (UI) logic from the business logic and model in a software application, enabling easier testing and maintenance.

Detailed Explanation of Model-View-Presenter Pattern with Real-World Examples

Real-world example

Consider a real-world analogy of the Model-View-Presenter (MVP) pattern using a restaurant scenario:

  • Model: This is the kitchen in a restaurant, where all the cooking and preparation of dishes happens. It's responsible for managing the food ingredients, cooking processes, and ensuring that recipes are followed correctly.

  • View: This represents the dining area and the menu presented to the customers. It displays the available dishes, takes orders, and shows the final presentation of the food. However, it doesn't decide what's cooked or how it's prepared.

  • Presenter: Acting as the waiter, the presenter takes the customer's order (input) and communicates it to the kitchen (model). The waiter then brings the prepared food (output) back to the customer in the dining area (view). The waiter ensures that what the customer sees (the menu and food presentation) aligns with what the kitchen can provide, and also updates the view based on the kitchen's capabilities (e.g., out-of-stock items).

In this analogy, the clear separation of roles allows the restaurant to operate efficiently: the kitchen focuses on food preparation, the dining area on customer interaction, and the waiter bridges the two, ensuring smooth operation and customer satisfaction.

In plain words

The Model-View-Presenter (MVP) pattern separates the user interface, business logic, and data interaction in an application, with the presenter mediating between the view and the model to facilitate clear communication and updates. Java developers use MVP to improve application structure.

Wikipedia says

Modelviewpresenter (MVP) is a derivation of the modelviewcontroller (MVC) architectural pattern, and is used mostly for building user interfaces. In MVP, the presenter assumes the functionality of the "middle-man". In MVP, all presentation logic is pushed to the presenter.

Programmatic Example of Model-View-Presenter Pattern in Java

The Model-View-Presenter (MVP) design pattern is a derivative of the well-known Model-View-Controller (MVC) pattern. It aims to separate the application's logic (Model), GUIs (View), and the way that the user's actions update the application's logic (Presenter). This separation of concerns makes the application easier to manage, extend, and test.

Let's break down the MVP pattern using the provided code:

  1. Model: The Model represents the application's logic. In our case, the FileLoader class is the Model. It's responsible for handling the file loading process.
@Getter
public class FileLoader implements Serializable {
  //...
  public void setFileName(String fileName) {
    this.fileName = fileName;
  }
  
  public boolean fileExists() {
    //...
  }

  public String loadData() {
    //...
  }
}
  1. View: The View is responsible for displaying the data provided by the Model. Here, the FileSelectorView interface and its implementation FileSelectorJFrame represent the View. They define how to display data and messages to the user.
public interface FileSelectorView {
  //...
  void setPresenter(FileSelectorPresenter presenter);
  void open();
  void close();
  void showMessage(String message);
  void displayData(String data);
  String getFileName();
}

public class FileSelectorJFrame implements FileSelectorView {
  //...
  @Override
  public void displayData(String data) {
    this.dataDisplayed = true;
  }
}
  1. Presenter: The Presenter acts as a bridge between the Model and the View. It reacts to the user's actions and updates the View accordingly. In our example, the FileSelectorPresenter class is the Presenter.
public class FileSelectorPresenter implements Serializable {
  //...
  public void setLoader(FileLoader loader) {
    this.loader = loader;
  }

  public void start() {
    view.setPresenter(this);
    view.open();
  }

  public void fileNameChanged() {
    loader.setFileName(view.getFileName());
  }

  public void confirmed() {
    //...
  }

  public void cancelled() {
    view.close();
  }
}

Finally, we wire up the Presenter, the View, and the Model in the App class:

public class App {
  public static void main(String[] args) {
    var loader = new FileLoader();
    var frame = new FileSelectorJFrame();
    var presenter = new FileSelectorPresenter(frame);
    presenter.setLoader(loader);
    presenter.start();
  }
}

In this setup, the App class creates instances of the Model, View, and Presenter. It then connects these instances, forming the MVP triad. The Presenter is given a reference to the View, and the Model is set on the Presenter. Finally, the Presenter is started, which in turn opens the View.

When to Use the Model-View-Presenter Pattern in Java

Use MVP in applications where a clear separation of concerns is needed between the presentation layer and the underlying business logic. It's particularly useful in client-server applications and enterprise-level applications.

Real-World Applications of Model-View-Presenter Pattern in Java

  • Desktop applications like those built using Java Swing or JavaFX.
  • Web applications with complex user interfaces and business logic.

Benefits and Trade-offs of Model-View-Presenter Pattern

Benefits:

  • Enhances testability of UI logic by allowing the presenter to be tested separately from the view.
  • Promotes a clean separation of concerns, making the application easier to manage and extend.
  • Facilitates easier UI updates without affecting the business logic.

Trade-offs:

  • Increases complexity with more classes and interfaces.
  • Requires careful design to avoid over-coupling between the presenter and the view.
  • Model-View-Controller (MVC): MVP is often considered a variant of MVC where the presenter takes over the controller's role in managing user input and updating the model.
  • Model-View-ViewModel (MVVM): Similar to MVP but adapted for frameworks like WPF or frameworks that support data binding, making the view update automatically when the model changes.

References and Credits