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

List with Cursor


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

The Pattern Concept

ITERATOR Pattern

Iterator Pattern


Iterator Pattern


Model/View/Controller

Model/View/Controller


Model/View/Controller

Model/View/Controller


OBSERVER Pattern

Observer Pattern

Names in Observer Pattern

Layout Managers

Layout Managers

Layout Managers

Layout Managers


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

Strategy Pattern: Layout Management     


Strategy Pattern: Sorting


Containers, Components, and COMPOSITE Pattern

COMPOSITE Pattern

Composite Pattern

Composite Pattern


Scroll Bars

Scroll Bars

DECORATOR Pattern

Decorator Pattern

Decorator Pattern: Scroll Bars


Streams

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

Decorator Pattern: Input Streams


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