Object-Oriented Design & Patterns

Cay S. Horstmann

Chapter 8

Frameworks

Chapter Topics

Frameworks

Application Frameworks

Applets

Applets

.

Applets

Example Applet

Example Applet



Applets as a Framework

Collections Framework

Collections Framework: Interface Types

Collections Framework: Classes

Collections Framework

.

Collection<E> Interface Type

boolean add(E obj) 
boolean addAll(Collection c)
void clear()
boolean contains(E obj)
boolean containsAll(Collection c)
boolean equals(E obj)
int hashCode()
boolean isEmpty()
Iterator iterator()
boolean remove(E obj)
boolean removeAll(Collection c)
boolean retainAll(Collection c)
int size()
E[] toArray()
E[] toArray(E[] a)

Iterator<E>  Interface Type

boolean hasNext()
E next()
void remove()

AbstractCollection Class

public E[] toArray()
{
   E[] result = new E[size()];
   Iterator e = iterator();
   for (int i = 0; e.hasNext(); i++)
      result[i] = e.next();
   return result;
}

AbstractCollection Class

Adding a new Class to the Framework

Adding a new Class to the Framework

.

Sets

Lists

boolean add(int index, E obj)
boolean addAll(int index, Collection c)
E get(int index)
int indexOf(E obj)
int lastIndexOf(E obj)
ListIterator listIterator()
ListIterator listIterator(int index)
E remove(int index)
E set(int index, int E)
List subList(int fromIndex, int toIndex)

List Iterators

int nextIndex()
int previousIndex()
boolean hasPrevious()
E previous()
void set(E obj)

List Classes 

List Classes 

.

Optional Operations

Views

Views

Graph Editor Framework

Graph Editor Framework

User Interface

User Interface


Mouse Operations

Division of Responsibility

Adding Nodes and Edges

Adding Nodes and Edges

PROTOTYPE Pattern

Context

  1. A system instantiates objects of classes that are not known when the system is built.
  2. You do not want to require a separate class for each kind of object.
  3. You want to avoid a separate hierarchy of classes whose responsibility it is to create the objects.

Solution

  1. Define a prototype interface type that is common to all created objects.
  2. Supply a prototype object for each kind of object that the system creates.
  3. Clone the prototype object whenever a new object of the given kind is required. 

PROTOTYPE Pattern

.

PROTOTYPE Pattern

Name in Design Pattern
Actual name (graph editor)
Prototype
Node
ConcretePrototype1
CircleNode
Creator
The GraphPanel that handles the mouse operation for adding new nodes


Framework Classes

Node Connection Points

.

Framework Classes

Framework Classes

Framework UI Classes

A Framework Instance

Programmer responsibilities

A Framework Instance

.

A Framework Instance

Generic Framework Code

Add New Node

public void mousePressed(MouseEvent event)
{
Point2D mousePoint = event.getPoint();
Object tool = toolBar.getSelectedTool();
...
if (tool instanceof Node)
{
Node prototype = (Node) tool;
Node newNode = (Node)prototype.clone();
graph.add(newNode, mousePoint);
}
...
repaint();
}

Add New Node

.

Add New Edge

Add New Edge

Add New Edge

.

Enhancing the Framework

Enhancing the Framework


Enhancing the Framework

Using the Framework Enhancement

Another Framework Instance

.

Another Framework Instance

Edge Properties



Enhancing the Framework II

Enhancing the Framework II