The Command Pattern


The Model-View-Controller Pattern

The Model-View-Controller (MVC) pattern is used to separate the visual representation of an application object from the application object itself. The MVC pattern consists of three primary components: 1) the model, which can consist of one or more classes working together to realize the functionality of a particular application, 2) the view, which can consist of one or more classes working together to implement the visual representation of the model, (For example, the view could provide a user interface on the model’s behalf.), and 3) the controller, which can consist of one or more classes working together to coordinate messaging between the model and the view.

The approach I like to take when implementing the MVC pattern is to completely isolate the model from the view. In other words, the model should know nothing about the view, and the view should know nothing about the model. The controller functions as a liaison between the model and view components, coordinating intercomponent messaging between the two. This state of affairs is illustrated in figure 25-2.

image from book
Figure 25-2: Model-View-Controller Pattern

The model is the most independent component in the MVC relationship. It should provide an interface and make no assumptions about the existence of the view or the controller. The view, especially if it’s a GUI, may or may not need to know something about the controller. Assume for a moment that the controller implements the ActionListener interface with the intention of handling view component actions with the actionPerformed() method. The view would need a reference to an ActionListener but not to a controller per se.

Examples 25.9 through 25.11 gives the code for a simple implementation of the MVC pattern. I call this application Motivational Messages. When the application runs it presents a simple user interface consisting of a Canvas and a JButton. These components are contained in a class named View. When the button is clicked the Controller object handles the click and calls a method on the Model object to get the next message and on the View object to set the message. This is an example of interobject message coordination provided by a controller object. The Controller class in this case also serves as the application. The results of running this program are shown in figure 25-3.

Example 25.9: Model.java

image from book
 1     public class Model { 2 3        private int i = 0; 4        private String[] messages = { "Eat right, get plenty of rest, and exercise daily.", 5                                       "Make love not war.", 6                                       "Carpe Diem!", 7                                       "Eat your vegatables.", 8                                       "Brush and floss your teeth three times daily.", 9                                       "A penny saved is a penny earned.", 10                                      "What you do today prepares you for tomorrow.", 11                                      "All work and no play makes Jack a dull boy.", }; 12 13       public String getMessage(){ 14         if(i++ == (messages.length-1)) i = 0; 15         return messages[i]; 16       } 17    } // end model class
image from book

Example 25.10: View.java

image from book
 1     import javax.swing.*; 2     import java.awt.event.*; 3     import java.awt.*; 4 5     public class View extends JFrame { 6 7        private Canvas canvas = null; 8        private JButton button = null; 9        private String message = ""; 10 11       public View(ActionListener al){ 12         super("Motivational Messages"); 13         canvas = new Canvas(){ 14                    public void paint(Graphics g){ 15                        g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); 16                        g.setFont(new Font("Roman", Font.BOLD, 14)); 17                        g.drawString(message, 10, 20); 18                    } // end paint() method 19                }; // end Canvas inner class definition 20         button = new JButton("Next Message"); 21         button.addActionListener(al); 22         this.getContentPane().setLayout(new BorderLayout()); 23         this.getContentPane().add(canvas); 24         this.getContentPane().add(BorderLayout.SOUTH, button); 25         this.setSize(400, 100); 26         this.show(); 27       } 28 29       public void setMessage(String message){ 30             this.message = message; 31             canvas.repaint(); 32       } 33 34    } // end View clas definition 
image from book

Example 25.11: Controller.java

image from book
 1     import java.awt.event.*; 2 3     public class Controller implements ActionListener { 4 5        private Model its_model = null; 6        private View its_view = null; 7 8        public Controller(){ 9          its_model = new Model(); 10         its_view = new View(this); 11       } 12 13       public void actionPerformed(ActionEvent ae){ 14         if(ae.getActionCommand().equals("Next Message")){ 15           its_view.setMessage(its_model.getMessage()); 16         } 17       } 18 19       public static void main(String[] args){ 20          new Controller(); 21       } 22    } // end Controller class definition 
image from book

image from book
Figure 25-3: Results of Running Example 25.11 and Clicking the “Next Message” Button Several Times

Potential Issues With One ActionListener

The MVC pattern does a nice job of helping you separate the concerns of an application from the concerns of its user interface. However, having the controller serve as the sole ActionListener for a large application will cause its ActionPerformed() method to grow quite large and ungainly due to the many if-else statements required to determine which GUI component was clicked. This issue can be effectively addressed with the help of the Command pattern which is discussed in the next section.

Quick Review

The Model-View-Controller (MVC) pattern is used to separate the visual representation of an application object from the application object itself. The MVC pattern consists of three primary components: 1) the model, which can consist of one or more classes working together to realize the functionality of a particular application, 2) the view, which can consist of one or more classes working together to implement the visual representation of the model, and 3) the controller, which can consist of one or more classes working together to coordinate messaging between the model and the view.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net