Patterns support reuse of software architecture and design. – Patterns ...
applications. • Together, design patterns and frameworks help to improve
software.
OO Patterns
Douglas C. Schmidt
Patterns and Frameworks
Introduction to Patterns and Frameworks
Motivation for Patterns and Frameworks
Douglas C. Schmidt Professor
[email protected] www.cs.wustl.edu/schmidt/
What is a Pattern? A Framework?
Department of EECS Vanderbilt University (615) 343-8197
Pattern Categories Pattern Examples
Vanderbilt University
OO Patterns
Douglas C. Schmidt
Motivation for Patterns and Frameworks
DIAGNOSTIC STATIONS
ATM MAN ATM LAN
MODALITIES (CT, MR, CR)
Vanderbilt University
Patterns support reuse of software architecture and design
Developing reusable software is even harder
ATM LAN
CLUSTER BLOB STORE
Douglas C. Schmidt
Overview of Patterns and Frameworks
Developing software is hard
DX BLOB STORE
OO Patterns
1
– Patterns capture the static and dynamic structures and collaborations of successful solutions to problems that arise when building applications in a particular domain
Proven solutions include patterns and frameworks
Frameworks support reuse of detailed design and code
www.cs.wustl.edu/schmidt/ patterns.html
– A framework is an integrated set of components that collaborate to provide a reusable architecture for a family of related applications Together, design patterns and frameworks help to improve software quality and reduce development time
CENTRAL BLOB STORE
– e.g., reuse, extensibility, modularity, performance 2
Vanderbilt University
3
OO Patterns
Douglas C. Schmidt
OO Patterns
Patterns of Learning
Douglas C. Schmidt
Becoming a Chess Master First learn the rules
Successful solutions to many areas of human endeavor are deeply rooted in patterns
– e.g., names of pieces, legal movements, chess board geometry and orientation, etc.
– In fact, an important goal of education is transmitting patterns of learning from generation to generation
Then learn the principles
In a moment, we’ll explore how patterns are used to learn chess Learning to develop good software is similar to learning to play good chess
– Though the consequences of failure are often far less dramatic!
– e.g., relative value of certain pieces, strategic value of center squares, power of a threat, etc. However, to become a master of chess, one must study the games of other masters
– These games contain patterns that must be understood, memorized, and applied repeatedly There are hundreds of these patterns
Vanderbilt University
OO Patterns
4
Douglas C. Schmidt
Vanderbilt University
5
OO Patterns
Douglas C. Schmidt
Design Patterns
Becoming a Software Design Master First learn the rules
Design patterns represent solutions to problems that arise when developing software within a particular context
– e.g., the algorithms, data structures and languages of software
– i.e., “Pattern == problem/solution pair in a context”
Then learn the principles
Patterns capture the static and dynamic structure and collaboration among key participants in software designs
– e.g., structured programming, modular programming, object oriented programming, generic programming, etc.
– They are particularly useful for articulating how and why to resolve non-functional forces
However, to become a master of software design, one must study the designs of other masters
Patterns facilitate reuse of successful software architectures and designs
– These designs contain patterns that must be understood, memorized, and applied repeatedly There are hundreds of these patterns Vanderbilt University
6
Vanderbilt University
7
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Example: Stock Quote Service SUBJECT MODEL
REAL-TIME MARKET DATA FEED
Structure of the Observer Pattern
Key Forces 1. There may be many observers
Subject notify() attach(observer) detach(observer)
2. Each observer may react differently to the same notification STOCK QUOTES
OBSERVERS
3. The subject should be as decoupled as possible from the observers
8
OO Patterns
Douglas C. Schmidt
Observer
1
update()
N observers
ION ICAT APPL ENDENT P INDE ION ICAT APPL NDENT DEPE
Concrete Subject
i.e., allow observers to change independently of the subject
Vanderbilt University
Intent
foreach o in observers loop o.update() end loop
subject
Concrete Observer update() subject->get_state()
get_state() subject_state_
return subject_state_
Vanderbilt University
9
OO Patterns
Graphical Notation
Douglas C. Schmidt
Collaboration in the Observer Pattern Concrete Subject
PROCESS
Concrete Observer 1
Concrete Variations Observer 2
set_state()
THREAD
“Push” architectures combine control flow and data flow “Pull” architectures separate control flow from data flow
notify()
OBJECT : CLASS
– Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
update()
TEMPLATE CLASS
get_state() update() get_state()
INHERITS
Vanderbilt University
CONTAINMENT
10
Vanderbilt University
11
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Frameworks
Design Pattern Descriptions Main parts
Pattern descriptions are often independent of programming language or implementation details
1. Name and intent 2. Problem and context 3. Force(s) addressed 4. Abstract description of structure and collaborations in solution
1. Frameworks are semi-complete applications Complete applications are developed by inheriting from, and instantiating parameterized framework components
Contrast with frameworks
2. Frameworks provide domain-specific functionality e.g., business applications, telecommunication applications, window systems, databases, distributed applications, OS kernels
5. Positive and negative consequence(s) of use
3. Frameworks exhibit inversion of control at run-time i.e., the framework determines which objects and methods to invoke in response to events
6. Implementation guidelines and sample code 7. Known uses and related patterns Vanderbilt University
12
OO Patterns
Douglas C. Schmidt
Class Libraries vs. Frameworks vs. Patterns
APPLICATIONSPECIFIC FUNCTIONALITY
LOCAL
MATH CLASSES
INVOCATIONS
ADT DATABASE CLASSES
CLASSES
GUI
GLUE
EVENT LOOP
NETWORK
CLASSES
CODE
LIBRARY ARCHITECTURE NETWORKING
LOOP
INVOKES
APPLICATIONSPECIFIC
GUI CALL EVENT LOOP
MATH CLASSES
(B) FRAMEWORK
Vanderbilt University
Component Integration in Frameworks
Class libraries
APPLICATION CODE
– Self-contained, “pluggable” ADTs Frameworks
EVENT HANDLER(S)
callback() DISPATCHER
Patterns
EVENT LOOP
Framework components are loosely coupled via callbacks Callbacks allow independently developed software components to be connected together Callbacks provide a connection-point where generic framework objects can communicate with application objects
BACKS
FUNCTIONALITY
DATABASE
13
Douglas C. Schmidt
Definition
– Reusable, “semi-complete” applications
EVENT
ADT CLASSES
OO Patterns
IPC
CLASSES
(A) CLASS
Vanderbilt University
– Problem, solution, context
EVENT SENSOR SYSTEM CODE
EVENT LOOP
– The framework provides the common template methods and the application provides the variant hook methods
ARCHITECTURE
14
Vanderbilt University
15
OO Patterns
Douglas C. Schmidt
OO Patterns
Comparing Patterns and Frameworks Patterns and frameworks are highly synergistic
State Singleton Cached Virtual Filesystem
Protocol Filter
Acceptor
Protocol Handler
Tilde Expander
/home/...
Event Dispatcher
Adapter
Protocol Pipeline Framework Streams
Concurrency Strategy Framework Service Configurator
Creational patterns
– Deal with initializing and configuring classes and objects
– i.e., neither is subordinate
~ Strategy
Asynchronous Completion Token
Active Object
Strategy
Reactor/Proactor I/O Strategy Framework
Douglas C. Schmidt
Design Pattern Space
Structural patterns
Patterns can be characterized as more abstract descriptions of frameworks, which are implemented in a particular language
– Deal with decoupling interface and implementation of classes and objects Behavioral patterns
– Deal with dynamic interactions among societies of classes and objects
In general, sophisticated frameworks embody dozens of patterns and patterns are often used to document frameworks Vanderbilt University
16
OO Patterns
Douglas C. Schmidt
Vanderbilt University
17
OO Patterns
Creational Patterns
Douglas C. Schmidt
Structural Patterns
Factory Method
Adapter
– Method in a derived class creates associates
– Translator adapts a server interface for a client
Abstract Factory
Bridge
– Factory for building related objects
– Abstraction for binding one of many implementations
Builder
Composite
– Factory for building complex objects incrementally Prototype
– Structure for building recursive aggregations Decorator
– Factory for cloning new instances from a prototype Singleton
– Decorator extends an object transparently
– Factory for a singular (sole) instance Vanderbilt University
18
Vanderbilt University
19
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Behavioral Patterns
Structural Patterns (cont’d)
Chain of Responsibility
Facade
– Request delegated to the responsible service provider
– Facade simplifies the interface for a subsystem
Command
Flyweight
– Request as first-class object
– Many fine-grained objects shared efficiently
Interpreter
Proxy
– Language interpreter for a small grammar
– One object approximates another
Iterator
– Aggregate elements are accessed sequentially
Vanderbilt University
20
OO Patterns
Douglas C. Schmidt
Vanderbilt University
21
OO Patterns
Behavioral Patterns (cont’d)
Douglas C. Schmidt
Behavioral Patterns (cont’d)
Mediator
Strategy
– Mediator coordinates interactions between its associates
– Abstraction for selecting one of many algorithms
Memento
Template Method
– Snapshot captures and restores object states privately
– Algorithm with some steps supplied by a derived class
Observer
Visitor
– Dependents update automatically when a subject changes
– Operations applied to elements of an heterogeneous object structure
State
– Object whose behavior depends on its state
Vanderbilt University
22
Vanderbilt University
23
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
When to Use Patterns
What Makes a Pattern a Pattern?
1. Solutions to problems that recur with variations
A pattern must: Solve a problem,
No need for reuse if the problem only arises in one context
Teach
– i.e., it must be useful!
2. Solutions that require several steps
Have a context,
Not all problems need all steps Patterns can be overkill if solution is simple linear set of instructions
– It must describe where the solution can be used
3. Solutions where the solver is more interested in the existence of the solution than its complete derivation
Have a name
– It must be referred to consistently
Recur,
– It must be relevant in other situations
Patterns leave out too much to be useful to someone who really wants to understand – They can be a temporary bridge, however 24
Vanderbilt University
25
Vanderbilt University
www.cs.wustl.edu/schmidt/PDF/DSEJ-94.pdf
TELECOM SWITCHES
EVENT SERVER
CALL CENTER MANAGER
SWITCH HANDLERS ERICSSON
www.cs.wustl.edu/schmidt/PDF/ECOOP95.pdf
26
OO framework for Call Center Management
Vanderbilt University
MD110
Algorithms Detailed designs Interfaces Implementations OO Patterns
– – – –
ERICSSON
Thus, it may be impractical to directly reuse:
MD110
– e.g., different concurrency and I/O models
NETWORK
OS platforms are often fundamentally incompatible
: Reactor
SUPERVISOR
System Overview
Developing portable, reusable, and efficient communication software is hard
SUPERVISOR
Case Study: A Reusable Object-Oriented Communication Software Framework
SUPERVISOR
Douglas C. Schmidt
SUPERVISOR
OO Patterns
SUPERVISOR HANDLERS
Do
Vanderbilt University
– It must provide sufficient understanding to tailor the solution
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Problem: Cross-platform Reuse OO framework was first developed on UNIX and later ported to Windows NT 3.51 in 1993
Protocol Handlers (HTTP)
I/O Completion Port
read() write() read() write() read() read() accept()
Patterns support reuse of software architecture
Publisher Subscriber Pipes & Filters
Layers
UNIX and Windows NT have fundamentally different I/O models
WaitForCompletion()
I/O Subsystem
Solution: Reuse Design Patterns
AcceptorConnector
– i.e., synchronous vs. asynchronous
Component Configurator
STRATEGIC PATTERNS
Thus, direct reuse of original framework was infeasible
Reactor
TACTICAL PATTERNS Iterator
Factory Composite Method
Proxy Wrapper Facade
Patterns embody successful solutions to problems that arise when developing software in a particular context Patterns reduced project risk by leveraging proven design expertise
– Later solved by ACE and Windows NT 4.0
Vanderbilt University
28
OO Patterns
Douglas C. Schmidt
Vanderbilt University
OO Patterns
The Reactor Pattern
1
Handle
N owns
Event Handler handle_event(type) get_handle()
Intent Decouples synchronous event demuxing & dispatching from event handling
notifies
Synchronous Event Demultiplexer select()
Concrete Event Handler
www.cs.wustl.edu/schmidt/POSA/
Forces Resolved
INITIALIZE
callback : main Reactor Concrete program Event_Handler Reactor()
REGISTER HANDLER EXTRACT HANDLE START EVENT LOOP FOREACH EVENT DO
Efficiently demux events synchronously within one thread Extending applications without changing demux infrastructure
Vanderbilt University
INITIALIZATION MODE
uses
handlers
select (handles); foreach h in handles loop table[h].handle_event(type) end loop
Douglas C. Schmidt
Collaboration in the Reactor Pattern
30
EVENT HANDLING MODE
Reactor handle_events() register_handler(h) remove_handler(h)
29
DATA ARRIVES OK TO SEND SIGNAL ARRIVES TIMER EXPIRES REMOVE HANDLER CLEANUP
Vanderbilt University
register_handler(callback) get_handle() handle_events() select() handle_input() handle_output()
Note inversion of control Also note how long-running event handler callbacks can degrade quality of service
handle_signal() handle_timeout() remove_handler(callback) handle_close()
31
OO Patterns
Douglas C. Schmidt
Differences Between UNIX and Windows NT
Vanderbilt University
/* NOTREACHED */ return 0;
for (;;) reactor.handle_events ();
Using ACE’s Reactor Pattern Implementation (cont’d)
OO Patterns
}
32
int main (int argc, char *argv[]) { My_Event_Handler eh; ACE_Reactor reactor;
Vanderbilt University
OO Patterns
#include "ace/Reactor.h" class My_Event_Handler : public ACE_Event_Handler { public: virtual int handle_input (ACE_HANDLE h) { cout