C SC 225 Lecture 5: Introduction to Design Patterns
[ previous
| schedule
| next ]
The Design Pattern movement in software engineering
- Part of the Extreme Programming and Agile Process movements
- Inspired by Christopher Alexander's patterns for architectural design
- Codified by Design Patterns: Elements of Reusable Object-Oriented Software , written by the "Gang of Four" (1995)
- Gang of Four (GoF) = Erich Gamma (co-writer of JUnit), Richard Helm, Ralph Johnson, and the late John Vlissides
- See our "I" drive for more information: I:\C SC\Design Patterns
- Patterns are a form of "best practices" for Object-Oriented Design
Introduction to Design Patterns
- Patterns address recurring situations
- A design pattern has four essential elements:
- Name - something simple yet descriptive we can use to refer to it
- Problem - description of the situation and context where the pattern applies
- Solution - the design, relationships, responsibilities, and collaborations
- Consequences - results and trade-offs of using the pattern
- Horstmann boils these down to context and solution
- Design patterns of object-oriented design fall into three broad categories:
- Creational - having to do with class and object creation
- Structural - having to do with class and object composition
- Behavioral - having to do with communication between objects
- The complete GoF collection (only a few have been added since):
- Creational: Abstract Factory, Builder, Factory Method, Prototype, Singleton
- Structural: Adapter, Bridge, Composite, Decorator, Facade, Proxy
- Behavioral: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Flyweight,
Observer, State, Strategy, Template Method, Visitor
- We will study selected patterns from all three categories
- You will recognize some of them from previous experience with the Java API
- Some overarching themes will quickly emerge as we study these patterns
Iterator pattern
- Horstmann starts with this, Head First tackles it later.
- Easy place to start since we used them extensively in CSC 205 and Java 5's
"foreach" construct uses them implicitly.
- Java Collections Framework iterators were based on this pattern
- We didn't study "bad" iteration techniques, but Horstmann shows two:
- Give client access to the aggregate's elements themselves, and client handles the traversal from
one array or linked element to the next. In addition to exposing the fields, it also exposes the implementation technique
- Aggregate maintains a cursor (an internal iterator). This protects fields
and implementation, but allows only one client at a time to iterate, plus
cursor movement has side-effects on the aggregate.
- The pattern's solution will seem obvious to you since you have already studied and used it
- Iterator is a behavioral pattern
Strategy pattern
- Head First starts with this, Horstmann introduces it third
- Define an interface (in the Java sense) to represent an algorithm that may vary
- The algorithm itself is abstract
- The interface represents the strategy
- For each concrete variation, define a class that implements the interface and its method(s)
- The client supplies its concrete object to the supplier class
- Supplier will invoke correct behavior through polymorphism
- Horstmann illustrates through Java AWT/Swing layout managers
- The interface is java.awt.LayoutManager
- The supplier (context) is a java.awt.Container
- Client gives the container an object created from one of LayoutManager's implementing classes
- The container invokes layout behaviors when required by calling methods specified in the interface.
Through polymorphism, the method supplied by the client through its object is invoked.
- Head First presents several inferior ways to provide varying behaviors. We will also study these.
- Strategy is a behavioral pattern
[ C
SC 225 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)