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

Similar documents
SDC Design patterns GoF

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

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

Design Pattern and Software Architecture: IV. Design Pattern

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

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

INTERNAL ASSESSMENT TEST III Answer Schema

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

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

Design Patterns. An introduction

Object-Oriented Oriented Programming

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

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

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

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

An Introduction to Patterns

CS 520/620 Advanced Software Engineering Spring February 11, 2016

3 Product Management Anti-Patterns by Thomas Schranz

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Topics in Object-Oriented Design Patterns

Design Patterns. Gunnar Gotshalks A4-1

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

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

Design Patterns Reid Holmes

Lecture 13: Design Patterns

Composite Pattern. IV.4 Structural Pattern

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Design Patterns. Lecture 10: OOP, autumn 2003

What are patterns? Design Patterns. Design patterns. Creational patterns. The factory pattern. Factory pattern structure. Lecture 10: OOP, autumn 2003

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

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

Lecture 20: Design Patterns II

Tuesday, October 4. Announcements

Object Oriented Paradigm

SOFTWARE PATTERNS. Joseph Bonello

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

CPSC 310 Software Engineering. Lecture 11. Design Patterns

CSE 70 Final Exam Fall 2009

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

An Introduction to Patterns

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

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

CS 520/620 Advanced Software Engineering Fall September 27, 2016

Summary of the course lectures

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

Coordination Patterns

A Reconnaissance on Design Patterns

Object-oriented Software Design Patterns

Singleton Pattern Creational

The Design Patterns Matrix From Analysis to Implementation

The GoF Design Patterns Reference

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Chapter 8, Design Patterns Visitor

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

Foundations of Software Engineering Design Patterns -- Introduction

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

EINDHOVEN UNIVERSITY OF TECHNOLOGY

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

Compsci 290/Mobile, Java

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

INSTITUTE OF AERONAUTICAL ENGINEERING

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

CS/CE 2336 Computer Science II

Creational Design Patterns

LOG6306 : Études empiriques sur les patrons logiciels

DESIGN PATTERN - INTERVIEW QUESTIONS

CSC207H: Software Design Lecture 6

Introduction to Software Engineering: Object Design I Reuse & Patterns

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

Using Design Patterns in Java Application Development

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

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

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

Design Patterns 2. Page 1. Software Requirements and Design CITS 4401 Lecture 10. Proxy Pattern: Motivation. Proxy Pattern.

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

Chapter 6 Introduction to Defining Classes

UNIT I Introduction to Design Patterns

Design Patterns Reid Holmes

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

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

Design Pattern- Creational pattern 2015

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

Object-Oriented Design

Design patterns. Jef De Smedt Beta VZW

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

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

Design Patterns! Acknowledgements!

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

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

design patterns FOR B.tech (jntu - hyderabad & kakinada) (IV/I - CSE AND IV/II - IT) CONTENTS 1.1 INTRODUCTION TO DESIGN PATTERNS TTERNS... TTERN?...

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

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson


Final Exam Review (extended)

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Object-Oriented Design

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

Java Object Oriented Design. CSC207 Fall 2014

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Design Patterns. Definition of a Design Pattern

Transcription:

What is a Pattern? Lecture 40: Design Patterns CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki "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 "Patterns are not a complete design method; they capture important practices of existing methods and practices uncodified by conventional methods" James Coplien What are design patterns? Elements of Design Patterns Design pattern is a problem & solution in context Design patterns capture software architectures and designs - Not code reuse - Instead solution/strategy reuse - Sometimes interface reuse Pattern Name Problem statement - context where it might be applied Solution - elements of the design, their relations, responsibilities, and collaborations. - Template of solution Consequences: Results and trade-offs

Example: Iterator Pattern Iterator Pattern Name: Iterator or Cursor Problem statement - How to process elements of an aggregate in an implementation independent manner Solution - Aggregate returns an instance of an implementation of Iterator interface to control iteration. Consequences: - Support different and simultaneous traversals - Multiple implementations of Iterator interface - One traversal per Iterator instance requires coherent policy on aggregate updates - Invalidate Iterator by throwing an exception, or - Iterator only considers elements present at the time of its creation Goals of Patterns Taxonomy of Patterns To support reuse, of - Successful designs - Existing code (though less important) To facilitate software evolution - Add new features easily, without breaking existing ones Design for change! Reduce implementation dependencies between elements of software system. Creational patterns - concern the process of object creation Structural patterns - deal with the composition of classes or objects Behavioral patterns - characterize the ways in which classes or objects interact and distribute responsibility.

Singleton Creational Patterns - Ensure a class only has one instance, and provide a global point of access to it. - Often used in recursively defined classes (e.g., lists & trees) where don t have public constructor, just public constant defined using private constructor Abstract Factory - Provide an interface for creating families of related or dependent objects without specifying their concrete classes. - Allows hiding actual constructor call in method definition Adapter Structural Patterns - Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces Proxy - Provide a surrogate or placeholder for another object to control access to it Decorator - Attach additional responsibilities to an object dynamically Behavioral Patterns Template - Define the skeleton of an algorithm in an operation, deferring some steps to subclasses Abstract superclass State - Allow an object to alter its behavior when its internal state changes. The object will appear to change its class Observer - Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically Creational Patterns

Abstract Factory Abstract Factory (cont.) Solution: Context: - System should be independent of how pieces created and represented - Different families of components - Must be used in mutually exclusive and consistent way - Hide existence of different families from clients - Create interface w/ operations to create new products of different kinds - Multiple concrete classes implement operations to create concrete product objects. - Products also specified w/interface - Concrete classes for each interface and family of products. - Client uses only interfaces Abstract Factory (cont.) Abstract Factory Consequences Isolate instance creation and handling from clients Examples: - GUI Interfaces: Mac Windows XP Unix Can easily change look-and-feel standard - Reassign a global variable Enforce consistency among products in each family Adding to family of products is difficult - Have to update factory abstract class and all concrete classes

Decorator Pattern Motivation Structural Patterns - Want to add responsibilities/capabilities to individual objects, not to an entire class. - Inheritance requires a compile-time choice of parent class. Solution - Enclose the component in another object that adds the responsibility/capability The enclosing object is called a decorator. Decorator Pattern Decorator Pattern Consequences Advantages A decorator forwards requests to its encapsulated component and may perform additional actions before or after forwarding. Can nest decorators recursively, allowing unlimited added responsibilities. Can add/remove responsibilities dynamically - fewer classes than with static inheritance - dynamic addition/removal of decorators - keeps root classes simple Disadvantages - proliferation of run-time instances - abstract Decorator must provide common interface Tradeoffs: - useful when components are lightweight

Decorator Example FileReader frdr= new FileReader(filename); LineNumberReader lrdr = new LineNumberReader(frdr); String line; Behavioral Patterns line = lrdr.readline() while (line!= null){ System.out.print(lrdr.getLineNumber() + ":\t" + line); line = lrdr.readline() Observer Pattern Observer Pattern Problem - Objects that depend on a certain subject must be made aware of when that subject changes E.g. receives an event, changes its local state, etc. - These objects should not depend on the implementation details of the subject They just care about how it changes, not how it s implemented. Solution structure - Subject is aware of its observers (dependents) - Observers are notified by the subject when something changes, and respond as necessary - Examples: Java event-driven programming Subject - Maintains list of observers; defines a means for notifying them when something happens Observer - Defines the means for notification (update)

Observer Pattern Observer Pattern Consequences class Subject { private Observer[] observers; public void addobserver(observer newobs){... public void notifyall(event evt){ forall obs in observers do obs.process(this,evt) class Observer { public void process(subject sub, Event evt) {... code to respond to event... Low coupling between subject and observers - Subject indifferent to its dependents; can add or remove them at runtime Support for broadcasting Updates may be costly - Subject not tied to computations by observers Visitor Pattern Visitor Pattern Problem: want to implement multiple analyses on the same kind of object data - Spellchecking and Hyphenating Glyphs - Generating code for and analyzing an Abstract Syntax Tree (AST) in a compiler Flawed solution: implement each analysis as a method in each object - Follows idea objects are responsible for themselves - But many analyses will occlude the objects' main code - Result is classes hard to maintain We define each analysis as a separate Visitor class - Defines operations for each element of a structure A separate algorithm traverses the structure, applying a given visitor - But, like iterators, objects must reveal their implementation to the visitor object Separates structure traversal code from operations on the structure - Observation: object structure rarely changes, but often want to design new algorithms for processing