January 12, 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, 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, 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, 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, A Brief History of Design Patterns Christopher Alexander pens The Timeless Way of Building –Building Towns for Dummies –Had nothing to do with software 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, What’s In a Design Pattern The GoF book describes a pattern using the following four attributes: nameThe name to describes the pattern, its solutions and consequences in a word or two problemThe problem describes when to apply the pattern solutionThe solution describes the elements that make up the design, their relationships, responsibilities, and collaborations are the results and trade-offs in applying the patternThe consequences are the results and trade-offs in applying the pattern All examples in C++ and Smalltalk
January 12, What’s In a Design Pattern 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, Grand’s Classifications of Design Pattern Fundamental patterns Creational patterns Partitioning patterns Structural patterns Behavioral patterns Concurrency patterns
January 12, Fundamental Design Patterns Delegation—when not to use Inheritance Interface Abstract superclass Interface and abstract class Immutable Marker interface Proxy
January 12, 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, Creational Patterns Factory method Abstract Factory Builder Prototype Singleton Object pool
January 12, 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, Partitioning Patterns Filter Composite Read-only interface
January 12, 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, Structural Patterns Adapter Iterator Bridge Façade Flyweight Dynamic linkage Virtual proxy Decorator Cache management
January 12, Structural Pattern Functions “Describe common ways that different types of objects can be organized to work with each other”– (Grand 207)
January 12, Behavioral Patterns Chain of responsibility Command Little language Mediator Snapshot Observer State Null object Strategy Template method Visitor
January 12, Behavioral Pattern Functions These “…patterns are used to organize, manage, and combine behavior” – (Grand 303)
January 12, 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, Concurrency Pattern Functions These patterns involve coordinating concurrent operations and address two primary problems –Shared resources –Sequence of operations
January 12, 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, Further Reading ses.htmhttp:// ses.htm pats.htmlhttp:// pats.html