January 12, 2009 1 Introduction to Design Patterns Tim Burke References: –Gamma, Erich, et. al....

22
January 12, 2009 1 Introduction to Design Patterns Tim Burke References: Gamma, Erich, et. al. (AKA, The Gang of Four). Design Patterns: Elements of Reusable Object – Oriented Software. Upper Saddle River, NJ: Addison-Wesley, 1994 Grand, Mark. Patterns in Java, Vol. 1 . Indianapolis: Wiley, 2002

Transcript of January 12, 2009 1 Introduction to Design Patterns Tim Burke References: –Gamma, Erich, et. al....

January 12, 20091

Introduction to Design Patterns

Tim Burke

References:–Gamma, Erich, et. al. (AKA, The Gang of Four). Design Patterns: Elements of Reusable Object – Oriented Software. Upper Saddle River, NJ: Addison-Wesley, 1994–Grand, Mark. Patterns in Java, Vol. 1. Indianapolis: Wiley, 2002

January 12, 20092

Overview

• What’s a design pattern?

• Why design patterns?

• Brief History of design patterns

• What’s in a design pattern

• Grand’s general classifications of design patterns

• Synopsis of specific design patterns

• Summary

• Further Reading

January 12, 20093

What’s a Design Pattern?• “[A] pattern describes a problem which occurs

over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever using it the same way twice.” – (Christopher Alexander quoted by Gamma, et. al. 2)

• “Reusable solutions to recurring problems that we encounter during software development.” – (Grand 1)

January 12, 20094

Why Design Patterns?• “Designing object-oriented code is hard, and

designing reusable object-oriented software is even harder.” – (Gamma, et.al 1)

• Patterns enable programmers to “…recognize a problem and immediately determine the solution without having to stop and analyze the problem first.” -- (Grand 1)

• Provides a framework for communicating complexities of OO design at a high level of abstraction

• Bottom line: productivity

January 12, 20095

A Brief History of Design Patterns

• 1979--Christopher Alexander pens The Timeless Way of Building– Building Towns for Dummies

– Had nothing to do with software

• 1994--Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (the Gang of Four, or GoF) publish Design patterns: Elements of Reusable Object-Oriented Software– Capitalized on the work of Alexander

– The seminal publication on software design patterns

January 12, 20096

What’s In a Design Pattern--1994• The GoF book describes a pattern using the following

four attributes:• The namename to describes the pattern, its solutions and

consequences in a word or two

• The problemproblem describes when to apply the pattern

• The solutionsolution describes the elements that make up the design, their relationships, responsibilities, and collaborations

• The consequences are the results and trade-offs in are the results and trade-offs in applying the patternapplying the pattern

• All examples in C++ and Smalltalk

January 12, 20097

What’s In a Design Pattern--2002• Grand’s book is the latest offering in the field and is very

Java centric. He develops the GoF attributes to a greater granularity and adds the Java specifics• Pattern name—same as GoF attribute

• Synopsis—conveys the essence of the solution

• Context—problem the pattern addresses

• Forces—reasons to, or not to use a solution

• Solution—general purpose solution to the problem

• Implementation—important considerations when using a solution

• Consequences—implications, good or bad, of using a solution

• Java API usage—examples from the core Java API

• Code example—self explanatory

• Related patterns—self explanatory

January 12, 20098

Grand’s Classifications of Design Pattern

• Fundamental patterns

• Creational patterns

• Partitioning patterns

• Structural patterns

• Behavioral patterns

• Concurrency patterns

January 12, 20099

Fundamental Design Patterns

• Delegation—when not to use Inheritance

• Interface

• Abstract superclass

• Interface and abstract class

• Immutable

• Marker interface

• Proxy

January 12, 200910

Fundamental Design Pattern Functions

• “The most fundamental and important design patterns to know”– (Grand 51)

– Most other patterns user at least one of these

– So ubiquitous that they’re often not mentioned

January 12, 200911

Creational Patterns

• Factory method

• Abstract Factory

• Builder

• Prototype

• Singleton

• Object pool

January 12, 200912

Creational Pattern Functions

• Provides guidance on how to create objects when their creation requires making decisions

– “Decisions typically involve dynamically deciding which class to instantiate or which objects an object will delegate responsibility to”— (Grand 101)

– Value is to tell us how to structure and encapsulate the decisions

January 12, 200913

Partitioning Patterns

• Filter

• Composite

• Read-only interface

January 12, 200914

Partitioning Pattern Functions

• Follows the divide and conquer paradigm

– “Provide guidance on how to partition classes and interfaces in ways that make it easier to arrive at a good design”– (Grand 175)

January 12, 200915

Structural Patterns• Adapter

• Iterator

• Bridge

• Façade

• Flyweight

• Dynamic linkage

• Virtual proxy

• Decorator

• Cache management

January 12, 200916

Structural Pattern Functions

• “Describe common ways that different types of objects can be organized to work with each other”– (Grand 207)

January 12, 200917

Behavioral Patterns• Chain of responsibility

• Command

• Little language

• Mediator

• Snapshot

• Observer

• State

• Null object

• Strategy

• Template method

• Visitor

January 12, 200918

Behavioral Pattern Functions

• These “…patterns are used to organize, manage, and combine behavior” – (Grand 303)

January 12, 200919

Concurrency Patterns• Single threaded execution

• Lock object

• Guarded suspension

• Balking

• Scheduler

• Read/Write lock

• Producer-consumer

• Two-phase termination

• Double buffering

• Asynchronous processing

• Future

January 12, 200920

Concurrency Pattern Functions

• These patterns involve coordinating concurrent operations and address two primary problems

– Shared resources

– Sequence of operations

January 12, 200921

Summary

• Patterns enable programmers to “…recognize a problem and immediately determine the solution without having to stop and analyze the problem first.” -- (Grand 1)

• Patterns provide a framework for communicating the complexities of OO design at a high level of abstraction

January 12, 200922

Further Reading• http://www.mindspring.com/~mgrand/pattern_synop

ses.htm

• http://hillside.net/patterns/

• http://hem.passagen.se/gumby/cs/patterns.html

• http://www.swtech.com/dpattern/

• http://www.enteract.com/~bradapp/links/sw-pats.html