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
- MODEL class ---MVC_Model
- VIEW class --- MVC_View
- CONTROLLER class ---MVC_Controller
- 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
/*
CONTROLLER class:
Model class :
MainMethod:
Compile:
execute main Methos class -- GlobalMVC.java
output:
/*
*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:
Excellent example! Thanks!
ReplyDeleteconstructor of MVC_View takes object from MVC_Model and never uses, why???
ReplyDeleteGreat and simple example :)
ReplyDeleteI just learned the MVC model and this example confuses me for two reasons:
ReplyDelete1) 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.
This comment has been removed by a blog administrator.
ReplyDelete
ReplyDeleteGood job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work
Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery
smm panel
ReplyDeleteSmm Panel
iş ilanları
instagram takipçi satın al
HIRDAVATÇI
HTTPS://WWW.BEYAZESYATEKNİKSERVİSİ.COM.TR/
Servis
JETON HİLESİ
maltepe lg klima servisi
ReplyDeletemaltepe alarko carrier klima servisi
kadıköy alarko carrier klima servisi
maltepe daikin klima servisi
kartal toshiba klima servisi
üsküdar alarko carrier klima servisi
tuzla alarko carrier klima servisi
ataşehir beko klima servisi
kadıköy lg klima servisi