Table of contents
Pattern Concept
Every pattern has:
- A (short) name
- A brief description of the context
- A lengthy description of the problem
- A prescription for a solution
A pattern presents proven advice in a standard format.
A design pattern gives advice about a problem in software design.
Pattern: Iterator
Iterators do not expose the internal structure of a collection class.
Iterators are preferred over cursors since you can attach more than one iterator to a collection.
The iterator concept occurs in many different programming situations.
The iterator pattern teaches how to acces elements of an aggregate object.
Context
- An oject (called aggregate) contains other objects (called elements)
- Clients (methods using aggregate) need acces to elements.
- The aggregate should not expose the internal structure
- There may be multiple clients that need simultaneous acces.
Solution
- Define an iterator class that fetches one element at a time.
- Each iterator object needs to keep track of the position of the next element to fetch.
- If there are several variation of the aggregate and the iterator classes, it is best if they implement common interface types.
Then the client only needs to know the interface types, not the concrete classes.

Example
| Name in Design Pattern | Actual Name |
| Aggregate | List |
| ConcreteAggregate | LinkedList |
| Iterator | ListIterator |
| ConcreteIterator | An anonymous class that implements the ListIterator interface type |
| createIterator() | listIterator() |
| next() | next() |
| isDone() | Opposite of hasNext() |
| currentItem() | Return value of next() |
Pattern: Observer
Model / View / Controller architecture:
- Model; holds the information in some data structure
- View; draw the visible parts of the data
- Controller; processes the user information
Sequence

The Observer pattern teaches how an object can tell other objects about events.
Context
- An object (called subject) is the source of events.
- One or more objects (called observers) want to know when an event occurs.
Solution
- Define an observer interface type. Observer classes must implement this interface type.
- The subject maintains a collection of observer objects
- The subject class supplies methods for attaching observers.
- Whenever an event occurs, the subject nofifies all observers.
Example
| Name in Design Pattern | Actual Name |
| Subject | JButton |
| Observer | ActionListener |
| ConcreteObserver | The class that implements the Action Listener interface type |
| attach() | addActionListener |
| notify() | actionPerformed |
Layout Managers and Pattern: Strategy
You add user interface components to a container. A layout manager arranges the components in a container.
Examples of Layout Managers:
- FlowLayout : Lays out components left to right, starting a new row when the row is full.
- BoxLayout: Lays out components horizontally or vertically without starting additional rows and columns.
- BorderLayout: Divides up a container in five areas (NORTH,SOUTH,WEST,EAST,CENTER), adds the component to the specified area and tries to fill it up.
- GridLayout: Lays out components in a rectangular grid. All components are resized to an identical size.
- GridBagLayout: Lays out components in a grid, but rows and columns can have different sizes and components can span multiple rows and columns.
Class Diagram

Pattern: Strategy
The Strategy pattern teaches how to supply variants of an algorithm.
Context
- A Class (called context) can benefit from different variations of an algorithm.
- Clients of the context class sometimes want to supply custom versions of the algorithm.
Solution
- Define an interface type that is an abstraction for the algorithm. We'll call this interface type the strategy.
- Concrete strategy classes implement the strategy interface type. Each strategy class implements a version of the algorithm.
- The clients supplies a concrete strategy object to the context class.
- Whenever the algorithm needs to be executed, the context class calls the appropriate methods of the strategy object.
Examples
| Name in Design Pattern | Actual Name |
| Context | Container |
| Strategy | LayoutManager |
| ConcreteStrategy | A Layout manager such as BorderLayout. |
| doWork() | A method of the LayoutManager interface type such as layoutContainer. |
| Name in Design Pattern | Actual Name |
| Context | Collections |
| Strategy | Comparator |
| ConcreteStrategy | A class that implements the Comparator interface type. |
| doWork() | compare() |
Components, Containers and Pattern: Composite
User Interface components are contained in containers. If a JPanel can contain other components, it must be a Container. But if we add it to another JPanel, it's a Component.
Pattern: Composite
The Composite pattern teaches how to combine several objects into an object that has the same behavior as its parts.
Context
- Primitive objects can be combined into composite objects.
- Clients treat a composite object as a primitive object.
Solution
- Define an interface type that is an abstraction for the primitive objects.
- A composite object contains a collection of primitive objects.
- Both primitive classes and composite classes implement that interface type.
Example
| Name in Design Pattern | Actual Name |
| Primitive | Component |
| Composite | Container or a subclass of JPanel |
| Leaf | A component that has no children such as JButton or JTextArea |
| method() | A method of the Component interface such as getPreferredSize |
Scroll Bars and Pattern: Decorator
When a component contains more information than can be shown on the screen, it becomes necessary to add scroll bars.
Because the scroll bars add functionality to the underlying text area, they are called a decoration.
Pattern: Decorator
The Decorator pattern teaches how to form a class that adds functionality to antoher class while keeping its interface.
There is one essential difference between the Decorator pattern and the Composite pattern. A decorator enhances the behavior of a single component, whereas a composite collects multiple components.
Context
- You want to enhance the behavior of a class (called component class).
- A decorated component can be used in the same way as a plain component.
- The component class does not want to take on the responsbility of the decoration.
- There may be an open-ended set of possible decorations.
Solution
- Define an interface type that is an abstraction for the component.
- Concrete component classes implement this interface type.
- Decorator classes also implement this interface type.
- A decorator object manages the component object that it decorates.
- When implementing a method from a component interface type, the decorator class applies the method to the decorated component and combines the result with the effect of the decoration.

Examples
| Name in Design Pattern | Actual Name |
| Component | Component |
| ConcreteComponent | JTextArea |
| Decorator | JScrollPane |
| method() | A method of the Component interface. For example, the paint method paints a part of the decorated component and the scroll bars. |
Another example is the Reader class.
| Name in Design Pattern | Actual Name |
| Component | Reader |
| ConcreteComponent | FileReader |
| Decorator | BufferedReader |
| method() | The read method. Calling read on a buffered reader invokes read on the component reader if the buffer is empty. |
How to recognize Patterns
Focus on the intent of the pattern.
- Composite: group components into a whole.
- Decorator: decorate a component.
- Strategy: wrap an algorithm into a class.
Remember where the patterns are used.
- Composite: Container
- Decorator: Scrollbar
- Strategy: LayoutManagers
Don't fall in the trap of using the name for the situation!
Just because something seems strategic does not mean that the Strategy pattern is at work.
Check the context!
- A Border class is not a Decorator Pattern:
- You want to enhance the behavior of the class.
TRUE. - A decorated component can be used in the same way as a plain component
TRUE. - The component class does not want to take on the responsibility of the decoration.
FALSE, a component class has a setBorder method. It is responsible for applying the border.
(the border class is used as an attribute which the component class uses, the border class does not transform the component class.)
- You want to enhance the behavior of the class.



.png)

Comments