Professional Documents
Culture Documents
Douglas C. Schmidt
Douglas C. Schmidt
Professor d.schmidt@vanderbilt.edu www.cs.wustl.edu/schmidt/ Department of EECS Vanderbilt University (615) 343-8197
Vanderbilt University
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Developing reusable software is even harder Proven solutions include patterns and frameworks www.cs.wustl.edu/schmidt/ patterns.html
)6 )
)6 )
+ 756-4 * * 56 4-
Vanderbilt University
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Patterns of Learning
Successful solutions to many areas of human endeavor are deeply rooted in patterns In fact, an important goal of education is transmitting patterns of learning from generation to generation In a moment, well 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!
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
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Design Patterns
Design patterns represent solutions to problems that arise when developing software within a particular context i.e., Pattern == problem/solution pair in a context Patterns capture the static and dynamic structure and collaboration among key participants in software designs They are particularly useful for articulating how and why to resolve non-functional forces Patterns facilitate reuse of successful software architectures and designs
However, to become a master of software design, one must study the designs of other masters
These designs contain patterns that must be understood, memorized, and applied repeatedly
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Key Forces 1. There may be many observers 2. Each observer may react differently to the same notication
Intent
>IAHLAH KF@=JA
>IAHLAHI
56 + 37 6-5
IK> A?J
Dene a one-to-many dependency between objects so that when one object changes state, all its dependents are notied and updated automatically.
*5-48-45
Vanderbilt University
Vanderbilt University
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Graphical Notation
24 +-55 604-),
+ ?HAJA 5K> A?J
Push architectures combine control ow and data ow Pull architectures separate control ow from data ow
6- 2 )6+ )55
1 0-4165
6)1
- 6
Vanderbilt University
10
Vanderbilt University
11
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Frameworks
1. Frameworks are semi-complete applications Complete applications are developed by inheriting from, and instantiating parameterized framework components 2. Frameworks provide domain-specic functionality
Pattern descriptions are often independent of programming language or implementation details Contrast with frameworks
e.g., business applications, telecommunication applications, window systems, databases, distributed applications, OS kernels
3. Frameworks exhibit inversion of control at run-time
i.e., the framework determines which objects and methods to invoke in response to events
13
OO Patterns
)22 1+)61 52-+1.1+ .7 +61 ) 16;
+) 1 8 +)61 5
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Denition
),6
+ )55-5 ,)6)*)5+ )55-5
Class libraries
Self-contained, pluggable ADTs
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 The framework provides the common template methods and the application provides the variant hook methods
-8- 6 2
/ 7+ ,-
/71
+ )55-5
-69 4
12+
+ )55-5
)+
)55 1*4)4;)4+016-+674-69 4 1 /
-8- 6 2
Frameworks
Reusable, semi-complete applications
-8- 6 0) , -45
?= >=?
,152)6+0-4 -8- 6 2
),6
+ )55-5
)22 1+)61
1 8 )60 + )55-5 -5
/71
+) *)+ 5 -8- 6 2
Patterns
Problem, solution, context
,)6)*)5-
-8- 6 2
*.4)
-9 4 )4+016-+674-
Vanderbilt University
14
Vanderbilt University
15
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Active Object
In general, sophisticated frameworks embody dozens of patterns and patterns are often used to document frameworks
Vanderbilt University 16 Vanderbilt University 17
OO Patterns
Factory Method
Method in a derived class creates associates
Abstract Factory
Factory for building related objects
Builder
Factory for building complex objects incrementally
Prototype
Factory for cloning new instances from a prototype
Singleton
Factory for a singular (sole) instance
Vanderbilt University 18
Strategy
Tilde Expander
i.e., neither is subordinate Patterns can be characterized as more abstract descriptions of frameworks, which are implemented in a particular language
Event Dispatcher
Structural patterns
Deal with decoupling interface and implementation of classes and objects
Acceptor
Behavioral patterns
Deal with dynamic interactions among societies of classes and objects
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Creational Patterns
Adapter
Structural Patterns
Bridge
Abstraction for binding one of many implementations
Composite
Structure for building recursive aggregations
Decorator
Decorator extends an object transparently
Vanderbilt University
19
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Behavioral Patterns
Chain of Responsibility
Request delegated to the responsible service provider
Flyweight
Many ne-grained objects shared efciently
Command
Request as rst-class object
Proxy
One object approximates another
Interpreter
Language interpreter for a small grammar
Iterator
Aggregate elements are accessed sequentially
Vanderbilt University
20
Vanderbilt University
21
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Memento
Snapshot captures and restores object states privately
Template Method
Algorithm with some steps supplied by a derived class
Observer
Dependents update automatically when a subject changes
Visitor
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
Teach
It must provide sufcient understanding to tailor the solution
Have a context,
It must describe where the solution can be used
Have a name
It must be referred to consistently
Recur,
It must be relevant in other situations
Vanderbilt University
25
Do
572-4 815 4
+- 6-4 ) )/-4
System Overview
572-4 815 4
OS platforms are often fundamentally incompatible e.g., different concurrency and I/O models Thus, it may be impractical to directly reuse:
572-4 815 4
-69 4
Vanderbilt University
26
Vanderbilt University
572-4 815 4
-8- 6 5-48-4
5916+0 0) , -45
+)
,
6- -+ 5916+0-5
www.cs.wustl.edu/schmidt/PDF/ECOOP95.pdf
-41+55
,
-41+55
4A=?J H
www.cs.wustl.edu/schmidt/PDF/DSEJ-94.pdf
OO Patterns
Douglas C. Schmidt
572-4815 4 0) , -45
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
OO framework was rst developed on UNIX and later ported to Windows NT 3.51 in 1993 UNIX and Windows NT have fundamentally different I/O models i.e., synchronous vs. asynchronous Thus, direct reuse of original framework was infeasible Later solved by ACE and Windows NT 4.0
WaitForCompletion()
I/O Subsystem
Patterns embody successful solutions to problems that arise when developing software in a particular context Patterns reduced project risk by leveraging proven design expertise
4A=?J H
+ HO F IEJA .=?J AJD @
2H NO 9H=FFAH .=?=@A
Vanderbilt University
28
Vanderbilt University
29
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Intent
D= @ AHI M I
1 161) 1 )61 ,-
0= @ A
JEBEAI
Note inversion of control Also note how long-running event handler callbacks can degrade quality of service
-8- 60) , 1 / ,-
www.cs.wustl.edu/schmidt/POSA/
+ -) 72
Vanderbilt University
30
Vanderbilt University
31
Do
OO Patterns
Douglas C. Schmidt
int main (int argc, char *argv[]) { My_Event_Handler eh; ACE_Reactor reactor;
/* NOTREACHED */ return 0;
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Vanderbilt University
35
Vanderbilt University
OO Patterns
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Key Principles
Successful patterns and frameworks can be boiled down to a few key principles: 1. Separate interface from implementation 2. Determine what is common and what is variable with an interface and an implementation Common == stable 3. Allow substitution of variable implementations via a common interface Dividing commonality from variability should be goal-oriented rather than exhaustive
Vanderbilt University
36
Vanderbilt University
37
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Vanderbilt University
38
Vanderbilt University
39
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Vanderbilt University
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Patterns explicitly capture expert knowledge and design tradeoffs, and make this expertise more widely available Patterns help improve developer communication
Pattern names form a vocabulary
Vanderbilt University
42
Vanderbilt University
43
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Institutionalize rewards for developing patterns Directly involve pattern authors with application developers and domain experts Clearly document when patterns apply and do not apply Manage expectations carefully
Drawbacks of frameworks
High initial learning curve Many classes, many levels of abstraction The ow of control for reactive dispatching is non-intuitive Verication and validation of generic components is hard
Vanderbilt University
44
Vanderbilt University
45
OO Patterns
Douglas C. Schmidt
OO Patterns
Douglas C. Schmidt
Vanderbilt University
46
Vanderbilt University
47
OO Patterns
Douglas C. Schmidt
Summary
Mature engineering disciplines have handbooks that describe successful solutions to known problems e.g., automobile designers dont design cars using the laws of physics, they adapt adequate solutions from the handbook known to work well enough The extra few percent of performance available by starting from scratch typically isnt worth the cost Patterns can form the basis for the handbook of software engineering If software is to become an engineering discipline, successful practices must be systematically documented and widely disseminated
Vanderbilt University 48