Comp201: Principles of Object-Oriented Programming I
Spring 2008 -- Lec25: Introduction to Java GUI Programming (part 2)    


Let's finish up the previous lecture...


1. JFrame with JButtons and event handlers (Frame2.java)

Command Design Pattern

When a user clicks on a button, an ActionEvent object is delivered to the button.  In order for the button to perform an application specific task, we must register an ActionEventListener to the button and program this event listener to perform the task we want.  The ActionListener class is an example of what is called a "command" from the Command Design Pattern.  The code to add an ActionListener to a button looks like:

        myButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // code to perform a task goes here...
            }
        });

The example Frame2 shows how to dynamically change the layout manager for a JFrame by calling its setLayout(...) method resulting in a dynamic re-arrangement of the GUI components in the JFrame.

2. JFrame with JButtons and Adapters (Frame3.java)

Frame3 illustrates how to decouple the view from the rest of the world by having the view communicate to an interface called "adapter".  A controller object, Frame3Control connects the view Frame3 to the world by installing a concrete adapter into the view.  The adapter is instantiated as anonymous inner object and has access to all of its outer object.  The view does not and should not know anything about the world to which it is connected.  This adds flexibility to the design.

 

Null-Object Pattern

In much of the current programming practice, the special value null is often used as a flag to represent a gamut of different and often disconnected concepts such as emptiness and falseness.  This can result in a lot of confusion and complex control structures in the code.  In our view, null should only be used to denote the non-existence of an object.  null is not an object and has no "intelligence" (i.e. behavior) for other objects to interact with.  Therefore, whenever we work with a union of objects and discover that one or more of these objects may be referencing a null value, we almost always have to write code to distinguish null as a special case.  To avoid this problem, we should think of adding a special object, called the null object, to the union with appropriate behavior that will allow us to treat all object references in a consistent and uniform way, devoid of special case consideration.  This design pattern is called the null object pattern.  We have used the null object pattern in one occasion: the EmptyNode to represent the empty state of a (mutable) list (LRStruct).  

In Frame3 the view adapter, _v2wAdapter,  is initialized to an (anonymous) IView2World object.  It is there to guarantee that _v2wAdapter is always referencing a concrete IView2World object, and , since setV2WAdapter(...)  only allows a non-null assignment, we can always call on _v2WAdapter to perform any method without checking for null.

Click here to download code samples.



Last Revised Thursday, 03-Jun-2010 09:50:30 CDT

©2008 Stephen Wong and Dung Nguyen