Java Design Patterns: Examples of Creational, Structural, and Behavioral Patterns - Prof. , Study notes of Programming Languages

An overview of design patterns, focusing on creational, structural, and behavioral patterns. It includes an example java program that demonstrates the use of these patterns through the creation of a simple gui application. The document also discusses the use of actionlistener and actionevent classes, as well as the differences between awt list class and jfc jlist class.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-jcf
koofers-user-jcf 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Design Patterns Taxonomy
Creational patterns
Concern the proc ess of object creat ion
Structural patterns
Deal with the composition of classes or objects
Behavioral patterns
Characterize the ways in which classes or objects
interact and distribute responsibility
2
Structural Patterns
3
An Example Application
Lets create a simple Java program that
allows you to enter names into a list,
and then select some of those names to
be transferred to another list.
Our initial list consists of a class roster
and the second list, those who will be
doing advanced work.
In the example UI,
You enter names into the top en try
field and click on Insert to move the
names into the left-hand list box.
To move names to the right-hand list
box, you click on them, and then click
on Add.
To remove a name from the right hand
list box, click on it and then on
Remove. This moves the name back to
the left-hand list.
4
Lets Look at some Code!
The code will consist of a GUI creation
constructor and an actionListener routine for the
three buttons:
pf3
pf4
pf5

Partial preview of the text

Download Java Design Patterns: Examples of Creational, Structural, and Behavioral Patterns - Prof. and more Study notes Programming Languages in PDF only on Docsity!

1

Design Patterns Taxonomy

Creational patterns

  • Concern the process of object creation

Structural patterns

  • Deal with the composition of classes or objects

Behavioral patterns

  • Characterize the ways in which classes or objects

interact and distribute responsibility

Structural Patterns

3

An Example Application

Lets create a simple Java program that

allows you to enter names into a list,

be transferred to another list.and then select some of those names to

doing advanced work.and the second list, those who will beOur initial list consists of a class roster

In the example UI,

field and click onYou enter names into the top entry

Insert

(^) to move the

names into the left-hand list box.

onbox, you click on them, and then clickTo move names to the right-hand list

Add

.

Remove list box, click on it and then onTo remove a name from the right hand

. This moves the name back to

the left-hand list.

Lets Look at some Code!

constructor and anThe code will consist of a GUI creation

actionListener

routine for the

three buttons:

5

ActionListener

  • public interface ActionListener extends EventListener

The listener interface for receiving action events.

  • The class that is interested in processing an action event

component'sthat class is registered with a component, using theimplements this interface, and the object created with

addActionListener

method. When the

action event occurs, that object's

actionPerformed

method is invoked.

ActionEvent

public class ActionEvent extends AWTEvent

A semantic event which indicates that a component-defined action occurred.

component-specific action occurs (such as being pressed).This high-level event is generated by a component (such as a Button) when the

such events using the component's addActionListener method.The event is passed to every ActionListener object that registered to receive

when the event occurs.The object that implements the ActionListener interface gets this ActionEvent

(semantic) event like "button pressed".movements and mouse clicks, and can instead process a "meaningful"The listener is therefore spared the details of processing individual mouse

java.lang.Object

java.util.EventObject

java.awt.AWTEvent

java.awt.event.ActionEvent

7

getSource()

Method inherited from class java.util.EventObject

public Object getSource()

  • Returns: The object on which the Event initially

occurred.

addName()

add() is a method of AWT

List

class.

13

More on JList…

vector or other ListModel class associated with it.The JList class is a window container which has an array,

the data.It is this ListModel that actually contains and manipulates

class.relies on being inserted in the viewport of the JScrollPaneThe JList class does not contain a scroll bar, but instead

as long as you provide the cell drawing routine for them.limited to strings, but may be almost any kind of objects,Data in the JList class and its associated ListModel are not

illustrating each choice in the list.This makes it possible to have list boxes with pictures

Our Task is Much Simpler!

that in this simple case, needs only the three methods:We are only going to create a class that emulates the List class, and

add(String);

remove(String);

String[] getSelectedItems()

the class we create implements those methods:We define the needed methods as an interface and then make sure that

15

JScrollPane

interface.which implements the methods of the awtListWe create a class that contains a JList class but

all, but the JScrollPane that encloses it.outer container for a JList is not the list element atThis is a pretty good choice here, because the

17

JListData

methods:AbstractListModel, which defines the followingthe JListData class. This class is derived from theNote that the actual data handling takes place in

19

Fire?

the actual displayed list data.path between the data stored in the ListModel andThe three fire methods are the communication

updated.Firing them causes the displayed list to be

Hence, Each time we add data to the

data

vector,

we call the

fireIntervalAdded

method to tell the

list.list display to refresh that area of the displayed

Final Touches

25

Reflection

must adapt to: reflection.Java has yet another way for adapters to recognize which of several classes it

parameters for any class.You can use reflection to discover the names of public methods and their

For example, for any arbitrary object you can use the

getClass()

(^) method to

obtain its class and the

getMethods()

method to obtain an array of the method

names.

Fine Print

very large list of methods.code shown in the previous slide can generate aA “method dump” like the one produced by the

which arguments that method requires.you are looking for and simply want to find outIt is easier if you know the name of the method

the adapting you need to carry out.From that method signature, you can then deduce