Java Model View Controller (MVC) Design Pattern


 

Java Model View Controller (MVC) Design Pattern

In This tutorial you will learn about Model View Control design pattern of java

In This tutorial you will learn about Model View Control design pattern of java

Java MVC ( Model View Controller ) Design Pattern

Model View controller is a classical design pattern used in applications who needs a clean separation between their business logic and view who represents data. MVC design pattern isolates the application logic from the user interface and permitted the individual development, testing and maintenance for each components. This design pattern is divided into three parts.

1. Model- This component manages the information and notify the observers when the information changes. It represents the data when on  which the application operates. The model provides the persistent storage of data, which manipulated by the controller.

2. View- The view displays the data , and also takes input from user. It renders the model data into a form to display to the user. there can be several view associated with a single model. It is actually representation of model data.

3. Controller- The controller handles all request coming from the view or user interface. The data flow to whole application is controlled by controller. It forwarded the request to the appropriate handeler. Only the controller is responsible for accessing model and rendering it into various UIs.

Model View Controller Diagram

A Simple Example of Model view Controller design pattern is given below

MainClaz.java

package roseindia.net;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClaz {
	public static void main(String[] arguments) {

		// Creating Contact model
		StudentModel studentModel = new StudentModel();
		// Creating Contact view
		StudentView view = new StudentView(studentModel);
		studentModel.addContactView(view);
		createGui(view, "Enter Student deatail");
		// Contact View
		StudentDisplayView displayView = new StudentDisplayView();
		studentModel.addContactView(displayView);
		createGui(displayView, "Student Details");
	}

	private static void createGui(JPanel contents, String windowName) {
		JFrame frame = new JFrame(windowName);
		frame.getContentPane().add(contents);
		frame.addWindowListener(new WindowCloseManager());
		frame.pack();
		frame.setVisible(true);
	}

	private static class WindowCloseManager extends WindowAdapter {
		public void windowClosing(WindowEvent evt) {
			System.exit(0);
		}
	}
}

StudentBean.java

package roseindia.net;

public class StudentBean {
	private String firstName;
	private String lastName;
	private String course;
	private String address;

	private StudentRefInterface view;

	public StudentBean(StudentRefInterface v) {

		firstName = "";
		lastName = "";
		course = "";
		address = "";

		view = v;
	}

	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;
	}

	public String getCourse() {
		return course;
	}

	public void setCourse(String course) {
		this.course = course;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public StudentRefInterface getView() {
		return view;
	}

	public void setView(StudentRefInterface view) {
		this.view = view;
	}
}

StudentController.java

package roseindia.net;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StudentController implements ActionListener {
	private StudentModel model;
	private StudentView view;

	public StudentController(StudentModel m, StudentView v) {
		this.model = m;
		this.view = v;
	}

	public void actionPerformed(ActionEvent evt) {
		Object source = evt.getSource();
		if (source == view.getUpdateRef()) {
			updateModel();
		}
	}

	private void updateModel() {
		String firstName = null;
		String lastName = null;

		firstName = view.getFirstName();
		lastName = view.getLastName();
		model.updateModel(firstName, lastName, view.getCourse(), view
				.getAddress());
	}

}

StudentDisplayView.java

package roseindia.net;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class StudentDisplayView extends JPanel implements StudentRefInterface {
	private JTextArea studentDetail;

	public StudentDisplayView() {
		createGui();
	}

	public void createGui() {
		setLayout(new BorderLayout());
		studentDetail = new JTextArea(10, 40);
		studentDetail.setEditable(false);
		JScrollPane scrollDisplay = new JScrollPane(studentDetail);
		this.add(scrollDisplay, BorderLayout.CENTER);
	}

	public void refresh(String newFirstName, String newLastName,
			String newCourse, String newAddress) {
		studentDetail.setText("\n\tStudent Details\n\t" + "\tName: "
				+ newFirstName + "\n\t\tLast Name: " + newLastName + "\n"
				+ "\t\tCourse: " + newCourse + "\n" + "\t\tAddress: "
				+ newAddress);
	}
}

StudentModel.java

package roseindia.net;

import java.util.ArrayList;
import java.util.Iterator;

public class StudentModel {
	private String firstName;
	private String lastName;
	private String course;
	private String address;
	private ArrayList studentViews = new ArrayList();

	public StudentModel() {
		this(null);
	}

	public StudentModel(StudentRefInterface view) {
		firstName = "";
		lastName = "";
		course = "";
		address = "";
		if (view != null) {
			studentViews.add(view);
		}
	}

	public void addContactView(StudentRefInterface view) {
		if (!studentViews.contains(view)) {
			studentViews.add(view);
		}
	}

	public void removeContactView(StudentRefInterface view) {
		studentViews.remove(view);
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getTitle() {
		return course;
	}

	public String getOrganization() {
		return address;
	}

	public void setFirstName(String newFirstName) {
		firstName = newFirstName;
	}

	public void setLastName(String newLastName) {
		lastName = newLastName;
	}

	public void setCourse(String newCourse) {
		course = newCourse;
	}

	public void setAddress(String newAddress) {
		address = newAddress;
	}

	public void updateModel(String newFirstName, String newLastName,
			String newTitle, String newOrganization) {
		if (!isEmptyString(newFirstName)) {
			setFirstName(newFirstName);
		}
		if (!isEmptyString(newLastName)) {
			setLastName(newLastName);
		}
		if (!isEmptyString(newTitle)) {
			setCourse(newTitle);
		}
		if (!isEmptyString(newOrganization)) {
			setAddress(newOrganization);
		}
		updateView();
	}

	private boolean isEmptyString(String input) {
		return ((input == null) || input.equals(""));
	}

	private void updateView() {
		Iterator notifyViews = studentViews.iterator();
		while (notifyViews.hasNext()) {
			((StudentRefInterface) notifyViews.next()).refresh(firstName,
					lastName, course, address);
		}
	}
}

StudentRefInterface.java

package roseindia.net;

public interface StudentRefInterface {
	public void refresh(String firstName, String lastName, String course,
			String address);
}

StudentView.java

package roseindia.net;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class StudentView extends JPanel implements StudentRefInterface {
	private static final long serialVersionUID = 1L;
	private static final String SHOW_BUTTON = "Show";
	private static final String EXIT_BUTTON = "Exit";
	private static final String STUDENT_FIRST_NAME = "First Name  ";
	private static final String STUDENT_LAST_NAME = "Last Name  ";
	private static final String STUDENT_COURSE = "Course  ";
	private static final String STUDENT_ADDRESS = "Address  ";
	private static final int FNAME_COL_WIDTH = 30;
	private static final int LNAME_COL_WIDTH = 50;
	private static final int COURSE_COL_WIDTH = 35;
	private static final int ADDRESS_COL_WIDTH = 50;
	private StudentController controller;
	private JLabel firstNameLabel, lastNameLabel, courseLabel, addressLabel;
	private JTextField firstName, lastName, course, address;
	private JButton display, exit;

	public StudentView(StudentModel model) {
		controller = new StudentController(model, this);
		createGui();
	}

	public StudentView(StudentModel model, StudentController newController) {
		controller = newController;
		createGui();
	}

	public void createGui() {
		display = new JButton(SHOW_BUTTON);
		exit = new JButton(EXIT_BUTTON);

		firstNameLabel = new JLabel(STUDENT_FIRST_NAME);
		lastNameLabel = new JLabel(STUDENT_LAST_NAME);
		courseLabel = new JLabel(STUDENT_COURSE);
		addressLabel = new JLabel(STUDENT_ADDRESS);

		firstName = new JTextField(FNAME_COL_WIDTH);
		lastName = new JTextField(LNAME_COL_WIDTH);
		course = new JTextField(COURSE_COL_WIDTH);
		address = new JTextField(ADDRESS_COL_WIDTH);

		JPanel editPanel = new JPanel();
		editPanel.setLayout(new BoxLayout(editPanel, BoxLayout.X_AXIS));

		JPanel labelPanel = new JPanel();
		labelPanel.setLayout(new GridLayout(0, 1));

		labelPanel.add(firstNameLabel);
		labelPanel.add(lastNameLabel);
		labelPanel.add(courseLabel);
		labelPanel.add(addressLabel);

		editPanel.add(labelPanel);

		JPanel fieldPanel = new JPanel();
		fieldPanel.setLayout(new GridLayout(0, 1));

		fieldPanel.add(firstName);
		fieldPanel.add(lastName);
		fieldPanel.add(course);
		fieldPanel.add(address);

		editPanel.add(fieldPanel);

		JPanel controlPanel = new JPanel();
		controlPanel.add(display);
		controlPanel.add(exit);
		display.addActionListener(controller);
		exit.addActionListener(new ExitHandler());

		setLayout(new BorderLayout());
		add(editPanel, BorderLayout.CENTER);
		add(controlPanel, BorderLayout.SOUTH);
	}

	public Object getUpdateRef() {
		return display;
	}

	public String getFirstName() {
		return firstName.getText();
	}

	public String getLastName() {
		return lastName.getText();
	}

	public String getCourse() {
		return course.getText();
	}

	public String getAddress() {
		return address.getText();
	}

	public void refresh(String newFirstName, String newLastName,
			String newTitle, String newOrganization) {
		firstName.setText(newFirstName);
		lastName.setText(newLastName);
		course.setText(newTitle);
		address.setText(newOrganization);
	}

	private class ExitHandler implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			System.exit(0);
		}
	}

}

When you run this application it will display message as shown below:


           

Download this example code

Ads