Slide navigation: Forward with space bar, → arrow key, or PgDn. Backwards with ← or PgUp.
CarIcon
to container Icon
to Component
Name in Design Pattern |
Actual Name (Icon->Component) |
Adaptee |
Icon |
Target |
JComponent |
Adapter |
IconAdapter |
Client |
The class that wants to add icons into a container |
targetMethod() |
paintComponent(), getPreferredSize() |
adapteeMethod() |
paintIcon(), getIconWidth(), getIconHeight() |
InputStreamReader
Name in Design Pattern |
Actual Name (Stream->Reader) |
Adaptee |
InputStream |
Target |
Reader |
Adapter |
InputStreamReader |
Client |
The class that wants to read text from an input stream |
targetMethod() |
read (reading a character) |
adapteeMethod() |
read (reading a byte) |
Action
Interface TypeActionListener
menu.add(helloAction);
AbstractAction
convenience class Action
Interface TypeName in Design Pattern |
Actual Name (Swing actions) |
Command |
Action |
ConcreteCommand |
subclass of AbstractAction |
execute() |
actionPerformed() |
state |
name and icon |
Iterator iter = list.iterator()
Iterator iter = new LinkedListIterator(list);
Collection coll = ...;
Iterator iter = new ???(coll);
Iterator iter = coll.iterator();
Name in Design Pattern |
Actual Name (iterator) |
Creator |
Collection |
ConcreteCreator |
A subclass of Collection |
factoryMethod() |
iterator() |
Product |
Iterator |
ConcreteProduct |
A subclass of Iterator (which is often anonymous) |
DateFormat
instancesDateFormat formatter = DateFormat.getDateInstance();
Date now = new Date();
String formattedDate = formatter.format(now);
getDateInstance
is a static method JLabel label = new JLabel(new ImageIcon(imageName));
JLabel label = new JLabel(new ImageProxy(imageName));
paintIcon
loads image if not previously loadedpublic void paintIcon(Component c, Graphics g, int x, int y)
{
if (image == null) image = new ImageIcon(name);
image.paintIcon(c, g, x, y);
}
Name in Design Pattern |
Actual Name (image proxy) |
Subject |
Icon |
RealSubject |
ImageIcon |
Proxy |
ImageProxy |
request() |
The methods of the Icon interface type |
Client |
JLabel |
public class SingleRandom
{
private SingleRandom() { generator = new Random(); }
public void setSeed(int seed) { generator.setSeed(seed); }
public int nextInt() { return generator.nextInt(); }
public static SingleRandom getInstance() { return instance; }
private Random generator;
private static SingleRandom instance = new SingleRandom();
}
Toolkit
class returns default toolkitToolkit kit = Toolkit.getDefaultToolkit();
Toolkit
Math
class not example of singleton pattern Math
are created Component,
Container,
etc. form hierarchy getPreferredSize
,repaint
Component
class void accept(Visitor v)
Visitor
is an interface type accept
method:public void accept(Visitor v) { v.visit(this); }
visit
public interface Visitor
{
void visitElementType1(ElementType1 element);
void visitElementType2(ElementType2 element);
...
void visitElementTypen(ElementTypen element);
}
DirectoryNode
,FileNode
void visitDirectoryNode(DirectoryNode node)
void visitFileNode(FileNode node)
public class ElementTypei
{
public void accept(Visitor v) { v.visitElementTypei(this); }
...
}
public class DirectoryNode
{
public void accept(Visitor v) { v.visitDirectoryNode(this); }
...
}
File
class denotes both files and directories FileNode
,DirectoryNode
FileSystemNode
FileSystemVisitor
visitFileNode
visitDirectoryNode
PrintVisitor
visitFileNode
)visitDirectoryNode
)..
command
CommandTester.java
GreetingAction.java
visitor
FileNode.java
DirectoryNode.java
DirectoryNode node = new DirectoryNode(new File(".."));
node.accept(new PrintVisitor());
node
is a DirectoryNode
node.accept
calls DirectoryNode.accept
v.visitDirectoryNode
v
is a PrintVisitor
PrintVisitor.visitDirectoryNode
Name in Design Pattern |
Actual Name (file system visitor) |
Element |
FileSystemNode |
ConcreteElement |
FileNode, DirectoryNode |
Visitor |
FileSystemVisitor |
ConcreteVisitor |
PrintVisitor |