Saturday, September 12, 2015

Learn MVC Architecture with Simple Java Example

Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.




Now I take simple java example to understand MVC Architecture.

Go to Netbeans and create New Java Application Project.

Create New Java Class Called "Model.java".I create Apple model inside it and create constructors and two methods to incrementApple and getvalueApple.
package mvcproject;
public class Model {
private int Apple;
public Model(){
Apple=0;
}
public Model(int Y){
this.Apple=Apple;
}
public void incApple(){
Apple++;
}
public int getApple(){
return Apple;
}
}
view raw Model.java hosted with ❤ by GitHub



So model represents an application’s data and contains the logic for accessing and manipulating that data.

Then create New Java Class Called "View.java".I create simple JFrame and implement JButton and Jlabel inside it.You can see code and Output below.
public class View {
private JFrame frame;
private JLabel label;
private JButton button;
public View(String text){
frame = new JFrame("View");
frame.getContentPane().setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
label=new JLabel(text);
frame.getContentPane().add(label,BorderLayout.CENTER);
button=new JButton("Button");
frame.getContentPane().add(button,BorderLayout.SOUTH);
}
public JButton getButton(){
return button;
}
public void setText(String text){
label.setText(text);
}
}
view raw View.java hosted with ❤ by GitHub



so the presentation semantics are encapsulated within the view.

Then Controller is most important part.I want to do when i press this button in View Apple value should increment by 1 and display in label.so that logic should implement inside controller.It connect Model and View.
package mvcproject;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Controller {
private Model model;
private View view;
private ActionListener actionListener;
public Controller(Model model,View view){
this.model=model;
this.view=view;
}
public void control(){
actionListener =new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
linkBandL();
}
};
view.getButton().addActionListener(actionListener);
}
public void linkBandL(){
model.incApple();
view.setText(Integer.toString(model.getApple()));
}
}
view raw Controller.java hosted with ❤ by GitHub



Create ActionListner and with in that, create method LinkBandL( ) to increment apple value and display in label.


so The controller is responsible for intercepting and translating user input into actions to be performed by the model.

then Create Main class to Run this Application
package mvcproject;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Model model=new Model(0);
View view =new View("label");
Controller controller=new Controller(model, view);
controller.control();
}
});
}
}
view raw Main.java hosted with ❤ by GitHub


final Output: 




 Click Button Apple value increment by 1 and display in label. 


4 comments: