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

  1. An oject (called aggregate) contains other objects (called elements)
  2. Clients (methods using aggregate) need acces to elements.
  3. The aggregate should not expose the internal structure
  4. There may be multiple clients that need simultaneous acces.

Solution

  1. Define an iterator class that fetches one element at a time.
  2. Each iterator object needs to keep track of the position of the next element to fetch.
  3. 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.


IteratorPattern (1).png

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:

  1. Model; holds the information in some data structure
  2. View; draw the visible parts of the data
  3. Controller; processes the user information

Sequence

mvcarchitecture-sequence (1).png

The Observer pattern teaches how an object can tell other objects about events.

Context

  1. An object (called subject) is the source of events.
  2. One or more objects (called observers) want to know when an event occurs.

Solution

  1. Define an observer interface type. Observer classes must implement this interface type.
  2. The subject maintains a collection of observer objects
  3. The subject class supplies methods for attaching observers.
  4. Whenever an event occurs, the subject nofifies all observers.

ObserverPattern.png

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. 

Layoutmanagers.jpg


Class Diagram

LayoutManager (3).png

Pattern: Strategy

The Strategy pattern teaches how to supply variants of an algorithm.

Context
  1. A Class (called context) can benefit from different variations of an algorithm.
  2. Clients of the context class sometimes want to supply custom versions of the algorithm.
Solution
  1. Define an interface type that is an abstraction for the algorithm. We'll call this interface type the strategy.
  2. Concrete strategy classes implement the strategy interface type. Each strategy class implements a version of the algorithm.
  3. The clients supplies a concrete strategy object to the context class.
  4. Whenever the algorithm needs to be executed, the context class calls the appropriate methods of the strategy object.

Strategy (1).png

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
  1. Primitive objects can be combined into 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. A composite object contains a collection of primitive objects.
  3. Both primitive classes and composite classes implement that interface type.

Composite (1).png

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
  1. You want to enhance the behavior of a class (called component class).
  2. A decorated component can be used in the same way as a plain component.
  3. The component class does not want to take on the responsbility of the decoration.
  4. There may be an open-ended set of possible decorations.
Solution
  1. Define an interface type that is an abstraction for the component.
  2. Concrete component classes implement this interface type.
  3. Decorator classes also implement this interface type.
  4. A decorator object manages the component object that it decorates.
  5. 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.

DecoratorPattern.png

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.)
Tag page
Pages that link here
Page statistics
829 view(s), 10 edit(s), and 13725 character(s)

Comments

You must login to post a comment.

Attach file

Attachments