Title: GSLT: Design Patterns
1GSLT Design Patterns
- Lars Degerstedt
- Linköping university, IDA
- larde_at_ida.liu.se
2Overview
- 1st hour Ex. of Design Patterns
- the concept
- Examples
- creational Singleton and Factory.
- behavioral Objectifier and Iterator.
- 2nd hour the GoF Catalogue
3Literature
Books
Design Patterns, Gamma et al, Addison Wesley,1994
Patterns in Java, Vol. 1, M Grand, 1998
Object-oriented Design and Patterns W. Pree,
1995, Addision-Wesley (Out of Print)
4What is a Design Pattern?
- Pattern problemsolutioncontext.
- A new level of abstraction
- not algorithmic,
- not module-level,
- not a system architecture.
- GoF
- reuse of micro-architecture.
- not a new concept - a catalogue!
5Ex The MVC Design Pattern
Model
Change of state
Subscribe/notify
View
Controller
User-event-handling
6The MVC GUI Component
a
b
c
30
10
60
x
30
20
50
y
10
10
80
z
a
b
c
7Features of MVC Components
- Separation of model-view
- serveral views
- change a view without changing the model.
- Compound Views
- composition of views is easy.
- Separation of view-controller
- change user-event-handling without changing the
view.
8Singleton
- Ensure one (n) instance(s) of a class.
- The instance should be accessible for all client
of the class. - Examples of use
- Debug or configuration utilities.
- Database or parser server manager.
- Java.lang.Runtime - access to OS.
9Class diagram Singleton
Singleton
static Singleton() private Singleton (first
bool) operation() getDaten()
if uniqueInstance null then
uniqueInstance new Singleton (true) return
uniqueInstance
static uniqueInstance data
10Pros and Cons Singleton
- Features
- improvment over global variables.
- More flexible than class operations.
- Potential problems
- how to subclass a singleton?
- threads one singleton per thread?
- Alternatives
- extension use a registry Singleton of
singletons. - pass as (compound) parameter.
11NLP Relevance Singleton
- A singleton manager class for each module in a
dialogue system. - A jungle of singletons for different data
sources - Problems when split into several processes - one
singleton per process.
12Factory
- How to create product objects through a
generic interface. - Manufacture custom compositions, shared
one-of-a-kind etc. - Avoids specifying the concrete classes of the
products. - Ex different look-and-feel of widgets.
13Factory Class Diagram
Client
AbstractFactory
Abstract product A
createProductA() createProductB()
ProdA2
ProdA1
Concrete Factory1
Concrete Factory2
Abstract product B
createProductA() createProductB()
createProductA() createProductB()
ProdB2
ProdB1
14Factory Pros and Cons
- Features
- isolates concrete classes the factory creates
new instances - abstract names in client code. - change of product families easy.
- consistency among products work on one product
family at the time. - Adding new products is difficult
- solution parametrisation of product name.
15Factory NLP Relevance
- JAXP abstract Java representations for XML
documents. - General abstract APIs for e.g. Interpretations.
- GATE2 suggests to organize NLP representations
in a taxonomy.
16Objectifier
- Separation of abstraction from implementation.
- Subclasses cannot change during runtime.
- Objectify the varying behavior.
- Use when a large amount of code is conditional.
- Ex listeners in Java Swing MVC.
17Objectifier Class Diagram
Objectifier
Client
objectifierInterface()
getObjectifier()
ConcreteObjectifier A
ConcreteObjectifier B
ConcreteObjectifier C
objectifierInterface()
objectifierInterface()
obectifierInterface()
18Objectifier Pros and Cons
- Features
- Encapsulation and customisation.
- Composition instead of inheritance increases
reuse. - Stateless objectifiers can be shared.
- Potential problems
- additional level of indirection.
- choice of objectifier client can be exposed to
implementations issues.
19Objectifier NLP Relevance
- Dialogue Manager e.g. the focus algorithm.
- Generation handler for text-frames with
variables. - Barge-in-handler extraction of different
barge-in strategies.
20Iterator
- Access elements of a collection sequentially.
- Does not expose underlying structure.
- Uses the basic pattern Objectifier.
- E.g. The interface java.util.Iterator
- Can be combined with Composite.
21Iterator Class Diagram
Iterator
Client
AbstractList
First() next() previous() (opt) hasNext() ...
createIterator() count() append(Item) remove(Item)
ArrayList
LinkedList
LinkedListIterator
ArrayListIterator
22Iterator Pros and Cons
- Features
- supports variations in traversals
- simplifies the aggregate interface
- supports parallel iteration of an aggregate.
- Potential problems
- changing aggregate during iteration
- high-order iterative constructs map-car?
23Iterator NLP Relevance
- Traversal of a Dialogue tree.
- Traversal of feature structures
- Traversal of lexicon and grammar representations.
- Traversal of Charts
24Overview (2nd hour)
- 1st hour what are Design Patterns?
- 2nd hour the GoF Catalogue
- GoF for problem-solving
- History and Frameworks
- Patterns of Java Swing
- The Adapter Pattern
- The Observer Pattern
- how to use patterns
25GoF problem-solving categories
- Jurisdiction the related oo concept
- class static semantics.
- object relations of peer objects.
- compound recursive object structures
- Characterization - what it does
- creational object creation.
- structural composition of entities.
- behavioral interaction and responsiblity.
26The GoF Catalogue
Characterization
Jurisdiction
27History
- Object-Oriented Patterns late 70s.
- e.g. Smalltalks Model-View-Controller
- OO Frameworks i general
- how handle the complexity for the user?
- A Pattern Lanuage (Alexander 77)
- 91 workshop towards an architecture handbook...
- E. Gamma Ph D Thesis (92) E
28OO Frameworks
- Component a static unit of deployment. Often a
module. - OO Framework partial design of a (class of)
system(s) - white-box
- user must learn the code.
- based on inheritance.
- black-box
- based on composition of components.
29Purpose of Design Patterns
- For components only
- basic patterns.
- used by more complex patterns.
- Explaining existing frameworks
- conceptual view faciliates usage.
- Creates a common design language.
- Generating new frameworks
- more complex patterns.
- Cornerstones of system architectures.
30Coding Patterns
- Applied design patterns
- closer to code level.
- only meaningful for a particular language.
- include programming standards.
- Features
- demonstrate how to combine the language concepts.
- give implementations of design patterns for a
language.
31Patterns of Java Swing
Model
View
Controller
UI
- JComponent parts model and UI.
- Programmers work with the JComponents
- a glue that holds the MVC together.
- JComponent actually extends the (AWT) Components
32Ex The JButton Component
- JButton ButtonModel ButtonUI
- Jbutton methods setModel and setUI.
- API ButtonModel
- describes the internal state of the JButton.
- query, manipulate, add listeners, fire events
- API ButtonUI
- describes the view and controller of JButton.
- paint, geometric info, handle events
33Ex (cont) ButtonUI VC
- V - methods in API
- install, paint, paint pressed,
- C - BasicButtonUIListener
- controller part of the BasicButtonUI
- a compound listener for mouse, mouse motion, and
state change.
34Observer
- One-to-many relation between subject and
observer objects - When the subject changes state, all observers are
notified and updated automatically - Indirect interfacing the subject does not know
what objects are listening - Independence between two aspects the subject and
observer aspects - Ex java.awt.event.MouseListener is an Observer
of the subject class java.awt.Component.
35Observer Class Diagram
Observers
Subject
Observer
attach(Observer) Detach(Observer) notify()
Update()
observerState subject-gtgetState()
For all o in observers o-gtUpdate()
ConcreteObserver
ConcreteSubject
Update()
getState() setState()
observerState
subjectState
36Adapter
- Convert interface of a class into an interface
the clients expect - Cooperation with unforseen classes
- You want to use an existing class but the
interface is wrong - Ex java.awt.event.MouseAdapter is an empty
implementation of MouseListener
37Adapter Class Diagram
Adaptee
Client
Target
SpecRequest()
Request()
Adapter
Adaptee -gt specRequest()
Request()
38GoF disassembling MVC
MVC observer composite strategy
- Observer generalises the pattern of the
model-view - Composite generalises the pattern of composite
views - Strategy generalises the extraction of
controller from the view
39How Patterns Solve Problems
- Finding appropriate objects.
- Determining object granularity.
- Specifying object Interfaces.
- Specifying object implementations.
- Putting reuse mechanisms to work.
- Relating run-time and compile-time structures.
- Designing for change.
40Reuse Your Design (Vlissedes 1996)
- Habit 1 taking time to reflect.
- Habit 2 adherence to a structure.
- Habit 3 being concrete early.
- Habit 4 keeping Patterns distinct
complementary.
- Habit 5 presenting Effectively.
- Habit 6 iterating tirelessly.
- Habit 7 collecting and incorporating feedback.
41A Layered Catalogue (Zimmer)
- Basic patterns
- heavily used simple patterns.
- basic design rather than patterns.
- Generic patterns
- for typical software problems.
- Not part of the basic patterns.
- Domain specific patterns
- e.g. patterns for compiler construction
42Relations between Patterns Zimmer PLOP 1
43Summing up
Design Patterns for NLP an open field!
- Design patterns
- are a catalogue of micro-architectures.
- can be combined into frameworks.
- form a design language for system (macro)
architectures. - Gamma is one of the software pioneers
- Dijkstra, Brooks, Hoare,,and Gamma!
- This is important stuff!! ?