Design pa*erns. Based on slides by Glenn D. Blank

Similar documents
Design Patterns. An introduction

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

Design Pa*erns. + Anima/on Undo/Redo Graphics and Hints

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

DESIGN PATTERN - INTERVIEW QUESTIONS

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Using Design Patterns in Java Application Development

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

SDC Design patterns GoF

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Design Patterns. Gunnar Gotshalks A4-1

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Tuesday, October 4. Announcements

CS/CE 2336 Computer Science II

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

An Introduction to Patterns

Design Patterns! Acknowledgements!

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

Topics in Object-Oriented Design Patterns

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Object-Oriented Design

What is Design Patterns?

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

A Reconnaissance on Design Patterns

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

An Introduction to Patterns

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

Introduction to Software Engineering: Object Design I Reuse & Patterns

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

MVC. Model-View-Controller. Design Patterns. Certain programs reuse the same basic structure or set of ideas

Object Oriented Paradigm

Introduction and History

CSE870: Advanced Software Engineering (Cheng) 1

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

1 Software Architecture

Applying the Observer Design Pattern

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

2.1 Design Patterns and Architecture (continued)

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

2.1 Design Patterns and Architecture (continued)

Review Software Engineering October, 7, Adrian Iftene

Object-oriented Software Design Patterns

Foundations of Software Engineering Design Patterns -- Introduction

Command. Comp-303 : Programming Techniques Lecture 22. Alexandre Denault Computer Science McGill University Winter 2004

Application Architectures, Design Patterns

Design Patterns Reid Holmes

3 Product Management Anti-Patterns by Thomas Schranz

A Metric for Measuring the Abstraction Level of Design Patterns

Object-Oriented Oriented Programming

Information systems modelling UML and service description languages

A Metric of the Relative Abstraction Level of Software Patterns

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Software Design COSC 4353/6353 D R. R A J S I N G H

6.3 Patterns. Definition: Design Patterns

Design patterns. Jef De Smedt Beta VZW

Design Patterns. it s about the Observer pattern, the Command pattern, MVC, and some GUI. some more

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Software Engineering I (02161)

Introduction to Patterns and Frameworks

Applying the Decorator Design Pattern

Design Principles & Prac4ces

What is Design Patterns?

Patterns of learning


Software Engineering I (02161)

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

SYLLABUS CHAPTER - 1 [SOFTWARE REUSE SUCCESS FACTORS] Reuse Driven Software Engineering is a Business

Information Systems Analysis and Design. XIV. Design Patterns. Design Patterns 1

Object-Oriented Design

CSC7203 : Advanced Object Oriented Development. J Paul Gibson, D311. Design Patterns

LECTURE NOTES ON DESIGN PATTERNS MCA III YEAR, V SEMESTER (JNTUA-R09)

Development and Implementation of Workshop Management System Application to Explore Combing Multiple Design Patterns

Design Pa*erns. Philippe Collet. Master 1 IFI Interna3onal h8p://dep3nfo.unice.fr/twiki/bin/view/minfo/soeeng1213. P.

Reuse at Design Level: Design Patterns

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

A Rapid Overview of UML

Chapter 10: Performance Patterns

Coordination Patterns

Lecture 17: Patterns Potpourri. Copyright W. Howden 1

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Brief Note on Design Pattern

WS01/02 - Design Pattern and Software Architecture

An Introduction to Design Patterns

CS251 Software Engineering Lectures 18: Intro to DP

Design patterns. OOD Lecture 6

Design Patterns. GoF design patterns catalog

Transcription:

Design pa*erns Based on slides by Glenn D. Blank

Defini6ons A pa#ern is a recurring solu6on to a standard problem, in a context. Christopher Alexander, a professor of architecture Why would what a prof of architecture says be relevant to so7ware? A pa*ern describes a problem which occurs over and over again in our environment, and then describes the core of the solu6on to that problem, in such a way that you can use this solu6on a million 6mes over, without ever doing it the same way twice. Jim Coplein, a soeware engineer: I like to relate this defini6on to dress pa*erns What are dress pa#erns?... I could tell you how to make a dress by specifying the route of a scissors through a piece of cloth in terms of angles and lengths of cut. Or, I could give you a pa*ern. Reading the specifica6on, you would have no idea what was being built or if you had built the right thing when you were finished. The pa*ern foreshadows the product: it is the rule for making the thing, but it is also, in many respects, the thing itself.

Pa*erns in engineering How do other engineers find and use pa#erns? Mature engineering disciplines have handbooks describing successful solu6ons to known problems Automobile designers don't design cars from scratch using the laws of physics Instead, they reuse standard designs with successful track records, learning from experience Should so7ware engineers make use of pa#erns? Why? Be sure that you make everything according to the pattern I have shown you here on the mountain. Exodus 25:40. Developing soeware from scratch is also expensive Pa*erns support reuse of soeware architecture and design

The gang of four (GoF) Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides (Addison- Wesley, 1995) Design Pa#erns book catalogs 23 different pa*erns as solu6ons to different classes of problems, in C++ & Smalltalk The problems and solu6ons are broadly applicable, used by many people over many years What design pa*ern did we discover with the Undo problem? Why is it useful to learn about this pa*ern? Pa*erns suggest opportuni6es for reuse in analysis, design and programming GOF presents each pa*ern in a structured format What do you think of this format? Pros and cons?

Elements of Design Pa*erns Design pa*erns have 4 essen6al elements: Pa*ern name: increases vocabulary of designers Problem: intent, context, when to apply Solu6on: UML- like structure, abstract code Consequences: results and tradeoffs

Model View Controller (MVC) MVC slides by Rick Mercer with a wide variety of others 6

Model/View/Controller The intent of MVC is to keep neatly separate objects into one of tree categories Model The data, the business logic, rules, strategies, and so on View Displays the model and usually has components that allows user to edit change the model Controller Allows data to flow between the view and the model The controller mediates between the view and model 7

Sun says Model-View-Controller ("MVC") is the recommended architectural design pattern for interactive applications MVC organizes an interactive application into three separate modules: one for the application model with its data representation and business logic, the second for views that provide data presentation and user input, and the third for a controller to dispatch requests and control flow. 8

Sun continued Most Web-tier application frameworks use some variation of the MVC design pattern The MVC (architectual) design pattern provides a host of design benefits 9

Java Server Pages Model 2 Architecture to serve dynamic content Model: Enterprise Beans with data in the DBMS JavaBean: a class that encapsulates objects and can be displayed graphically Controller: Servlets create beans, decide which JSP to return, do the bulk of the processing View: The JSPs generated in the presentation layer (the browser) 10

OO-tips Says The MVC paradigm is a way of breaking an application, or even just a piece of an application's interface, into three parts: the model, the view, and the controller. MVC was originally developed to map the traditional input, processing, output roles into the GUI realm: Input --> Processing --> Output Controller --> Model --> View 11

MVC Benefits Clarity of design easier to implement and maintain Modularity changes to one don't affect the others can develop in parallel once you have the interfaces Multiple views games, spreadsheets, powerpoint, Eclipse, UML reverse engineering,. 12

The Model's responsibilities Model Provide access to the state of the system Provide access to the system's functionality Can notify the view(s) that its state has changed 13

The view's responsibilities View Display the state of the model to the user At some point, the model (a.k.a. the observable) must registers the views (a.k.a. observers) so the model can notify the observers that its state has changed 14

Controller The controller's responsibilities Accept user input Button clicks, key presses, mouse movements, slider bar changes Send messages to the model, which may in turn notify it observers Send appropriate messages to the view In Java, listeners are controllers 15

from http://www.enode.com/x/markup/tutorial/mvc.html) 16

Command pa*ern Synopsis or Intent: Encapsulate a request as an object, thereby leeng you parameterize clients with different requests, queue or log requests, and support undoable opera6ons Context: You want to model the 6me evolu6on of a program: What needs to be done, e.g. queued requests, alarms, condi6ons for ac6on What is being done, e.g. which parts of a composite or distributed ac6on have been completed What has been done, e.g. a log of undoable opera6ons What are some applicaaons that need to support undo? Editor, calculator, database with transac6ons Perform an execute at one 6me, undo at a different 6me Solu0on: represent units of work as Command objects Interface of a Command object can be a simple execute() method Extra methods can support undo and redo Commands can be persistent and globally accessible, just like normal objects

Command pa*ern, con6nued Structure: Par0cipants (the classes and/or objects par6cipa6ng in this pa*ern): Command (Command) declares an interface for execu6ng an opera6on ConcreteCommand defines a binding between a Receiver object and an ac6on implements Execute by invoking the corresponding opera6on(s) on Receiver Invoker asks the command to carry out the request Receiver knows how to perform opera6ons associated with carrying out the request Client creates a ConcreteCommand object and sets its receiver

Command pa*ern, con6nued Consequences: You can undo/redo any Command Each Command stores what it needs to restore state You can store Commands in a stack or queue Command processor pa*ern maintains a history It is easy to add new Commands, because you do not have to change exis6ng classes Command is an abstract class, from which you derive new classes execute(), undo() and redo() are polymorphic func6ons

Design Pa*erns are NOT Data structures that can be encoded in classes and reused as is (i.e., linked lists, hash tables) Complex domain- specific designs (for an en6re applica6on or subsystem) If they are not familiar data structures or complex domain- specific subsystems, what are they? They are: Descrip6ons of communica6ng objects and classes that are customized to solve a general design problem in a par6cular context.

Observer pa*ern Intent: Define a one- to- many dependency between objects so that when one object changes state, all its dependents are no6fied and updated automa6cally Used in Model- View- Controller framework Model is problem domain View is windowing system Controller is mouse/keyboard control How can Observer pa#ern be used in other applicaaons? JDK s Abstract Window Toolkit (listeners) Java s Thread monitors, no6fy(), etc.

Structure of Observer Pa*ern Subject Observer for all observers obs { obs->update() } +notify() +attach(in Observer) +detach(in Observer) 1 * +update() ConcreteSubject -subjectsate +getstate() 1 * ConcreteObserver +update() return subjectstate observerstate = subject->getstate()

Three Types of Pa*erns Crea0onal pa>erns: Deal with ini6alizing and configuring classes and objects Structural pa>erns: Deal with decoupling interface and implementa6on of classes and objects Composi6on of classes or objects Behavioral pa>erns: Deal with dynamic interac6ons among socie6es of classes and objects How they distribute responsibility

Singleton pa*ern (crea6onal) Ensure that a class has only one instance and provide a global point of access to it Why not use a global variable? class Singleton { public: static Singleton* getinstance(); protected: //Why are the following protected? Singleton(); Singleton(const Singleton&); Singleton& operator= (const Singleton&); private: static Singleton* instance; }; Singleton *p2 = p1->getinstance();

Crea6onal Pa*erns Abstract Factory: Factory for building related objects Builder: Factory for building complex objects incrementally Factory Method: Method in a derived class creates associates Prototype: Factory for cloning new instances from a prototype Singleton: Factory for a singular (sole) instance

Structural pa*erns Describe ways to assemble objects to realize new func6onality Added flexibility inherent in object composi6on due to ability to change composi6on at run- 6me not possible with sta6c class composi6on Example: Proxy Proxy: acts as convenient surrogate or placeholder for another object. Remote Proxy: local representa6ve for object in a different address space Virtual Proxy: represent large object that should be loaded on demand Protected Proxy: protect access to the original object

Structural Pa*erns Adapter: Translator adapts a server interface for a client Bridge: Abstrac6on for binding one of many implementa6ons Composite: Structure for building recursive aggrega6ons Decorator: Decorator extends an object transparently Facade: Simplifies the interface for a subsystem Flyweight: Many fine- grained objects shared efficiently. Proxy: One object approximates another

Behavioral Pa*erns Chain of Responsibility: Request delegated to the responsible service provider Command: Request or Ac6on is first- class object, hence re- storable Iterator: Aggregate and access elements sequen6ally Interpreter: Language interpreter for a small grammar Mediator: Coordinates interac6ons between its associates Memento: Snapshot captures and restores object states privately Which ones do you think you have seen somewhere?

Behavioral Pa*erns (cont.) Observer: Dependents update automa6cally when subject changes State: Object whose behavior depends on its state Strategy: Abstrac6on for selec6ng one of many algorithms Template Method: Algorithm with some steps supplied by a derived class Visitor: Opera6ons applied to elements of a heterogeneous object structure

Pa*erns in soeware libraries AWT and Swing use Observer pa*ern Iterator pa*ern in C++ template library & JDK Façade pa*ern used in many student- oriented libraries to simplify more complicated libraries! Bridge and other pa*erns recurs in middleware for distributed compu6ng frameworks

More soeware pa*erns Design pa*erns idioms (low level, C++): Jim Coplein, Sco> Meyers I.e., when should you define a virtual destructor? design (micro- architectures) [Gamma- GoF] architectural (systems design): layers, reflec0on, broker Reflec6on makes classes self- aware, their structure and behavior accessible for adapta6on and change: Meta- level provides self- representa6on, base level defines the applica6on logic Java Enterprise Design Pa#erns (distributed transac6ons and databases) E.g., ACID Transac6on: Atomicity (restoring an object aeer a failed transac6on), Consistency, Isola6on, and Durability Analysis pa>erns (recurring & reusable analysis models, from various domains, i.e., accoun6ng, financial trading, health care) Process pa>erns (soeware process & organiza6on)

Benefits of Design Pa*erns Design pa*erns enable large- scale reuse of soeware architectures and also help document systems Pa*erns explicitly capture expert knowledge and design tradeoffs and make it more widely available Pa*erns help improve developer communica6on Pa*ern names form a common vocabulary

Web Resources h*p://home.earthlink.net/~huston2/dp/ h*p://www.dofactory.com/ h*p://hillside.net/pa*erns/ Java Enterprise Design Pa#erns