Model View Controller

Model View Controller (MVC) is a design pattern. Design patterns are programming constructs: simple structures that give some hint on how to solve some specific problems. The term, design pattern was coined and formally described in the book, Design Patterns, Elements of Reusable Object-Oriented Software, written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. The book defines a design pattern as a structure with a name, a problem, and a solution. The name describes the pattern and gives the vocabulary for the developer community to use when talking about these patterns. It is important that different developers use the same language terminology in order to understand each other. The problem describes the situation, that is, the design problem where the pattern can be applied. The solution describes classes and objects and the relations between them, which contribute to a good design.

One of them is MVC, which is suitable for programming web applications but generally for any application that has a user interface. In our case, we do not have a classical user interface because the client is also a program; still, MVC can be and is a good choice to use.

The MVC pattern, as the name also indicates, has three parts: a model, a view, and a controller. This separation follows the single responsibility principle, requiring one part for each distinct responsibility. The controller is responsible for handling the inputs of the system, and it decides what model and view to use. It controls the execution but usually does not do any business logic. The model does the business logic and contains the data. View converts the model data to a representation that is consumable by the client.

MVC is a well-known and widely used design pattern, and it is directly supported by Spring in such a way that when you create a web application, you program the controller built into the framework, using annotations; thus, you essentially configure it. You can program the view, but it is more likely that you will use one that is built into the framework. You will want to send data to the client in XML, JSON, or HTML. If you are very exotic, you may want to send YAML, but generally, that is it. You do not want to implement a new format that needs to be programmed on the server and, since this is new, also on the client.

We create the model, and this time, we also program it. After all, that is the business logic. Frameworks can do many things for us, mainly the things that are the same for most of the applications but for the business logic. Business logic is the code that distinguishes our code from other programs. That is what we have to program.

On the other hand, that is what we like to do. Focus on the business code and avoid all boilerplate provided by the framework.

Now that we know what JSON, REST, and the general Model View Controller design pattern are, let's look at how these are managed by Spring and how we can put these technologies into action.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.157.197