Object-Oriented Design & Patterns

Cay S. Horstmann

Chapter 5

Patterns and GUI Programming

Chapter Topics

List Iterators

LinkedList<String> list = . . .;
ListIterator<String> iterator = list.listIterator();
while (iterator.hasNext())
{
String current = iterator.next();
. . .
}

Classical List Data Structure

High-Level View of Data Structures

List with Cursor

.

for (list.reset(); list.hasNext(); list.next())
{
Object x = list.get();
. . .
}

The Pattern Concept

Short Passages Pattern

.

Short Passages Pattern

Context

"...Long, sterile corridors set the scene for everything bad about modern architecture..."

Problem

a lengthy description of the problem, including

Short Passages Pattern

Solution

Keep passages short. Make them as much like rooms as possible, with carpets or wood on the floor, furniture, bookshelves, beautiful windows. Make them generous in shape and always give them plenty of light; the best corridors and passages of all are those which have windows along an entire wall.

.

Iterator Pattern

Context

  1. An aggregate object contains element objects
  2. Clients need access to the element objects
  3. The aggregate object should not expose its internal structure
  4. Multiple clients may want independent access

Iterator Pattern

Solution

  1. Define an iterator that fetches one element at a time
  2. Each iterator object keeps track of the position of the next element
  3. If there are several aggregate/iterator variations, it is best if the aggregate and iterator classes realize common interface types.

Iterator Pattern

.

Iterator Pattern

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 hasNext()


Model/View/Controller

Model/View/Controller



Model/View/Controller

Model/View/Controller

Model/View/Controller


.

Observer Pattern

Observer Pattern

Context

  1. An object, called the subject, is source of events
  2. One or more observer objects want to be notified when such an event occurs.

Solution

  1. Define an observer interface type. All concrete observers implement it.
  2. The subject maintains a collection of observers.
  3. The subject supplies methods for attaching and detaching observers.
  4. Whenever an event occurs, the subject notifies all observers.

Observer Pattern

.

Names in Observer Pattern

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()

Layout Managers

Layout Managers

Layout Managers

.

Layout Managers

Layout Managers

.

Voice Mail System GUI

Voice Mail System GUI

. 

Voice Mail System GUI

Voice Mail System GUI

.

Voice Mail System GUI

.

Custom Layout Manager


The LayoutManager Interface Type

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);
}

Form Layout

Strategy Pattern

Strategy Pattern

Context

  1. A class can benefit from different variants for an algorithm
  2. Clients sometimes want to replace standard algorithms with custom versions

Solution

  1. Define an interface type that is an abstraction for the algorithm
  2. Actual strategy classes realize this interface type.
  3. Clients can supply strategy objects
  4. Whenever the algorithm needs to be executed, the context class calls the appropriate methods of the strategy object

Strategy Pattern

.

Strategy Pattern: Layout Management     

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

Strategy Pattern: Sorting

Name in Design Pattern
Actual Name (sorting)
Context
Collections
Strategy
Comparator
ConcreteStrategy
a class that implements Comparator
doWork()
compare

Containers and Components

Composite Pattern

Context

  1. Primitive objects can be combined to composite objects
  2. Clients treat a composite object as a primitive object

Solution

  1. Define an interface type that is an abstraction for the primitive objects
  2. Composite object collects primitive objects
  3. Composite and primitive classes implement same interface type.
  4. When implementing a method from the interface type, the composite class applies the method to its primitive objects and combines the results

Composite Pattern

.

Composite Pattern

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)

Scroll Bars


Scroll Bars

.

Decorator Pattern

Context

  1. Component objects can be decorated (visually or behaviorally enhanced)
  2. The decorated object can be used in the same way as the undecorated object
  3. The component class does not want to take on the responsibility of the decoration
  4. There may be an open-ended set of possible decorations

Decorator Pattern

Solution

  1. Define an interface type that is an abstraction for the component
  2. Concrete component classes realize this interface type.
  3. Decorator classes also realize this interface type.
  4. A decorator object manages the component object that it decorates
  5. When implementing a method from the component interface type, the decorator class applies the method to the decorated component and combines the result with the effect of the decoration.

Decorator Pattern

.

Decorator Pattern: Scroll Bars

Name in Design Pattern
Actual Name (scroll bars)
Component Component
ConcreteComponent JTextArea
Decorator JScrollPane
method()
a method of Component (e.g. paint)

Streams

InputStreamReader reader = new InputStreamReader(System.in); 
BufferedReader console = new BufferedReader(reader);

Decorator Pattern: Input Streams

Name in Design Pattern
Actual Name (input streams)
Component Reader
ConcreteComponent InputStreamReader
Decorator BufferedReader
method()
read

How to Recognize Patterns

Litmus Test


Litmus Test

  1. Component objects can be decorated (visually or behaviorally enhanced)
    PASS
  2. The decorated object can be used in the same way as the undecorated object
    PASS
  3. The component class does not want to take on the responsibility of the decoration
    FAIL--the component class has setBorder method
  4. There may be an open-ended set of possible decorations

Putting Patterns to Work

Bundles

Bundles

.

Discounted Items

Discounted Items

.

Model/View Separation

Change Listeners

Change Listeners

Observing the Invoice

.

Iterating Through Invoice Items

Iterators

Iterators


.

Formatting Invoices

Formatting Invoices

Formatting Invoices

.

Formatting Invoices

.