Introduction to Patterns and Frameworks What Is a Pattern? a Framework?
Total Page:16
File Type:pdf, Size:1020Kb
Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Patterns and Frameworks CS 342: Object-Oriented Software Development Lab Motivation for Patterns and Frameworks Introduction to Patterns and Frameworks What is a Pattern? A Framework? Pattern Categories Dr. David L. Levine and Douglas C. Schmidt Department of Computer Science Pattern Examples Washington University, St. Louis flevine,[email protected] http://classes.cec.wustl.edu/cs342/ Department of Computer Science 1 Washington University, St. Louis Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Motivation for Patterns and Frameworks Overview of Patterns and Frameworks Patterns support reuse of software architecture and design Developing software is hard – Patterns capture the static and dynamic structures and Developing reusable software is even harder collaborations of successful solutions to problems that arise when Proven solutions include patterns and frameworks building applications in a particular domain Frameworks support reuse of detailed design and code – 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 – e.g., reuse, extensibility, modularity, performance Department of Computer Science 2 Washington University, St. Louis Department of Computer Science 3 Washington University, St. Louis Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Patterns of Learning Becoming a Chess Master First learn rules and physical requirements 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 principles Below, we’ll explore how patterns are used to learn chess – e.g., relative value of certain pieces, strategic value of center squares, power of a threat, etc. Learning to develop good software is similar to learning to play good chess However, to become a master of chess, one must study the 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 Department of Computer Science 4 Washington University, St. Louis Department of Computer Science 5 Washington University, St. Louis Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Becoming a Software Design Master Design Patterns 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., “Patterns == problem/solution pairs in a context” Then learn the principles Patterns capture the static and dynamic structure and collaboration – e.g., structured programming, modular programming, object among key participants in software designs oriented programming, generic programming, etc. – They are particularly useful for articulating how and why to However, to truly master software design, one must study the resolve non-functional forces designs of other masters Patterns facilitate reuse of successful software architectures and – These designs contain patterns must be understood, memorized, designs and applied repeatedly There are hundreds of these patterns Department of Computer Science 6 Washington University, St. Louis Department of Computer Science 7 Washington University, St. Louis Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Example: Stock Quote Service Observer Pattern SUBJECT REAL-TIME MODEL MARKET Intent DATA FEED – Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Key forces 1. There may be many observers STOCK QUOTES 2. Each observer may react differently to the same notification 3. The subject should be as decoupled as possible from the observers to allow observers to change independently of the OBSERVERS subject Department of Computer Science 8 Washington University, St. Louis Department of Computer Science 9 Washington University, St. Louis Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Structure of the Observer Pattern Graphical Notation foreach o in observers loop o.update() end loop Observer update() PROCESS 1N THREAD Subject observers notify() APPLICATION attach(observer) INDEPENDENT detach(observer) APPLICATION DEPENDENT Concrete OBJECT TEMPLATE Observer : CLASS CLASS Concrete update() Subject subject get_state() subject->get_state() subject_state_ return subject_state_ INHERITS CONTAINMENT Department of Computer Science 10 Washington University, St. Louis Department of Computer Science 11 Washington University, St. Louis Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Collaboration in the Observer Pattern Design Pattern Descriptions Concrete Concrete Concrete Subject Observer 1 Observer 2 Main parts set_state() 1. Name and intent notify() 2. Problem and context 3. Force(s) addressed update() 4. Abstract description of structure and collaborations in solution 5. Positive and negative consequence(s) of use get_state() 6. Implementation guidelines and sample code 7. Known uses and related patterns update() get_state() Pattern descriptions are often independent of programming language or implementation details – Contrast with frameworks . Department of Computer Science 12 Washington University, St. Louis Department of Computer Science 13 Washington University, St. Louis Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Frameworks Class Libraries vs. Frameworks vs. Patterns NETWORKING 1. Frameworks are semi-complete applications APPLICATION Definition SPECIFIC LOGIC INVOKES MATH ADTS – Class libraries Complete applications are developed by inheriting from, and EVENT USER Self-contained, instantiating parameterized framework components DATA LOOP INTERFACE BASE “pluggable” ADTs 2. Frameworks provide domain-specific functionality – Frameworks (A) CLASS LIBRARY ARCHITECTURE Reusable, e.g., business applications, telecommunication applications, “semi-complete” window systems, databases, distributed applications, OS kernels NETWORKING USER MATH INTERFACE applications APPLICATION 3. Frameworks exhibit inversion of control at run-time CALL – Patterns INVOKES SPECIFIC BACKS LOGIC EVENT Problem, solution, i.e., the framework determines which objects and methods to LOOP ADTS context invoke in response to events DATABASE (B) FRAMEWORK ARCHITECTURE Department of Computer Science 14 Washington University, St. Louis Department of Computer Science 15 Washington University, St. Louis Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Component Integration in Frameworks Comparing Pattern and Frameworks Framework components are loosely coupled via “callbacks” Patterns and frameworks are highly synergistic concepts, with neither subordinate to the other Callbacks allow independently developed software components to be connected together Patterns have been characterized as more abstract descriptions of frameworks, which are then implemented in a particular language Callbacks provide a connection-point where generic framework objects can communicate with application objects Sophisticated frameworks typically embody dozens of patterns – The framework provides the common template methods and the Likewise, patterns have been used to document frameworks application provides the variant hook methods Department of Computer Science 16 Washington University, St. Louis Department of Computer Science 17 Washington University, St. Louis Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Design Pattern Space Creational Patterns Factory Method Creational patterns – Method in a derived class creates associates – Deal with initializing and configuring classes and objects Abstract Factory Structural patterns – Factory for building related objects – Deal with decoupling interface and implementation of classes and Builder objects – Factory for building complex objects incrementally Behavioral patterns Prototype – Deal with dynamic interactions among societies of classes and objects – Factory for cloning new instances from a prototype Singleton – Factory for a singular (sole) instance Department of Computer Science 18 Washington University, St. Louis Department of Computer Science 19 Washington University, St. Louis Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Dr. David L. Levine CS 342: Patterns and Frameworks Introduction Structural Patterns Structual Patterns (cont’d) Adapter Facade – Translator adapts a server interface for a client – Facade simplifies