- Forward


Design Patterns
An Introduction for Beginning Programmers


Prof. David Bernstein
James Madison University

Computer Science Department

Print

Software Design Patterns
Back SMYC Forward
  • Defined:
    • "Descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context." (Gamma, Helm, Johnson and Vlissides)
  • Level of Abstraction:
    • Design patterns are at a higher level of abstraction than data structures and algorithms
Software Design Patterns (cont.)
Back SMYC Forward
  • Types:
    • Creational (i.e. concerned with object creation)
    • Structural (i.e., concerned with the composition of objects)
    • Behavioral (i.e., concerned with interactions and responsibilities)
  • Benefits:
    • Simplifies the development of designs
    • Promotes better communication
    • May increase re-use and quality
The Purpose(s) of this Presentation
Back SMYC Forward
  • Convince you that you have something to learn
  • Convince you that design patterns are useful
  • Discuss some design patterns (that even beginning programmers can understand and use)
Singleton Pattern - Motivation
Back SMYC Forward
  • Some applications require exactly one instance of a class:
    • Windowing systems with one event queue
    • Word processors with one menu bar (for all documents)
  • Unfortunately, constructors can be called repeatedly:
    • In one method
    • In one class
    • Across multiple classes
Singleton Pattern - The Traditional Approach
Back SMYC Forward

singleton

Singleton Pattern - An Example
Back SMYC Forward
A FileViewer
javaexamples/singleton/v1/FileViewer.java (Fragment: skeleton)
 
javaexamples/singleton/v1/FileViewer.java (Fragment: createInstance)
 
Singleton Pattern - An Example (cont.)
Back SMYC Forward
Using a FileViewer
javaexamples/singleton/v1/FileChooser.java (Fragment: valueChanged)
 
Iterator - Motivation
Back SMYC Forward
  • Computers vs. Calculators:
    • Computers can perform the same operation many times
  • Programming Languages:
    • Harness this power using loops
  • A Problem:
    • Traditional looping requires an understanding of the structure of the "aggregate"
Iterator - Motivation (cont.)
Back SMYC Forward

Looping Over an Array

javaexamples/iterator/ArrayExample.java (Fragment: 1)
 

Looping Over a Vector

javaexamples/iterator/VectorExample.java (Fragment: 1)
 
Iterator - The Traditional Approach
Back SMYC Forward

In UML:

iterator
Iterator - Examples in Java
Back SMYC Forward
  • The Iterator:
    • Enumeration
  • Some Aggregates:
    • Vector
    • Hashtable
Model-View-Controller - Motivation
Back SMYC Forward
  • Many Objects:
    • Manage information/content
    • Present/display that information
    • React to events
  • Examples:
    • Components/widgets in a GUI
    • Database management systems
M-V-C - The Traditional Approach
Back SMYC Forward
mvc_pattern
M-V-C - An Example
Back SMYC Forward
MVCTextField_context
M-V-C - An Example: (cont.)
Back SMYC Forward

Handling an arrowkeyPressed Event

MVCTextField_arrowkeyPressed
M-V-C - An Example: (cont.)
Back SMYC Forward

Handling a mouseUp Event

MVCTextField_mouseUp
M-V-C - An Example: (cont.)
Back SMYC Forward

Handling a characterKeyPressed Event

MVCTextField_characterkeyPressed
M-V-C - Concrete Examples
Back SMYC Forward

Creating an Integer Entry Field

javaexamples/mvc/IntegerDocument.java
 
javaexamples/mvc/IntegerEntryField.java
 

Creating a Password Entry Field

What would you change and why?

Skins

What would you change and why?

Other Software Design Patterns
Back SMYC Forward
  • Some Examples:
    • Command Pattern
    • Composite Pattern
    • Decorator Pattern
    • Facade Pattern
    • Observer Pattern
    • Proxy Pattern
  • Prerequesites - You Must Understand:
    • Specialization and Inheritance
    • Interfaces
    • Abstract Classes
Decorator - Motivation
Back SMYC Forward
  • Adding Capabilities to a Class:
    • Specialization
  • Adding Capabilities to an Object:
    • Needs to be done "dynamically" (i.e., at run time)
Decorator - The Traditional Approach
Back SMYC Forward
decorator_pattern
Decorator - A Generic Application
Back SMYC Forward
decorator_pattern_sequence-diagram
Decorator - An Example
Back SMYC Forward
decorator_pattern_printer-example
Decorator - An Example (cont.)
Back SMYC Forward
javaexamples/decorator/Printer.java
 
javaexamples/decorator/ConsolePrinter.java
 
javaexamples/decorator/PrinterDecorator.java
 
javaexamples/decorator/UppercasePrinter.java
 
javaexamples/decorator/WrappingPrinter.java
 
javaexamples/decorator/Driver.java
 
There's Always More to Learn
Back -