Foundations of Software Engineering Design Patterns -- Introduction

Similar documents
Design Patterns. An introduction

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

Design Patterns. Gunnar Gotshalks A4-1

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

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

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

CS/CE 2336 Computer Science II

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

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

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

Using Design Patterns in Java Application Development

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

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

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

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

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

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

SDC Design patterns GoF

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

Coordination Patterns

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

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

administrivia today UML start design patterns Tuesday, September 28, 2010

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

Tuesday, October 4. Announcements

CS251 Software Engineering Lectures 18: Intro to DP

An Introduction to Patterns

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

Inheritance. EEC 521: Software Engineering. Dealing with Change. Polymorphism. Software Design. Changing requirements Code needs to be flexible

Object-Oriented Design

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

CPSC 310 Software Engineering. Lecture 11. Design Patterns

Lecture 20: Design Patterns II

Work groups meeting 3

Introduction and History

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Topics in Object-Oriented Design Patterns

Patterns of learning

3 Product Management Anti-Patterns by Thomas Schranz

A Rapid Overview of UML

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

Applying the Observer Design Pattern

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

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

Object-Oriented Design

Design Patterns Reid Holmes

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

DESIGN PATTERN - INTERVIEW QUESTIONS

Object Oriented Paradigm

Object-Oriented Concepts and Design Principles

Design Patterns! Acknowledgements!

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

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

A Reconnaissance on Design Patterns

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

A Metric of the Relative Abstraction Level of Software Patterns

THE DESIGN PATTERNS JAVA COMPANION JAMES W. COOPER. Copyright 1998, by James W. Cooper

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

Reuse at Design Level: Design Patterns

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

DESIGN PATTERNS SURESH BABU M ASST PROFESSOR VJIT

INSTITUTE OF AERONAUTICAL ENGINEERING

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

INTERNAL ASSESSMENT TEST III Answer Schema

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

Applying the Decorator Design Pattern

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Object-oriented Software Design Patterns

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

Work groups meeting 3

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

Chapter 12 (revised by JAS)

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

Introduction to Software Engineering: Object Design I Reuse & Patterns

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

Lecture 17: Patterns Potpourri. Copyright W. Howden 1

CHAPTER 6: CREATIONAL DESIGN PATTERNS

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

Creational Design Patterns

Introduction to Design Patterns

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

SOFTWARE ENGINEERING SOFTWARE DESIGN. Saulius Ragaišis.

Introduction to Design Patterns

An Introduction to Patterns

The Design Patterns Matrix From Analysis to Implementation

Overview of Patterns: Introduction

Lecture Material. Design Patterns. Visitor Client-Server Factory Singleton

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Template Method Pattern. George Blankenship

A Metric for Measuring the Abstraction Level of Design Patterns

Summary of the course lectures

CSE870: Advanced Software Engineering (Cheng) 1

What is Design Patterns?

Chapter 1. OO e OO. (Pattern) Alexander) (context) (elements) k. (issue) Š. (name) (classification)

The GoF Design Patterns Reference

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

A Primer on Design Patterns

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

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

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Transcription:

Foundations of Software Engineering Design Patterns -- Introduction Fall 2016 Department of Computer Science Ben-Gurion university Based on slides of: Nurit Gal-oz, Department of Computer Science Ben-Gurion University Mira Balaban Department of Computer Science Ben-Gurion university F. Tip, J. Vlissides, J. Cooper, IBM T J Watson Research Center. R. Whitney, San-Diego State University. D.C. Schmidt, Vanderbilt University.

Sources 1. Classical text: Design Patterns: Elements of Reusable Object-Oriented Software, Gamma, Helm, Johnson, Vlissides, (GoF), 1995 2. Java Design Patterns A Tutorial, J.W. Cooper, 2000. (available online) 3. Applied Java Patterns, S. Stelting, O. Maassen, 2002. 4. Patterns home page: http://hillside.net/patterns/

Gang of Four (GoF)

What is a Pattern? (R. Whitney) "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice "Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution" Christopher Alexander on architecture patterns A Pattern Language, Christopher Alexander, 1977

What is a Pattern? "Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution" Christopher Alexander on architecture patterns A Pattern Language, Christopher Alexander, 1977 Problem Context Solution

Example: A Place To Wait (Alexander 1977) The process of waiting has inherent conflicts in it. The context: Waiting for doctor, airplane etc. requires spending time hanging around doing nothing Can not enjoy the time since you do not know when you must leave Classic "waiting room" Dreary little room People staring at each other Reading a few old magazines Offers no solution

Example: A Place To Wait (Alexander 1977) Fundamental problem: How to spend time wholeheartedly and Still be on hand when doctor, airplane etc arrive The solution: Fuse the waiting with other activity that keeps them in earshot Playground beside Pediatrics Clinic Allow the person to become still meditative A window seat that looks down on a street A protected seat in a garden A dark place and a glass of beer A private seat by a fish tank

Example: A Place To Wait (Alexander 1977) The Solution: "In places where people end up waiting, create a situation which makes the waiting positive. Fuse the waiting with some other activity - newspaper, coffee, pool tables, horseshoes; something which draws people in who are not simple waiting. And also the opposite: make a place which can draw a person waiting into a reverie; quiet; a positive silence

What are design patterns? (J.W. Cooper) Recurring solutions to design problems you see over and over. (Alpert et al, 1998) A set of rules describing how to accomplish certain tasks in the realm of software development. (Pree, 1994) Focus on reuse of recurring architectural design themes (Coplien and Schmidt, 1995) A three-part rule, which expresses a relation between a certain context, a certain system of forces which occurs repeatedly in that context, and a certain software configuration which allows these forces to resolve themselves Address a recurring design problem that arises in a specific context and presents a solution to it (Buschmann. et al, 1996) Identify and specify abstractions that are above the level of single classes or instances, or of components. (GoF)

More Paradigms of design patterns Problem Analysis (Fowler) Enterprise systems (Fowler) Responsibility assignment in system design (Larman) User interfaces. Web site construction. Software design (GoF, Coplien, and others)

What is a Software design pattern? (F. Tip) Related question: what is the difference between experienced and inexperienced software designers? experienced designers : know from experience what works and what doesn t able to recognize standard design problems and apply proven solutions to them Definition of a software design pattern: a description of communicating classes and objects that are customized to solve a general design problem in a particular context Collections of proven ways to structure the relationships between objects in the pursuit of a given objective

Benefits of Software Patterns (R. Whitney) By providing domain expertise patterns Reduce time to find solutions Avoid problems from inexpert design decisions Patterns reduce time to design applications Patterns are design chunks larger than objects Patterns reduce the time needed to understand a design

Inspiration, and a Vocabulary Design patterns speed the process of finding a solution, by eliminating the need to reinvent well-known and proven solutions Just as important, design patterns provide a vocabulary with which to discuss the space of possible design choices This vocabulary is termed a pattern language

Stages of Design Pattern awareness (J. Vlissides) benefit familiarity Ignorance consternation initiation understanding

Learn to apply design patterns to the design process (J. Vlissides) Find the right patterns Understand (un)applicability See when and how to bend a pattern Learn by (counter) example

Recurring Design Structures OO systems exhibit recurring structures that promote Abstraction Flexibility Modularity Elegance Therein lies valuable design knowledge Problem: capturing, communicating, & applying this knowledge

Elements of Design Patterns (F. Tip) A design pattern has 4 elements: a name (e.g. Abstract Factory or Visitor ) the problem that the pattern addresses the solution: the program constructs that are part of the pattern the consequences: the results and tradeoffs of applying the pattern other factors: problem & solution have been observed in practice choice of implementation language important

Basic Pattern Metamodel Problem Pattern name Solution * Consequence

Classifying Design Patterns (F. Tip) purpose: what a pattern does creational: concerned with creation of objects structural: related to composition of classes or objects behavioral: related to interaction and distribution of responsibility scope class-level: concerned with relationship between classes and their subclasses object-level: concerned with object relationship (more dynamic, may be changed at run-time)

GoF Design Patterns Classified Machado, B., Marinho. W. Dsign Patterns

GoF Design Patterns Classified (F. Tip)

Example

Principles of Object-Oriented Design (F. Tip) Program to an interface, not an implementation. Favor object composition over class inheritance.

Class vs. Interface Inheritance (F. Tip) Class inheritance defines an object s implementation in terms of another object s implementation mechanism for code & representation sharing Interface inheritance describes when an object can be used in place of another (subtyping) many languages (e.g., C++) don t make this distinction, but Java does An object's class defines how the object is implemented. The class defines the object's internal state and the implementation of its operations. An object's type only refers to its interface the set of requests to which it can respond. An object can have many types, and objects of different classes can have the same type

Class vs. Interface Inheritance (2) (F. Tip) benefits of class inheritance extend an application s functionality by reusing functionality in parent classes lets you get new implementations almost for free, inheriting most of what you need from existing classes benefits of interface inheritance clients remain unaware of specific types of objects they use, and of the classes that implement these objects using interface inheritance greatly reduces dependencies between subsystems reduces the impact of changes

Mechanisms for Reusing Functionality (F. Tip) Inheritance versus Composition class inheritance: define implementation of one class in terms of another often referred to as white-box reuse: internals of parent class visible to extending class class inheritance breaks encapsulation object composition: compose objects to get new, more complex functionality implemented by giving objects references to other objects; access these objects via interfaces requires that objects have well-defined interfaces often called black-box reuse: no internal details of objects are visible to the class that uses them composition does not break encapsulation

Pros & Cons of Class Inheritance (F. Tip) Advantages: directly supported by the programming language, hence easy to use makes it easy to modify the reused implementation (by simply overriding a few methods) Disadvantages: cannot change inherited functionality at run-time, because inheritance is fixed at compile-time parent classes define at least part of their subclasses physical representation, and subclasses are exposed to details of their parent s implementation implementation of subclass becomes very tightly coupled with implementation of parent change in parent is likely to require changes in subclass

Delegation (F. Tip) Delegation is an alternative to inheritance: two objects are involved: a receiving object delegates an operation to its delegate analogy: a subclass that defers a request to its parent class suppose an object of type C wants to delegate a method f() to an object of type D: class D defines method f() class C needs to contain a reference field d to a D-object, which needs to be initialized C needs a forwarding method f() that calls f() on d

Delegation: Example (F. Tip) class Window delegates public class Rectangle { its area() operation to private int width; a Rectangle instance private int height; public int area(){ return width * height; } } public class Window { private Rectangle rect; public int area(){ return rect.area(); } } public class WindowClient { void someoperation(window w){... w.area()... } }

(Ab)use inheritance for the same purpose (F. Tip) public class Rectangle { private int width; private int height; public int area(){ return width * height; } } public class Window extends Rectangle { private Rectangle rect; // method area() inherited from superclass } public class WindowClient { void someoperation(window w){... w.area()... } }

Why use delegation? (F. Tip) inheritance can be more convenient: only define method f() once no need to forward calls somewhat more efficient however, it is less flexible: cannot change the implementation of f() after creating the object in languages with single inheritance, you can only inherit methods from one superclass

True delegation (F. Tip) with inheritance, the method in the superclass can use dynamic dispatch to invoke methods in a subclass with delegation, this requires some extra work: pass receiving object s this pointer as an argument to the delegate delegate invokes methods on this reference when it needs to invoke methods on receiving object this form of delegation is called true delegation for an example of true delegation, see the State design pattern

When to use inheritance? (F. Tip) generally speaking, use inheritance for: is-a relationships that don t change over time situations where the class containing the actual operation is abstract generally speaking, use delegation for: has-a, part-of relationships is-a-role-played-by relationships relationships that change over time situations where multiple inheritance would be needed (if language doesn t allow MI)

Designing for change (F. Tip) many design patterns introduce flexibility to avoid common causes of redesign such as: creating an object by specifying a class explicitly dependence on specific operations dependence on hardware/software platform dependence on object representations or implementations algorithmic dependencies tight coupling extending functionality by subclassing inability to alter classes conveniently

Designing for change (R. Whitney) Some common design problems that GoF patterns address: Creating an object by specifying a class explicitly Abstract factory, Factory Method, Prototype Dependence on specific operations Chain of Responsibility, Command Dependence on hardware and software platforms Abstract factory, Bridge Dependence on object representations or implementations Abstract factory, Bridge, Memento, Proxy

Designing for change (R. Whitney) Algorithmic dependencies Builder, Iterator, Strategy, Template Method, Visitor Tight Coupling Abstract factory, Bridge, Chain of Responsibility, Command, Facade, Mediator, Observer Extending functionality by subclassing Bridge, Chain of Responsibility, Composite, Decorator, Observer, Strategy Inability to alter classes conveniently Adapter, Decorator, Visitor

GOF: Describing Design Patterns Pattern Name and Classification The pattern's name conveys the essence of the pattern succinctly. A good name is vital, because it will become part of your design vocabulary. Intent A short statement that answers the following questions: What does the design pattern do? What is its rationale and intent? What particular design issue or problem does it address? Also Known As Other well-known names for the pattern, if any. Motivation A scenario that illustrates a design problem and how the class and object structures in the pattern solve the problem. The scenario will help you understand the more abstract description of the pattern that follows. Applicability What are the situations in which the design pattern can be applied? What are examples of poor designs that the pattern can address? How can you recognize these situations? Structure A graphical representation of the classes in the pattern using a notation based on the Object Modeling Technique, interaction diagrams to illustrate sequences of requests and collaborations between objects.

GOF: Describing Design Patterns Participants The classes and/or objects participating in the design pattern and their responsibilities. Collaborations How the participants collaborate to carry out their responsibilities. Consequences How does the pattern support its objectives? What are the trade-offs and results of using the pattern? What aspect of system structure does it let you vary independently? Implementation What pitfalls, hints, or techniques should you be aware of when implementing the pattern? Are there languagespecific issues? Sample Code Code fragments that illustrate how you might implement the pattern in C++ or Smalltalk. Known Uses Examples of the pattern found in real systems. We include at least two examples from different domains. Related Patterns What design patterns are closely related to this one? What are the important differences? With which other patterns should this one be used?