Lecture 08 Design Patterns

Joseph Haugh

University of New Mexico

Free Recall

  • Get out a sheet of paper or open a text editor
  • For 2 minutes write down whatever comes to mind about the last class
    • This could be topics you learned
    • Questions you had
    • Connections you made

What is a Design Pattern?

  • A pattern is a solution to a problem in a context.
    • context - recurring situation in which pattern applies
    • problem - goal to achieve plus any constraints
    • solution - general design which resolves problem
  • Language for communicating solutions with others
  • Pattern languages exist for many problems, but we focus on design

References

  • Design Patterns: Elements of Reusable Object-Oriented Software
    • Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides
    • Gang of Four (GoF)
  • Head First Design Patterns
    • Available free online with edu email
  • Portland Pattern Repository

Caution!

  • Design patterns are not a substitute for thought
  • Class names and directory structures do not equal good design
  • Design patterns have tradeoffs
    • For example, the mediator pattern does not remove complexity in interactions but just provides a structure for centralizing it
  • Design patterns depend on the programming language
    • Certain language restrictions may necessitate certain patterns, e.g., patterns related to object creation and destruction

Motivation for Design Patterns

  • Provide an abstraction of the design experience
    • Can often serve as a reusable base of experience
  • Provide a common vocabulary for discussing complete system designs
  • Reduce system complexity by naming abstractions
    • increasing program comprehension
    • reducing learning time for a new piece of code
  • Provide a target for the reorganization or refactoring of class hierarchies

Parts of a Design Pattern Description

  • Pattern name and classification
  • Intent (Justification)
  • Applicability
  • Structure
  • Participants and Collaborations
  • Implementation
  • Sample Code
  • Known usages
  • Related Patterns

How to Use Design Patterns

  • Keep it simple
  • Use a pattern when it solves a problem in your design
  • Refactor with patterns
  • Don’t be afraid to remove patterns
  • Don’t add unnecessary complexity
  • Focus on design, not on patterns