(Java) MVC – Model View Controller , Design Pattern ||Basic||

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.

Example :

1.(Model) Create Class and Class Name Model


import javax.swing.JOptionPane;
import mvc2.Model.event.NameListener;
/**
 *
 * @author root
 */
public class Model {
 private String angka1;
 private String angka2;
 private String hasil;
 private NameListener nameListener;
 
 
 public String getAngka1(){
     return angka1;
     
 }
 public void setAngka1(String angka1){
     this.angka1 = angka1;
 }
 public String getAngka2(){
     return angka2;
     
 }
 public void setAngka2(String angka2){
     this.angka2 = angka2;
 }
 public String getHasil(){
     return hasil;
 }
 public void setHasil(){
     this.hasil = hasil;
 }
 
 public NameListener getNameListener() {
		return nameListener;
}
public void setNameListener(NameListener nameListener) {
		this.nameListener = nameListener;

}        
}

<pre> 

Create Interface and name Interface NameListener


import mvc2.Model.Model;

/**
 *
 * @author root
 */
public interface  NameListener {
    public void onChange(Model model);
}

<pre> 

2.(View) Create Java Swing

public class VIew extends javax.swing.JFrame implements NameListener{
private Model model;
private Contol con;
   

    /** Creates new form VIew */
    public VIew() {
        initComponents();
        
    }
<pre> 


<pre> 

Event Button1

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        con = new Contol();
        model = new Model();
        model.setNameListener(this);
        con.setModel(model);
        
        con.infoForm(this);
        con.Jumlah(this);
        
        // TODO add your handling code here:
    }    
<pre> 

3.(Contoller)Create Class And CLass Name Contoler


import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.View;
import mvc2.Model.Model;
import mvc2.View.VIew;

/**
 *
 * @author root
 */
public class Controler {
    private Model model;
    private VIew view;
    
    
public void setModel(Model model){
    this.model = model;
}

public void infoForm(VIew view){
    String angka1 = view.getAngka1().getText();
    String angka2 = view.getAngka2().getText();
    
    if (angka1.trim().equals("") && angka2.trim().equals("")){
        JOptionPane.showMessageDialog(view,"Gak boleh kosong");
    }
    
}
public void Jumlah(VIew view){
     String angka1 = view.getAngka1().getText();
    String angka2 = view.getAngka2().getText();
    String hasil = view.getHasil().getText();
    
    hasil = angka1+angka2;
    
    view.getHasil().setText(hasil);
}  
}

<pre> 

Run Program

Published by

jarcode

hello I am jarcode

2 thoughts on “(Java) MVC – Model View Controller , Design Pattern ||Basic||”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s