LinkedList<String> list = . . .; ListIterator<String> iterator = list.listIterator(); while (iterator.hasNext()) { String current = iterator.next(); . . . }
Link currentLink = list.head;
while (currentLink != null)
{
Object current = currentLink.data;
currentLink = currentLink.next;
}
for (list.reset(); list.hasNext(); list.next()) { Object x = list.get(); . . . }
Name in
Design Pattern |
Actual Name
(linked lists) |
Aggregate |
List |
ConcreteAggregate |
LinkedList |
Iterator |
ListIterator |
ConcreteIterator |
anonymous class implementing ListIterator |
createIterator() |
listIterator() |
next() |
next() |
isDone() |
opposite of hasNext() |
currentItem() |
return value of next() |
Name in Design Pattern |
Actual Name (Swing buttons) |
Subject |
JButton |
Observer |
ActionListener |
ConcreteObserver |
the class that implements the ActionListener
interface type |
attach() |
addActionListener() |
notify() | actionPerformed() |
JPanel keyPanel = new JPanel();
keyPanel.setLayout(new GridLayout(4, 3));
for (int i = 0; i < 12; i++)
{
JButton keyButton = new JButton(...);
keyPanel.add(keyButton);
keyButton.addActionListener(...);
}
JPanel speakerPanel = new JPanel();
speakerPanel.setLayout(new BorderLayout());
speakerPanel.add(new JLabel("Speaker:"), BorderLayout.NORTH);
speakerField = new JTextArea(10, 25);
speakerPanel.add(speakerField, BorderLayout.CENTER);
public interface LayoutManager { void layoutContainer(Container parent); Dimension minimumLayoutSize(Container parent); Dimension preferredLayoutSize(Container parent); void addLayoutComponent(String name, Component comp); void removeLayoutComponent(Component comp); }
Comparator<Country> comp = new CountryComparatorByName();
Collections.sort(countries, comp);
Name in
Design Pattern |
Actual Name
(layout management) |
Context |
Container |
Strategy |
LayoutManager |
ConcreteStrategy |
a layout manager such as BorderLayout |
doWork() |
a method such as layoutContainer |
Name in
Design Pattern |
Actual Name
(sorting) |
Context |
Collections |
Strategy |
Comparator |
ConcreteStrategy |
a class that implements Comparator |
doWork() |
compare |
Name in
Design Pattern |
Actual Name
(AWT components) |
Primitive |
Component |
Composite |
Container |
Leaf |
a component without children
(e.g. JButton) |
method() |
a method of Component
(e.g. getPreferredSize) |
JScrollPane pane = new JScrollPane(component);
Name in
Design Pattern |
Actual Name
(scroll bars) |
Component | Component |
ConcreteComponent | JTextArea |
Decorator | JScrollPane |
method() |
a method of Component
(e.g. paint) |
InputStreamReader reader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(reader);
Name in
Design Pattern |
Actual Name
(input streams) |
Component | Reader |
ConcreteComponent | InputStreamReader |
Decorator | BufferedReader |
method() |
read |
Border b = new EtchedBorder()
component.setBorder(b);
final Invoice invoice = new Invoice();
final JTextArea textArea = new JTextArea(20, 40);
ChangeListener listener = new
ChangeListener()
{
public void stateChanged(ChangeEvent event)
{
textArea.setText(...);
}
};