Beruflich Dokumente
Kultur Dokumente
These lecture slides are copyright (C) Marty Stepp, 2007. They may not be rehosted,
sold, or modified without expressed permission from the author. All rights reserved.
1
Big questions
Design challenges
Design patterns
design pattern:
a solution to a common software problem in a context
History of patterns
Creational Patterns
Factory Method
Builder
Abstract Factory
Prototype
Singleton
Structural Patterns
Adapter
Decorator
Proxy
Bridge
Facade
Composite
Flyweight
Behavioral Patterns
Command
Mediator
Strategy
Template Method
Interpreter
Observer
Chain of Responsibility
Iterator
State
Visitor
Pattern: Iterator
objects that traverse collections
Iterator pattern
map.values().iterator()
Pattern: Observer
objects whose state can be watched
10
11
Model-view-controller
Model
View
Component
data for
renderin
g
events
updates
Model
View
Controller
Controll
12
Observer pattern
13
Benefits of observer
15
Observer interface
package java.util;
public interface Observer {
public void update(Observable o, Object arg);
}
16
Observable class
17
have the panels take action when the model notifies them of
events (e.g. repaint, play sound, show option dialog, etc.)
18
give each its own unique inner components and code to draw
the model's state in its own way
19
20
Pattern: Strategy
objects that hold alternate algorithms to solve a
problem
21
Strategy pattern
examples:
file saving/compression
layout managers on GUI containers
AI algorithms for computer game players
22
// uses strategy
23
24
Container
container: an object that holds components; it also
governs their positions, sizes, and resize behavior
Pattern: Composite
objects that can serve as containers, and can hold
other objects like themselves
26
Composite pattern
<node>
::= <leafnode> | <compositenode>
<compositenode> ::= <node>*
examples in Java:
27
28
Pattern: Decorator
objects that wrap around other objects to add
useful features
29
Decorator pattern
examples in Java:
30
31
32
References
http://c2.com/cgi/wiki?IteratorPattern
http://c2.com/cgi/wiki?DecoratorPattern
http://c2.com/cgi/wiki?CompositePattern
http://www.patterndepot.com/put/8/JavaPatterns.htm
33