Home Java Java-tips GUI Structure Big Blob Structure

Ask Questions?

View Latest Questions

Advertisement


 
 

Big Blob Structure
Posted on: July 26, 2006 at 12:00 AM
Know the Big Blob Structrue

Java Notes

Big Blob Structure

A common style of programming is to put all processing  in the GUI.

This works Ok as long at the "model", the logic, is so small that it isn't worth putting into a separate class.

However, mixing model with presentation usually makes the program hard to read, and the inevitable growth of the program leads to a mess.

This fails the simple Interface Independence test.

Could the model easily work with a command line or web interface? No.

It also fails the Model Independence test.

Could we easily change the model, eg, to BigDecimal? No.Exap

 

When you run the example code it will show you nice SWING GUI. It will give you a button to perform the calculation.

 

When use clicks on the "Multiply" button the application will perform calculation and display the total.

 

The screen shot of the application given below will help you in understanding it in detail.

 

Next - Presentation-Model

Sample window

You'll see a lot of programs that look like the following. They jam the main program, the GUI, and the model into one file. Many text books show this style, not because it is what you should use, but because it's easier to show it in a text book: everything is in one file.

Don't use this structure except for the absolutely simplest programs. It's hard to read, maintain, and enhance.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
// structure/BigBlob.java -- Everything in one file.
// Fred Swartz - December 2004

//     A common style of programming is to put all processing
//     in the GUI.  This works Ok as long at the "model", the
//     logic, is so small that it isn't worth putting into
//     a separate class.  
//
//     However, mixing model with presentation usually makes the program hard
//      to read, and the inevitable growth of the program leads to a mess.
//
//     This fails the simple Interface Independence test.
//       Could the model easily work with a command line or web interface? No.
//
//     It also fails the Model Independence test.
//       Could we easily change the model, eg, to BigDecimal?  No.


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.math.BigInteger;

public class BigBlob {    
    public static void main(String[] args) {
        JFrame window = new BigBlobGUI();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("Simple Calc");
        window.setVisible(true);
    }
}


class BigBlobGUI extends JFrame {
    //... Constants
    private static final String INITIAL_VALUE = "1";
    
    //... Components
    private JTextField m_totalTf     = new JTextField(10);
    private JTextField m_userInputTf = new JTextField(10);
    private JButton    m_multiplyBtn = new JButton("Multiply");
    private JButton    m_clearBtn    = new JButton("Clear");
    
    private BigInteger m_total;  // The total current value state.
    
    /** Constructor */
    BigBlobGUI() {
        //... Initialize components and model
        m_total = new BigInteger(INITIAL_VALUE);
        m_totalTf.setText(INITIAL_VALUE);
        m_totalTf.setEditable(false);
        
        //... Layout the components.        
        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());
        content.add(new JLabel("Input"));
        content.add(m_userInputTf);
        content.add(m_multiplyBtn);
        content.add(new JLabel("Total"));
        content.add(m_totalTf);
        content.add(m_clearBtn);
        
        //... finalize layout
        this.setContentPane(content);
        this.pack();
        
        //... Listener to do multiplication
        m_multiplyBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    m_total = m_total.multiply(new BigInteger(m_userInputTf.getText()));
                    m_totalTf.setText(m_total.toString());
                } catch (NumberFormatException nex) {
                    JOptionPane.showMessageDialog(BigBlobGUI.this, "Bad Number");
                }
            }
        });
        
        //... Listener to clear.
        m_clearBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                m_total = new BigInteger(INITIAL_VALUE);
                m_totalTf.setText(INITIAL_VALUE);
            }
        });
    }
}
Copyleft 2004 Fred Swartz MIT License
Advertisement


DMCA.com