Sie sind auf Seite 1von 4

Model-View-Controller (MVC) pattern simple example

About this architectural pattern is so much articles have been written, so I don't describe it in details. Follow MVC in Wikipedia link to read about theory of MVC. I just want to represent a very simple implementation of MVC pattern using Java and Swing library. The MVC block diagram is shown on Fig.1.

Fig.1

Model
Our simple model just has a one private variable "x". This variable is initialized to zero by default constructor Model(), or to custom value by constructor Model(int x) . Methods incX() and getX() give access to "x". Method incX() performs some operations: it increments "x", while method getX just return "x". Note that Model don't know anything about View and Controller.
package mvc.models; public class Model { private int x; public Model(){ x = 0; } public Model(int x){ this.x = x; } public void incX(){ x++; } public int getX(){ return x; } }

View
The View class uses Swing library to create window and place label and button on it. Public method setText(String text) allows us to set text label, method getButton() returns button reference. Note that View don't know anything about Model and Controller.
package mvc.views; import javax.swing.*; import java.awt.BorderLayout; 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); }

CONTROLLER
The Controller class keeps references to Model and View classes. So Controller sees Model and View interfaces. Controller provides interaction between Model and View. Method control() gets reference to view's button and assign actionListener to it. In actionListener's method actionPerformed() method linkBtnAndLabel() is called. In linkBtnAndLabel() model's variable "x" increments and than "x" sends to view's label to display changes.
package mvc.controllers;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import mvc.models.*; import mvc.views.*; 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 contol(){ actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { linkBtnAndLabel(); } }; view.getButton().addActionListener(actionListener); } private void linkBtnAndLabel(){ model.incX(); view.setText(Integer.toString(model.getX())); } }

MAIN CLASS
The Main class creates objects of Model, View and Controller classes, initializes Controller by Model and View and call Controller's method control(). The result by compiling and run program is showed on Fig.2.
package mvc; import javax.swing.SwingUtilities; import mvc.models.*; import mvc.views.*; import mvc.controllers.*; 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("-"); Controller controller = new Controller(model,view); controller.contol(); } }); } }

WHY MVC? Perhaps this example is too simple to understand why MVC is needed. But the power of MVC arises in complicated and large programs. MVC helps to divide functionality between different objects, so program becomes more clear and understandable. The pattern allows to modify code (realization), that is encapsulated inside one of these classes without intrusion inside other classes code(of course you have to keep the same interfaces).

Das könnte Ihnen auch gefallen