Thursday, 24 May 2018

MVC Design Pattern

MVC stands for Model View Controller. Web frame works like spring, struts2 are developed on top of mvc design patter.

In MVC, application is divided into 3 components.
         M - Model: Represents the data, a model can be a java object with properties, those can be accessed via getters and setters
         V - View: Represents the user interface (html, jsp)
         C - Controller: Manages the application flow






User requests are received by controller, controller manages the model object, mix the model with view, send the response back to the user.

For example, I am going to build an employee application, where user can request an employee details in specific format, controller receives the request, give the model object to view. View uses the model object to print the information.

Employee.java
package com.sample.myApp.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

}

EmployeeView.java
package com.sample.myApp.view;

import com.sample.myApp.model.Employee;

public class EmployeeView {
 private static final char DEFAULT_STYLE = '*';
 private char style = DEFAULT_STYLE;

 public char getStyle() {
  return style;
 }

 public void setStyle(char style) {
  this.style = style;
 }

 public static char getDefaultStyle() {
  return DEFAULT_STYLE;
 }

 public static enum ViewType {
  STARS('*'), DOTS('.'), DASHES('-');

  private char style;

  ViewType(char style) {
   this.style = style;
  }

  public char getStyle() {
   return style;
  }

  public void setStyle(char style) {
   this.style = style;
  }

 }

 public EmployeeView(ViewType viewType) {
  this.style = viewType.getStyle();
 }

 public void printEmployeeDetails(Employee employee) {
  printStyle(this.style);
  printEmployeeInfo(employee);
  printStyle(this.style);
 }

 private static void printEmployeeInfo(Employee employee) {
  System.out.printf("Id : %d\n", employee.getId());
  System.out.printf("First Name : %s\n", employee.getFirstName());
  System.out.printf("Last Name : %s\n", employee.getLastName());
 }

 private static void printStyle(char style) {
  for (int i = 0; i < 40; i++) {
   System.out.print(style);
  }
  System.out.println();
 }
}


EmployeeController.java
package com.sample.myApp.controller;

import com.sample.myApp.model.Employee;
import com.sample.myApp.view.EmployeeView;

public class EmployeeController {

 private Employee model;
 private EmployeeView view;

 public EmployeeController(Employee model, EmployeeView view) {
  this.model = model;
  this.view = view;
 }

 public void updateView() {
  view.printEmployeeDetails(model);
 }

 public Employee getModel() {
  return model;
 }

 public EmployeeView getView() {
  return view;
 }

}

Test.java
package com.sample.myApp.test;

import com.sample.myApp.controller.EmployeeController;
import com.sample.myApp.model.Employee;
import com.sample.myApp.view.EmployeeView;

public class Test {
 public static void main(String args[]) {
  Employee model = new Employee();
  model.setId(123);
  model.setFirstName("Siva");
  model.setLastName("Ponnam");

  EmployeeView view = new EmployeeView(EmployeeView.ViewType.STARS);

  EmployeeController controller = new EmployeeController(model, view);
  controller.updateView();

  System.out.println("\nSetting the printing view to dashes\n");

  controller.getView().setStyle(EmployeeView.ViewType.DASHES.getStyle());
  controller.updateView();

 }
}

When you ran Test.java, you can able to see below messages in console.
****************************************
Id : 123
First Name : Siva
Last Name : Ponnam
****************************************

Setting the printing view to dashes

----------------------------------------
Id : 123
First Name : Siva
Last Name : Ponnam
----------------------------------------


You may like

No comments:

Post a Comment