Design Patterns – Domain Model vs Presentation Model

design-patternsdomain-modelpresentation-model

I have been building an app for my company i join for the past > 1 year, we're using

  • Java
  • Spring
  • Hibernate
  • Javascript
  • jQuery
  • Kendo UI

And for the structure i can say something like this from Backend to Frontend :

Backend (pojo > dao > bo > service > web service) > UI ( model, view
model, html/view)

Well i think i know how to code them all, but then earlier today my senior asked me this and i can't say a word jeez i need to know more of the concept. He asked me this

What Presentation Model is, how it differs from Domain Model and its general function ? i'm looking for hours combining article to article but it got me more confused. Can anyone explain with "easy explanation about this"? and also are pojo = domain model in java, then view model = presentation model in MVVM ?

Best Answer

Domain Model

The domain model is a collection of classes without any presentation specific code. Just think of a pure list of customers. Also, one may add the buissnes logic into the domain model: code to modificate/change the model.


Presentation model by Martin Fowler

As Fowler describes:

The essence of a Presentation Model is of a fully self-contained class that represents all the data and behavior of the UI window, but without any of the controls used to render that UI on the screen. A view then simply projects the state of the presentation model onto the glass.

As you read, the domain model and the presentation model are completely different. If the domain is out list of customers we need a nice GUI to display this list. Here comes the presentation model and defines classes which contains the state of all elements in the GUI.


POJO (Plain-old-Java-Object)

A POJO is a class in Java which only extends Object. nothing more.

class Foo { // this is a POJO
}

class Foo implements Bar { // this is also a POJO
}

class Foo extends Baz { // this is *not* a POJO
}

MVVM (Model-View-ViewModel)

The presentation model is slightly different from MVVM. The term model is the domain model. The view contains all GUI elements. The ViewModel only contains the correct representation of the data and includes a data binding. This pattern requires the use of any kind of binding whereas the presentation model does not. (Fowler said to have data binding but does not say in which class)

As we can read in this article MVVM comes after the presentation model and I think we should use MVPVM. in MVVM the ViewModel contains logic to present and logic to create the representation of the data which violates the Single-Responsibility-Principle. If we create a presenter and put the presentation logic into that class the violation is gone. So I think MVPVM is compatible with the SOLID-Design.