C SC 225 Lecture 12: OO Frameworks

[ previous | schedule | next ]

Framework Overview

What is an OO Framework?

Inversion of Control

Inversion of control and the Template Method design pattern

How is a framework different than a library?

How are frameworks different than design patterns?

What qualities should a framework have?

It may help to think of an example, such as Java’s Collection classes.

Jia textbook recites these qualities, with credit, from Booch’s classic 1994 OOAD book. I use the term “client” to refer to a developer using the framework.

Java Collections Framework as example framework

Overview Collection Interfaces General-Purpose Implementations Other implementations: Abstract Implementations Algorithms Infrastructure Array utilities

Applets as example application framework

These excerpts from java.applet.Applet source code show the defaults:

public class Applet extends Panel {
    /**
     * Called by the browser or applet viewer to inform
     * this applet that it has been loaded into the system. 
     */
    public void init() {
    }
	
    /**
     * Called by the browser or applet viewer to inform
     * this applet that it should start its execution.
     */
    public void start() {
    }
	
    /**
     * Called by the browser or applet viewer to inform
     * this applet that it should stop its execution.
     */
    public void stop() {
    }
	
    /**
     * Called by the browser or applet viewer to inform
     * this applet that it is being reclaimed and that it should destroy
     * any resources that it has allocated.
     */
    public void destroy() {
    }
}

MARS Tools feature as example framework

Overview MARS Tool

This excerpt from mars.tools.AbstractToolAndApplication source code shows the template method and default definitions of operations. One is abstract and must be defined by subclass. Some details are omitted to make the code shorter and easier to follow.

   public abstract class AbstractMarsToolAndApplication extends JFrame implements MarsTool, Observer {

      /**
       *  Template method invoked when MARS user selects this tool from the Tools menu.
       */
      public void action() {
         dialog = new JDialog(Globals.getGui(), this.title);		
         initializePreGUI();
         JPanel contentPane = new JPanel(new BorderLayout(5,5));
         contentPane.setBorder(emptyBorder);
         contentPane.setOpaque(true);
         contentPane.add(buildHeadingArea(), BorderLayout.NORTH);
         contentPane.add(buildMainDisplayArea(),BorderLayout.CENTER);
         contentPane.add(buildButtonAreaMarsTool(), BorderLayout.SOUTH);
         initializePostGUI();
         dialog.setContentPane(contentPane);
         dialog.pack();      
         dialog.setVisible(true);
      }

      /**
       *  Define this method to initialize any data structures needed for the application
       *  whose values will be needed to determine the initial state of GUI components. 
       */
      protected void initializePreGUI() {
      }

      /**
       *  Define this method to initialize data structures needed for the application
       *  whose values may depend on the initial state of GUI components. 
       */
      protected void initializePostGUI() {
      }

      /**
       *  Constructs GUI header as label with default positioning and font.  May be overridden.
       */
      protected JComponent buildHeadingArea() {
         headingLabel = new JLabel();
         Box headingPanel = Box.createHorizontalBox();
         headingPanel.add(Box.createHorizontalGlue());
         headingPanel.add(headingLabel);
         headingPanel.add(Box.createHorizontalGlue());
         headingLabel.setText(heading);
         headingLabel.setHorizontalTextPosition(JLabel.CENTER);
         headingLabel.setFont(new Font(headingLabel.getFont().getFontName(),Font.PLAIN,18));
         return headingPanel;
      }	

      /**
       *  Abstract method that must be instantiated by subclass to build the main display area
       *  of the GUI.  It will be placed in the CENTER area of a BorderLayout.  The title
       *  is in the NORTH area, and the controls are in the SOUTH area.
       */
       protected abstract JComponent buildMainDisplayArea();

      /**
       *  The MarsTool default set of controls has one row of 3 buttons.  It includes a dual-purpose button to 
       *  attach or detach simulator to MIPS memory, a button to reset the tool, and one to close the tool.
       */
      protected JComponent buildButtonAreaMarsTool() {
         Box buttonArea = Box.createHorizontalBox();
         connectButton = new ConnectButton();
         connectButton.setToolTipText("Control whether tool will respond to running MIPS program");
         connectButton.addActionListener( /* details omitted */ ); 	
         JButton resetButton = new JButton("Reset");
         resetButton.setToolTipText("Reset all counters and other structures");
         resetButton.addActionListener( /* details omitted */ );	
         JButton closeButton = new JButton("Close");
         closeButton.setToolTipText("Close (exit) this tool");
         closeButton.addActionListener( /* details omitted */ );     
         buttonArea.add(connectButton);
         buttonArea.add(Box.createHorizontalGlue());
         buttonArea.add(resetButton);
         buttonArea.add(Box.createHorizontalGlue());
         buttonArea.add(closeButton);
         return buttonArea;
      }
   }

[ C SC 225 | Peter Sanderson | Math Sciences server  | Math Sciences home page | Otterbein ]

Last updated:
Peter Sanderson (PSanderson@otterbein.edu)