Object-Oriented Design & Patterns

Cay S. Horstmann

Chapter 10

More Design Patterns

Chapter Topics

Adapters

The ADAPTER Pattern

Context

  1. You want to use an existing class (adaptee) without modifying it.
  2. The context in which you want to use the class requires target interface that is different from that of the adaptee.
  3. The target interface and the adaptee interface are conceptually related.

The ADAPTER Pattern

Solution

  1. Define an adapter class that implements the target interface.
  2. The adapter class holds a reference to the adaptee. It translates target methods to adaptee methods.
  3. The client wraps the adaptee into an adapter class object.

The ADAPTER Pattern

.

The ADAPTER Pattern

Name in Design Pattern
Actual Name (Icon->Component)
Adaptee
Icon
Target
JComponent
Adapter
IconAdapter
Client
The class that wants to add icons into a container
targetMethod()
paintComponent(), getPreferredSize()
adapteeMethod()
paintIcon(), getIconWidth(), getIconHeight()

The ADAPTER Pattern

The ADAPTER Pattern

Name in Design Pattern
Actual Name (Stream->Reader)
Adaptee
InputStream
Target
Reader
Adapter
InputStreamReader
Client
The class that wants to read text from an input stream
targetMethod()
read (reading a character)
adapteeMethod()
read (reading a byte)

User Interface Actions

User Interface Actions


The Action Interface Type

The Action Interface Type

.

Action Example

The COMMAND Pattern

Context

  1. You want to implement commands that behave like objects

The COMMAND Pattern

Solution

  1. Define a command interface type with a method to execute the command.
  2. Supply methods in the command interface type to manipulate the state of command objects.
  3. Each concrete command class implements the command interface type.
  4. To invoke the command, call the execute method.

The COMMAND Pattern

.

The COMMAND Pattern

Name in Design Pattern
Actual Name (Swing actions)
Command
Action
ConcreteCommand
subclass of AbstractAction
execute()
actionPerformed()
state
name and icon

Factory Methods

The FACTORY METHOD Pattern

Context

  1. A type (the creator) creates objects of another type (the product).
  2. Subclasses of the creator type need to create different kinds of product objects.
  3. Clients do not need to know the exact type of product objects.

The FACTORY METHOD Pattern

Solution

  1. Define a creator type that expresses the commonality of all creators.
  2. Define a product type that expresses the commonality of all products.
  3. Define a method, called the factory method, in the creator type.
    The factory method yields a product object.
  4. Each concrete creator class implements the factory method so that it returns an object of a concrete product class.

The FACTORY METHOD Pattern

.

The FACTORY METHOD Pattern

Name in Design Pattern
Actual Name (iterator)
Creator
Collection
ConcreteCreator
A subclass of Collection
factoryMethod()
iterator()
Product
Iterator
ConcreteProduct
A subclass of Iterator (which is often anonymous)

Not a FACTORY METHOD

Proxies

Deferred Image Loading



Deferred Image Loading

Proxies

The PROXY Pattern

Context

  1. A class (the real subject) provides a service that is specified by an interface type (the subject type)
  2. There is a need to modify the service in order to make it more versatile.
  3. Neither the client nor the real subject should be affected by the modification.

The PROXY Pattern

Solution

  1. Define a proxy class that implements the subject interface type.
    The proxy holds a reference to the real subject, or otherwise knows how to locate it.
  2. The client uses a proxy object.
  3. Each proxy method invokes the same method on the real subject and provides the necessary modifications.

The PROXY Pattern

.

The PROXY Pattern

Name in Design Pattern
Actual Name (image proxy)
Subject
Icon
RealSubject
ImageIcon
Proxy
ImageProxy
request()
The methods of the Icon interface type
Client
JLabel

Singletons

Random Number Generator Singleton

public class SingleRandom
{
   private SingleRandom() { generator = new Random(); }
   public void setSeed(int seed) { generator.setSeed(seed); }
   public int nextInt() { return generator.nextInt(); }
   public static SingleRandom getInstance() { return instance; }
   private Random generator;
   private static SingleRandom instance = new SingleRandom();
}

The SINGLETON Pattern

Context

  1. All clients need to access a single shared instance of a class.
  2. You want to ensure that no additional instances can be created accidentally.

The SINGLETON Pattern

Solution

  1. Define a class with a private constructor.
  2. The class constructs a single instance of itself.
  3. Supply a static method that returns a reference to the single instance.

Not a SINGLETON

Inflexible Hierarchies

Inflexible Hierarchies

.

Visitors

Visitors

Visitors

Double Dispatch

Visitor Example

Visitor Example

.

Visitor Example

Visitor Example

Double Dispatch Example

Double Dispatch Example

.

The VISITOR Pattern

Context

  1. An object structure contains element classes of multiple types, and you want to carry out operations that depend on the object types.
  2. The set of operations should be extensible over time.
  3. The set of element classes is fixed.

The VISITOR Pattern

Solution

  1. Define a visitor interface type that has methods for visiting elements of each of the given types.
  2. Each element class defines an accept method that invokes the matching element visitation method on the visitor parameter.
  3. To implement an operation, define a class that implements the visitor interface type and supplies the operation s action for each element type.

The VISITOR Pattern

.

The VISITOR Pattern

Name in Design Pattern
Actual Name (file system visitor)
Element
FileSystemNode
ConcreteElement
FileNode, DirectoryNode
Visitor
FileSystemVisitor
ConcreteVisitor
PrintVisitor

Other Design Patterns

Conclusion: What You Learned

  1. Object-oriented design  
    The design methodology  
    CRC cards and UML diagrams  
    Design patterns
  2. Advanced Java  
    Interface types, polymorphism, and inheritance  
    Inner classes  
    Reflection  
    Multithreading  
    Collections
  3. User interface programming  
    Building Swing applications  
    Event handling  
    Graphics programming