Introduction to Patterns and Frameworks
Total Page:16
File Type:pdf, Size:1020Kb
OO Patterns Douglas C. Schmidt Patterns and Frameworks Introduction to Patterns and Frameworks Motivation for Patterns and Frameworks Douglas C. Schmidt What is a Pattern? A Framework? Professor Department of EECS Pattern Categories [email protected] Vanderbilt University www.cs.wustl.edu/ schmidt/ (615) 343-8197 Pattern Examples Vanderbilt University 1 OO Patterns Douglas C. Schmidt OO Patterns Douglas C. Schmidt Motivation for Patterns and Frameworks Overview of Patterns and Frameworks DX Developing software is hard BLOB Patterns support reuse of software architecture and design STORE Developing reusable – Patterns capture the static and dynamic structures and ATM software is even harder collaborations of successful solutions to problems that arise when LAN DIAGNOSTIC STATIONS building applications in a particular domain Proven solutions include patterns and frameworks Frameworks support reuse of detailed design and code ATM www.cs.wustl.edu/ schmidt/ – A framework is an integrated set of components that collaborate MAN CLUSTER patterns.html to provide a reusable architecture for a family of related ATM BLOB LAN STORE applications Together, design patterns and frameworks help to improve software MODALITIES CENTRAL quality and reduce development time (CT, MR, CR) BLOB STORE – e.g., reuse, extensibility, modularity, performance Vanderbilt University 2 Vanderbilt University 3 OO Patterns Douglas C. Schmidt OO Patterns Douglas C. Schmidt Patterns of Learning 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 – e.g., relative value of certain pieces, strategic value of center In a moment, we’ll explore how patterns are used to learn chess squares, power of a threat, etc. Learning to develop good software is similar to learning to play good However, to become a master of chess, one must study the chess games of other masters – Though the consequences of failure are often far less dramatic! – These games contain patterns that must be understood, memorized, and applied repeatedly There are hundreds of these patterns Vanderbilt University 4 Vanderbilt University 5 OO Patterns Douglas C. Schmidt OO Patterns Douglas C. Schmidt Becoming a Software Design Master Design Patterns First learn the rules Design patterns represent solutions to problems that arise when – e.g., the algorithms, data structures and languages of software developing software within a particular context – i.e., “Pattern == problem/solution pair in a context” Then learn the principles – e.g., structured programming, modular programming, object Patterns capture the static and dynamic structure and collaboration oriented programming, generic programming, etc. among key participants in software designs – They are particularly useful for articulating how and why to However, to become a master of software design, one must study the designs of other masters resolve non-functional forces – These designs contain patterns that must be understood, Patterns facilitate reuse of successful software architectures and memorized, and applied repeatedly designs 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 Structure of the Observer Pattern Key Forces Intent SUBJECT REAL-TIME foreach o in observers loop MODEL o.update() MARKET 1. There may be many end loop Observer – Define a one-to-many DATA FEED observers 1Nupdate() dependency between Subject observers objects so that when one 2. Each observer may react notify() APPLICATION INDEPENDENT object changes state, all differently to the same attach(observer) detach(observer) APPLICATION its dependents are notification DEPENDENT notified and updated STOCK 3. The subject should be as Concrete automatically. QUOTES Observer decoupled as possible Concrete update() from the observers Subject subject OBSERVERS get_state() subject->get_state() i.e., allow observers to subject_state_ change independently of return subject_state_ the subject Vanderbilt University 8 Vanderbilt University 9 OO Patterns Douglas C. Schmidt OO Patterns Douglas C. Schmidt Graphical Notation Collaboration in the Observer Pattern Concrete Concrete Concrete Variations Subject Observer 1 Observer 2 PROCESS “Push” architectures combine THREAD set_state() control flow and data flow notify() “Pull” architectures separate control flow from data flow update() OBJECT TEMPLATE : CLASS CLASS get_state() update() get_state() INHERITS CONTAINMENT Vanderbilt University 10 Vanderbilt University 11 OO Patterns Douglas C. Schmidt OO Patterns Douglas C. Schmidt Design Pattern Descriptions Frameworks Main parts Pattern descriptions are often independent of 1. Frameworks are semi-complete applications 1. Name and intent programming language or Complete applications are developed by inheriting from, and 2. Problem and context implementation details instantiating parameterized framework components 3. Force(s) addressed Contrast with 2. Frameworks provide domain-specific functionality frameworks 4. Abstract description of structure and e.g., business applications, telecommunication applications, collaborations in solution window systems, databases, distributed applications, OS kernels 5. Positive and negative 3. Frameworks exhibit inversion of control at run-time consequence(s) of use i.e., the framework determines which objects and methods to 6. Implementation guidelines and invoke in response to events sample code 7. Known uses and related patterns Vanderbilt University 12 Vanderbilt University 13 OO Patterns Douglas C. Schmidt OO Patterns Douglas C. Schmidt Class Libraries vs. Frameworks vs. Patterns Component Integration in Frameworks APPLICATION- LOCAL MATH SPECIFIC INVOCATIONS CLASSES Definition FUNCTIONALITY ADT Framework components are loosely CLASSES DATABASE Class libraries APPLICATION CLASSES CODE coupled via callbacks GUI EVENT GLUE NETWORK – Self-contained, “pluggable” ADTs CLASSES Callbacks allow independently developed LOOP CODE IPC CLASSES EVENT software components to be connected Frameworks (A) CLASS LIBRARY ARCHITECTURE HANDLER(S) together – Reusable, “semi-complete” EVENT callback() NETWORKING LOOP applications Callbacks provide a connection-point where ADT DISPATCHER generic framework objects can CLASSES GUI APPLICATION- CALL Patterns EVENT LOOP communicate with application objects INVOKES SPECIFIC BACKS FUNCTIONALITY EVENT EVENT SENSOR MATH LOOP – Problem, solution, context – The framework provides the common CLASSES SYSTEM template methods and the application DATABASE EVENT CODE LOOP provides the variant hook methods (B) FRAMEWORK ARCHITECTURE Vanderbilt University 14 Vanderbilt University 15 OO Patterns Douglas C. Schmidt OO Patterns Douglas C. Schmidt Comparing Patterns and Frameworks Design Pattern Space Reactor/ProactorState Singleton Patterns and frameworks Strategy I/O Strategy Cached Virtual Creational patterns Framework Filesystem are highly synergistic – i.e., neither is – Deal with initializing and configuring classes and objects Asynchronous Completion Token Tilde ~ Expander /home/... subordinate Protocol Acceptor Structural patterns Handler Event Dispatcher Protocol Patterns can be Strategy – Deal with decoupling interface and implementation of classes and Filter characterized as more objects Adapter abstract descriptions of Behavioral patterns Concurrency frameworks, which are Protocol Pipeline Strategy Active Object implemented in a particular Framework Framework – Deal with dynamic interactions among societies of classes and Streams Service Configurator language objects In general, sophisticated frameworks embody dozens of patterns and patterns are often used to document frameworks Vanderbilt University 16 Vanderbilt University 17 OO Patterns Douglas C. Schmidt OO Patterns Douglas C. Schmidt Creational Patterns Structural Patterns Factory Method Adapter – Method in a derived class creates associates – Translator adapts a server interface for a client Abstract Factory – Factory for building related objects Bridge – Abstraction for binding one of many implementations Builder – Factory for building complex objects incrementally Composite Prototype – Structure for building recursive aggregations – Factory for cloning new instances from a prototype Decorator 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 Structural Patterns (cont’d) Behavioral Patterns Facade Chain of Responsibility – Facade simplifies the interface for a subsystem – Request delegated to the responsible service provider Flyweight Command – Many fine-grained objects shared efficiently – Request as first-class object Proxy Interpreter – One object approximates another – 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 Behavioral Patterns (cont’d) Behavioral Patterns (cont’d) Mediator Strategy – Mediator coordinates interactions between its associates – Abstraction for selecting one of many algorithms