MVC Adding 2 Numbers Program

MVC Architecture simple JAVA Program(MVC Adding 2 Numbers Program )


M-Model
V-View
C-Controller

Introduction: 
         MVC architecture is uses in Programming languages when that program contains "user Interface components "(like buttons ,text fields, combo box).
         
Tutorial :
        I am taking a simple example "adding two number" to demonstrate MVC Architecture.i'm going to divide the program into 4 parts  

  1. MODEL class ---MVC_Model
  2. VIEW class --- MVC_View
  3. CONTROLLER class ---MVC_Controller
  4. Intialize all above three classes in Main Method class ---GlobelMVC

View class : View is only contains the user interface part (JButton, JTextField..etc) and it wouldn't  having the controllers means setting a listener for a button ( button.addActionListener(this) ). the use of the separate class for User Interface is we can modify the User Interface in Future with out changing the other functionality  
/*
*program: Adding Two Numbers Using MVC 
 *author: padmanLabs.blogspot.in
 *date: 18-Aug-2012
 */

import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MVC_View extends JFrame
{
 Container con;
 JButton addButton;
 JTextField textField1,textField2,textFieldresult;
 JLabel first,second,result;
 
 public MVC_View(MVC_Model model)
 {
  con=getContentPane();
  setTitle("MVC ADD");
  setLayout(new FlowLayout());
  setSize(220,200);
  first=new JLabel("first variable");
  textField1=new JTextField("",10);
  second=new JLabel("second variable");
  textField2=new JTextField("",8);
  result=new JLabel("result");
  textFieldresult=new JTextField("",13);
  addButton=new JButton("add");
  con.add(first);
  con.add(textField1);
  con.add(second);
  con.add(textField2);
  con.add(result);
  con.add(textFieldresult);
  con.add(addButton);
  setVisible(true);
 }
}



CONTROLLER class:

/*
 *program: Adding Two Numbers Using MVC 
 *author: padmanLabs.blogspot.in
 *date: 18-Aug-2012
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;

public class MVC_Controller implements ActionListener
{
 MVC_Model model;
 MVC_View view;
 public MVC_Controller(MVC_View v,MVC_Model m)
 {
  model=m;
  view=v;
  actionListenerMeth(this);//add action listener to the Button
 } 
 
 @Override
 public void actionPerformed(ActionEvent arg0) {
  try
  {
   //retrieve the input from View
   String a=view.textField1.getText();
   String b=view.textField2.getText();
   
   //call add method
   int result=model.add(a,b);
   
   //display result in View(UI)
   view.textFieldresult.setText(""+result);
  }
  catch(Exception ee)
  {
   ee.printStackTrace();
  }  
 }
 
 public void actionListenerMeth(ActionListener ae)
 {
  view.addButton.addActionListener(ae);
 }
 

}



Model class :

/*
 *program: Adding Two Numbers Using MVC 
 *author: padmanLabs.blogspot.in
 *date: 18-Aug-2012
 */

public class MVC_Model
{
 //implement the logic of the program
 public int add(String x,String y)
 {
  int a=Integer.parseInt(x);
  int b=Integer.parseInt(y);
  int result=a+b;
  return (result);
 }
 
}



MainMethod:
/*
 *program: Adding Two Numbers Using MVC 
 *author: padmanLabs.blogspot.in
 *date: 18-Aug-2012
 */
class GlobalMVC
{
 public static void main(String args[])
 {
  MVC_Model m=new MVC_Model();
  MVC_View v= new MVC_View(m); 
  new MVC_Controller(v,m);
  
 }
}

Compile:

execute main Methos class -- GlobalMVC.java

output:










Comments

  1. constructor of MVC_View takes object from MVC_Model and never uses, why???

    ReplyDelete
  2. I just learned the MVC model and this example confuses me for two reasons:
    1) I was taught the model and view should not interact and this is violated by the following line:
    MVC_View v= new MVC_View(m);
    2) I was taught that the logic of a program should be in the controller, but in this program the logic is in the model
    I'd appreciate if the author could answer.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

Relative Layout in android UI Design

Linear Layout (Horizontal & Vertical) in Android