Title: Understanding the language
1Understanding the language
- A look at the Java API documentation
- Why different than other languages
- Where to find it
- What is contains
- Format - JavaDocs
- API definition including
- inheritance, realization trees
- behavior and state definitions
- Tutorials
- Links
2Understanding the language
- Understanding the Java Development Kit
installation - bin
- executables include compiler, virtual machine,
jar file management, appletviewer, RMI support,
IDL (CORBA related) support - demos
- applets designed to demonstrate language
functionality - docs
- Java API documentation
- JRE
- deployable runtime environment
- lib (library jar files)
- incorporation into other executables
3Whats next..
- Continue to development understand of basic OO
constructs and how they are represented in two OO
languages Java and C - Java
- interfaces vs. abstract classes
- concrete classes vs. non-concrete classes
- class methods and attributes vs. instance methods
and attributes - C
- interfaces (pure virtual classes and methods)
- abstract classes (virtual classes and methods)
- concrete classes vs. non-concrete classes
- class methods and attributes vs. instance methods
and attributes
4Whats next..
- Mechanisms to be used in understanding these
constructs (hint - programming assignment
related) - Java
- event handling model (GUI related)
- object persistence (provided object
serialization) - remote method invocation (RMI)
- libraries and static classes
- C
- typical call back (GUI related)
- object persistence (home grown object
serialization) - remote method calls
- libraries and static classes
5Interface Review...
- Java Definition - an entity used to define
signatures of behaviors and constants to
represent an abstracted entity defines behavior
of an object by declaring a set of
characteristics for the object - no default behavior implementation allowed
- provides a degree of multiple inheritance (type
not implementation can inherit other interfaces
but not classes) - not a class!!!!
- Similar to C pure virtual methods/classes
- classes implementing an interface must provide
implementations to all methods else the class is
declared to be abstract by the compiler
6Interface Usage...
- Why use an interface (general example)
- define, abstract behavior without concern of
implementation - allows for construction without low levels
details - behavior must be define as to not impact possible
state definitions - if all parameter and return types are primitive
types, limitation of state could be forced - use of other interfaces provides a stronger
framework (ATC example) - program to the interface, not an implementation
- avoid declaring variables to be instances of
particular concrete classes focus on the
interface - provides stronger encapsulation
- facilitates polymorphism
7Interface Usage...
- Why use an interface
- Consider - different viewers and browsers such as
Appletviewer, Internet Explorer and Netscape
Navigator view images differently - Implementations of viewers and browsers vary from
platform to platform (hardware and operating
systems) - To resolve this issue all viewers and browsers
implement the java.applet.AppletContext used my
the java.applet.Applet class. Since the
java.applet.AppletContext is an interface, the
java.applet.Applet class is tied to the method
signatures, not method implementations Allows
this combination to be used consistently across
environments
8Interface Usage
- Javas Event Delegation Model is strongly
dependent on the use of interfaces - There are four key concepts to understanding this
model - event classes
- event listeners
- explicit event enabling
- adapters
9Interface Usage
- Event classes represent all activity between a
program, the system, and the programs users - The event delegation models defines the following
events - ActionEvent generated by activation of
components - AdjustmentEvent generated by adjustment of
adjustable components such as scroll bars - ContainerEvent generated when components are
added to or removed from a container - FocusEvent generated when a component receives
or loses input focus - ItemEvent generated when an item is selected
from a list, choice, or checkbox
10Interface Usage
- The event delegation models defines the following
events (continued) - KeyEvent generated by keyboard activity
- MouseEvent generated by mouse activity
- PaintEvent generated when a component is painted
- TextEvent generated when a text component is
modified - WindowEvent generated by window activity (such
as iconifying or deiconifying)
11Interface Usage
12Interface Usage
- All Java events can be placed in one of two
categories low-level events and semantic events - low-level events are considered to be singular
points of functionality - semantic events are aggregate groups of low-level
input events. There are four basic semantic
events - ActionEvent
- AdjustmentEvent
- ItemEvent
- TextEvent
- there is no way to access the low level events
within a semantic event
13Interface Usage
- There are two approaches to handling events
- the first approach delegates event handling to a
listener object - the second approach explicitly enables the
originating component to handle its own events - Both approaches will be discussed
- Event delegation is first
14Interface Usage
- Event delegation
- Key topic event listener
- an object that a component has delegated the task
of handling a particular kind of event - when a component receives input, an event of the
appropriate type is constructed - the event is passes as a parameter to a method
call on the listener - it is assumed that the listener must implement
the interface that contains the event-handling
method - Example on next slides
15Interface Usage...
Formula for giving an Action listener to a
component Create a listener class that
implements the ActionListener interface Construc
t the component Construct the instance of
the listener class Call addActionListener() on
the component, passing in the listener object
class MyActionListener implements ActionListener
public void actionPerformed(ActionEvent ae)
System.out.print(Action performed)
Public class ListenerTest extends Applet
public void init () Button btn new
Button(Ok) MyActionListener myAL new
MyActionListener() btn.addActionListener(my
Al) add(btn)
16Interface Usage...
There are 11 listener types, each represent by an
interface
- ActionListener
- AdjustmentListener
- ComponentListener
- ContainerListener
- FocusListener
- ItemListener
- KeyListener
- MouseListener
- MouseMotionListener
- TextListener
- WindowListener
Note a component may have multiple listeners for
any event type but there is no guarantee that
listeners are notified in the order in which they
were added.
17Interface Usage...
- When adding an event listener the method call is
formatted as - addXXXListener (XXX is the event
component name) - When removing an event listener the method call
is formatted as - removeXXXListener (XX is the
event component name)
Public class ListenerTest extends Applet
public void method ()
btn.removeActionListener(myAl)
18Interface Usage
- Explicit Event Enabling
- It is possible to subclass the component and
override the method that receives events and
dispatches them to listeners
Class MyBtn extends Button public MyBtn
(String label) super(label)
enableEvents(AWTEvent.ACTION_EVENT_MASK)
public void processActionEvent(ActionEvent ae)
System.out.print(Processing an action
event.) super.processActionEvent(ae)
The constructor calls enableEvents(), passing in
a constant that enables the processing of Action
events. Each of the 11 listener types has
a corresponding XXX_EVENT_MASK constant defined
in the AWTEvent class along with
corresponding processXXXEvent() methods.
19Interface Usage...
These are the 11 event tasks.
- AWTEvent.ACTION_EVENT_MASK
- AWTEvent.ADJUSTMENT_EVENT_MASK
- AWTEvent.COMPONENT_EVENT_MASK
- AWTEvent.CONTAINTER_EVENT_MASK
- AWTEvent.FOCUS_EVENT_MASK
- AWTEvent.ITEM_EVENT_MASK
- AWTEvent.KEY_EVENT_MASK
- AWTEvent.MOUSE_EVENT_MASK
- AWTEvent.MOUSE_MOTION_EVENT_MASK
- AWTEvent.TEXT_EVENT_MASK
- AWTEvent.WINDOW_EVENT_MASK
20Interface Usage...
- Another method for explicit event enabling
The difference between this method and the
enableEvents() method is the order in which event
handlers are called. When enableEvents() is
called, the components processActionEvent()
method will be called before any Action listeners
are notified. When the components subclass is
its own event listener, there is no guarantee as
to the order of notification. Caution must be
used in either approach so not to increase
coupling and breaking the MVC strategy.
Class MyBtn extends Button implements
ActionListener public MyBtn (String label)
super(label) addActionListener(this
) public void actionPerformed(ActionEvent
ae) // process event here
21Interface Usage
Consider the following example of a problem with
using event listeners
This class will not compile because the
WindowListener actually defines seven methods
and this class only defines one.
class IconListener implements WindowListener
public void windowIconified(WindowEvent we)
// Process the event
22Interface Usage
- Adapters allow for a convenient solution to the
previous problem - A class that implements an interface by providing
null or do nothing implementations - Allows a programmer to use an interface without
directly defining implementations to all methods
within the interface. This eliminates tedious
possible error prone development activities - There are adapters for all event listener
interfaces (seven) that have more that one method
to declare their interface
23Interface Usage...
The 7 listener adapters -
Adapter Class Listener Interface
- ComponentAdapter ComponentListener
- ContainerAdapter ContainerListener
- FocusAdapter FocusListener
- KeyAdapter KeyListener
- MouseAdapter MouseListener
- MouseMotionAdapter MouseMotion Listener
- WindowAdapter WindowListener
24Interface Usage
Solution
class IconListener extends WindowAdapter
public void windowIconified(WindowEvent we)
// Process the event
This class overrides the WindowAdapter windowIconi
fied method. Since the WindowAdapter class
provides implementation to the other methods
defined in the WindowLister interface, this
class will compile.
25Interface Usage
26Interface Usage
- Use of interfaces outside of the event delegation
GUI model. Note that the Observable and
Observer entities are a Java class and interface
(respective).
27Interface Usage
- Interfaces were used to support the previous two
methods of event handling but abstract classes
can be used to enhance those - Java defines an entity known as Action
(interface) and it extends the ActionListener
interface (this should be somewhat familiar)
28Interface Usage
- To take advantage of that mechanism, the abstract
class AbstractAction is provided in the JDK. It
implements the Action interface - AbstractAction can then be subclassed for use
29Interface Usage
- Actions
- Actions represent functionality to be used by a
GUI component. By using an Action the
encapsulated functionality can be easily used by
many different components - a single action my
have a menu, toolbar and panel button
representation simultaneously (An icon on a
toolbar, a menu choice, or even a KeyStroke can
all use the same object for processing) - Actions decentralized logic usually incorporated
in GUI components - often a large
actionPerformed() method contains the logic for
all of the GUI components of a window or panel
(large case or conditional this is evil).
Actions segregate functionality from GUIs such
that each functionality is a single entity.
30Interface Usage
- Actions
- The default implementation of an Action,
AbstractAction provides display information and
the implementation of the actionPerformed method
as required by the listener interface
Both Actions and the other event delegation
model have the same possible problem - the
possibility of making the actionPerformed method
very large, containing a great deal of business
domain processing logic
31Interface Usage
- Actions
- To resolve the possible actionPerformed problem
the Command pattern can be used. - The Command pattern decouples a request from
the requestor with a request being any type of
operation (generally high-level) the normal
process is that when an operation is desired, a
request to the appropriate GUI component is made
32Interface Usage
The command object contains everything needed to
execute the command. The only thing that the
GUI knows it that it needs to call the execute()
method in its Command object reference. This
completely decouples the GUI or presentation
layer from the model layer. Changes in one
doesnt always require changes in the other!
33Interface Usage
- Command Pattern
- Use of this pattern provide a complete move to
objects, event in terms of functionality (easier
to achieve the benefits of object-oriented
analysis, design and implementation - Changes in the presentation layer (GUI) will not
cause changes in the application/business domain
logic code. This allows for greater reuse
opportunity, decreased testing requirements,
better maintainability and etc - Makes it easier for teams to work a system.
Encapsulating GUI and events allows for different
members to work on the presentation and model
layers at the same time