1 Patterns in Object Oriented Programming A Concise Introductory Presentation Amnon H. Eden Tel Aviv University

Slides:



Advertisements
Similar presentations
Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Object-Oriented Design Patterns Composite Singleton State Observer … Autumn 2012UCN Technology: IT/Computer Science1.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Plab – Tirgul 12 Design Patterns
Fundamentals of Software Development 1Slide 1 Gang of Four The beginnings… The original “patterns” idea was from architecture – there are repeatable patterns.
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Design Patterns CS is not simply about programming
Introduction to Design Patterns (1). “ In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Ralph Johnson - University of Illinois1 Patterns: What They Are, and How to Write Them Ralph Johnson University of Illinois at Urbana-Champaign
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Design Patterns William A. Hoffman NYU OOP Class.
Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.
Software Engineering I Object-Oriented Design Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Pattern Myths1 Ten Design Pattern Myths Jim Fawcett condensed from Pattern Hatching, John Vlissides, Addison-Wesley, 1998.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Proxy Design Pattern (1) –A structural design pattern.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns Discussion of pages: xi-11 Sections: Preface, Forward, Chapter
Design Patterns Trends and Case Study John Hurst June 2005.
Design Patterns.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Patterns 1 Patterns in OO - history 77 - Work of the architect Christopher Alexander 80s - Kent Beck playing with Smalltalk 91 - Gamma’s PhD thesis on.
Introduction to Design Patterns (1). Definition: “ In software engineering, a design pattern is a general reusable solution to a commonly occurring problem.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:
101 User Interface Patterns and its applications Tonya Groover Department of Computer Science.
January 12, Introduction to Design Patterns Tim Burke References: –Gamma, Erich, et. al. (AKA, The Gang of Four). Design Patterns: Elements of Reusable.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Powerpoint Templates Page 1 Powerpoint Templates What is Design Patterns ? by Indriati Teknik Informatika – UB.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
CS 325: Software Engineering February 12, 2015 Applying Responsibility-Assignment Patterns Design Patterns Situation-Specific Patterns Responsibility-Assignment.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
ECE450S – Software Engineering II
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Understanding and using patterns in software development EEL 6883 Software Engineering Vol. 1 Chapter 4 pp Presenter: Sorosh Olamaei.
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CSC 480 Software Engineering Design With Patterns.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Security Patterns Template and Tutorial - Darrell M. Kienzle, Ph.D., Matthew C. Elder, Ph.D., David S. Tyree, James Edwards-Hewitt Presented by Dan Frohlich.
Model View Controller Architectural Pattern and Observer Pattern
Advanced Object-Oriented Design Patterns and Architectures Part One COEN396A John Xiao
Design Patterns in Context ©SoftMoore ConsultingSlide 1.
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Patterns in programming
Design Patterns: MORE Examples
The Object-Oriented Thought Process Chapter 15
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns Introduction
Instructor: Dr. Hany H. Ammar
Advanced Programming Behnam Hatami Fall 2017.
Web Programming Language
Informatics 122 Software Design II
Object Oriented Design Patterns - Creational Patterns
Patterns.
DESIGN PATTERNS : Introduction
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Informatics 122 Software Design II
Presentation transcript:

1 Patterns in Object Oriented Programming A Concise Introductory Presentation Amnon H. Eden Tel Aviv University Copyrights  All Rights Reserved No part of this document may be reproduced or distributed by any means without the prior written permission of the author. Permission is granted for individuals to read and use this document for self study.

2 Contents Prologue: Motivation 1. Historical perspective 2. Terminology and taxonomy 3. Design patterns 4. Summary Epilogue: Future directions Appendix: References

3 A Claim Today, design patterns make the most effective technique of conducting and training OOD !

4 Prologue: Motivation “Software Engineering is an art rather than science” It requires: n Skill, talent, intelligence n Experience n Creativity, imagination, insight

5 Motivation II There are about 3000 years of artistic experience to learn from... …but only some 20 years of experience with OOP! D:Delegator C1:Concrete_ State Client F 2: do1(a,b) 1: do1(a,b)

6 Motivation III Patterns are about conveying skill and experience

7 Part 1: Historical Perspective n The work of Christopher Alexander n Introducing patterns in OOP – The Hillside group and their work – A patterns hype

8 Christopher Alexander An architect Relate publications: n The Timeless Way of Building (1979) n A Pattern Language (1977)

9 The Work of Christopher Alexander Pattern: “A solution to a problem in a context”. Pattern language: A set of patterns used to solve closely related problems. Purpose: – effective reuse – dissemination of solutions

10 Introducing Patterns into OOP n 1987: A workshop in OOPSLA by Beck & Cunningham n 1993: The Hillside Group - Beck, Cunningham, Coplien, Booch, Johnson,... n 1994: PLoP conference n 1995: Design Patterns: Elements of Reusable Object Oriented Software

11 Part 2: Taxonomy Terms: n Pattern n Pattern language Related terms: n Design n Architecture n Framework n Library n Component

12 Domain Specific Patterns n Communications and distributed processing n Operating systems and processes Business n Reactive systems n MMI-application interaction

13 Language Specific Patterns Also called idioms C++: n Localized Ownership (PLoPD2) n Coplien: “Advanced C++ Programming styles and Idioms” Smalltalk: n Lazy Optimization (PLoPD2) n Beck: “Smalltalk Patterns: Best Practices”

14 Dominant Pattern Domains Design patterns ! Architectural Patterns?

15 Presenting a Pattern The common form: n Name n Intent n Solution n Consequences Other authors: n Name n Problem n [Context] n [Forces] n Solution

16 Part 3: Design Patterns n The Gang of Four n Examples n Definitions n Observations and conventions n Summary

17 The Gang of Four n Erich Gamma n Richard Helm n Ralph Johnson n John Vlissides Design Patterns: Elements of Reusable Object Oriented Software (1995).

18 Design pattern: Iterator n Intent: Allow (iterative/sequential) access to elements of a data structure n Forces: Preserve data abstraction: Do not expose its internal structure

19 Iterator Example i: List Iterator f1:File f2:File f3:File fileList: LinkedList :Link F current F next F element F next F element F F first...

20 Iterator: Solution (general form) Aggregate createIterator( ) Insert( ) Remove( ) A Client Iterator First( ) Next( ) Prev( ) A Concrete Aggregate2 createIterator( ) Insert( ) Remove( ) Concrete Iterator2 First( ) Next( ) Prev( ) Concrete Aggregate1 createIterator( ) Insert( ) Remove( ) Concrete Iterator2 First( ) Next( ) Prev( )

21 Design Pattern: Proxy n Problem: Defer the cost of object creation and init. until actually used n Applicability (possible contexts): – Virtual Proxy: Create an expensive object on demand (lazy construction) – Cache Proxy (PLoPD 2): Hold results temporarily – Remote Proxy: Use a local representative for a remote object (different address space) – Protection Proxy: Control access to shared object

22 Sample Context: Word Processor Paragraph Document Paragraph Image document glyph Draw( ) DrawDetailed( ) Position( ) A paragraph Draw( ) DrawDetailed( ) Position( ) image Draw( ) DrawDetailed( ) Position( ) table Draw( ) DrawDetailed( ) Position( ) 0..n

23 Forces 1. The image is expensive to load 2. The complete image is not always necessary 2MB 2KB optimize!

24 Anti-Pattern Obviously, the document should not be aware to the optimization, nor to the image contents, etc. document::draw() { switch (glyph.type) { case image: if (cannot_optimize) load_full_image(); else optimize(glyph); break; case paragraph:...

25 Proxy Solution (example) Image Proxy || file_name : String || get_image( ) Draft( ) DrawDetailed( ) Document Real Image || ImageData Glyph Draw( ) 0..n real_image Solution: Provide a “surrogate” or place-holder which provides transparent access to another object

26 Proxy Solution (example): Object Diagram aDocument image Proxy theBitmap: RealImage 1: DrawDetailed ( ) 3: new (fileName) 2: get_image ( ) 4: DrawDetailed( )

27 Proxy Solution (general form) RealSubject Request( ) Proxy Request( ) Subject Request( ) client realSubject

28 Definitions of Design Patterns n GoF: Description of communicating objects and classes that are customized to solve a general design in a particular context n Coplien & Schmidt: Design patterns capture the static and dynamic structures of solutions that occur repeatedly when producing applications in a particular context

29 Design Pattern: Observer n Problem: Decouple a data model from “parties” interested in its internal state n Forces – The identity and number of receivers is not predetermined – Receivers of novel classes may be added to the system in the future – Polling is inappropriate (too expensive, etc.)

30 Observer Example: File System Remote File Server Window in PC 1 Window in PC 2

31 Observer: Solution Subject::Notify() { for (i = 0; i < _ObsNumber; i++) _my_obs[i]->Update(this); } ConcreteSubject::ChangeData() { // Chnage data somehow Notify(); } Observer 1 Update(subject *) Concrete Subject ChangeData( ) Observer Update(subject *) = 0 A Subject Attach(Observer*) Detach(Observer *) Notify() my_obs 1 0..n Observer 2 Update(subject *)

32 The Observer Ignores: n How information about the change is conveyed n Refining the mechanism for different kinds of changes... Different implementations conform to the same pattern ! (Variations listed in: “Implementation Patterns for the Observer Pattern”, Kim & Benner, PLoPD2)

33 Design pattern: Strategy n Intent: Let each member of a family of algorithms vary independently from clients that use it n Forces: – input depends on the algorithm – calculations are based on the client’s abstraction (not using client’s implementation or global data)

34 Strategy sample: I Traffic Lights Control Strategy sample: Intersection Traffic Lights Control The light- switching policy changes by the hour

35 The Behavior Varies n The “dumb” policy: change the green route every 5 seconds n Midnight policy: change to green whenever a “sensor” detects a vehicle n Rush hour policy: double the “green time” in the busy route n …

36 Anti-Pattern Use multiple conditional statement to set the behavior according to current policy intersection::next_green(the_green_route r) { switch (current_policy) { case dumb: next_time := time + constant_time; case midnight: if (not the_green_route.is_busy) then … case rush_hour: … }

37 Strategy Solution (example) Intersection TheConstantTime( ) PreviousGreenRoute( ) ThisGreenRoute( ) TraficRouting Policy compute_change_time() DumbPolicy compute_change_time() Midnight Policy compute_change_time() RushHour Policy compute_change_time() Emergency Policy compute_change_time() current_policy

38 Strategy Solution (example) Interaction Diagram theIntersection current_policy: rush_hour 1: compute_change_time(self) 2: const_time ( ) 4: change_time_is (2 * 13) 3: const_time_is (13)

39 Strategy: Consequences n Easy to add new strategy (or remove an existing one, etc.) For instance: From 8 a.m. to 10 a.m. there is no turn left from Dizengof to Arlozorov

40 Strategy: Consequences II n Easy to factor out similar strategies (using inheritance) RushHour Policy compute_change_time() Morning RushHour busy_direction( ) Afternoon RushHour busy_direction( ) rushHour::compute_change_time(intersection inter) { if (inter.curr_green_route == busy_direction()) return (inter.const_time * 2) else return (inter.const_time); }

41 Part 4: Design Patterns - Summary n Perspectives n Observations n Types of design patterns n The Merits of Patterns

42 Perspectives on Patterns I “Each design pattern lets some aspect of system structure vary independently of other aspects, thereby making a system more robust to a particular kind of change.” [GoF 95, p. 24]. For instance: – Observer: the observers vary independently – Strategy: the strategies vary independently – Iterator: the data structures vary independently

43 Perspectives II Design patterns are recurring building blocks of rough granularity X Y Z Observer 1 Update(subject *) Concrete Subject ChangeData( ) Observer Update(subject *) = 0 A Subject Attach() Detach(Observer *) Notify() my_obs 1 0..n Observer 2 Update(subject *)

44 Perspectives III: Patterns Combine & Interact Real Image Observer Update(subject *) = 0 A Subject Attach() Detach() Notify() 1 n Image Proxy Update(Subject *) Glyph Document || file_name : String get_image( ) 0..n

45 Perspectives IV Patterns are of different levels of abstraction Pattern A Pattern B Pattern C

46 Observation: The specified solution is not always complete in detail. Possible reasons: – Context dependent – Language dependent – There are many possible implementations with similar results (difference does not count) – It cannot be precisely formulated (but only expressed in natural language)

47 Summary: The Merits of Patterns

48 Merits of Patterns I: Merits of Patterns I: Higher Level of Abstraction

49 Merits of Patterns II: Merits of Patterns II: Improved Design “reuse” ideas (as opposed to pieces of code) better understanding of mechanisms: do not repeat mistakes or “reinvent the wheel” RealSubject Request( ) Proxy Request( ) Subject Request( ) client realSubject

50 Merits of Patterns III: Merits of Patterns III: Improve all Forms of Communication documenting group planning and design presenting or learning an existing system Patter n

51 Merits of Patterns IV: Training dissemination of solutions recording the expert’s experience

52 Classifications of Design Patterns Each design pattern dictates structure and/or interaction protocol Patterns are classified according to structure and/or protocol n Structural: how classes and objects form structures n Behavioral: patterns of communications, assigning responsibilities n Creational: abstract the instantiation process

53 Conventions and Beliefs About Patterns n It must be documented in at least 3 distinct applications before it is a “pattern” n A paper should go through a writer’s workshop before it is published n It cannot be “coded” in a conventional OOPL

54 Epilogue: Future Directions Open problems: n Classification and indexing n Tool support n Formalization n Analysis patterns Questions: n Will patterns survive? n Will the industry of patterns converge?

55 Appendix: References n Books n Articles

56 Collections of Patterns n Gamma, Helm, Johnson, Vlissides (1995). Design Patterns: Elements of Reusable O-O Software n Coplien & Schmidt, eds. (1994). Pattern Languages of Program Design (AKA PLoPD 1). n Vlissides, Coplien & Kerth, eds. (1995). Pattern Languages of Program Design 2 (AKA PLoPD 2). n Buschmann et. al (1996). Pattern-Oriented Software Architecture (AKA The Siemens Book). n Martin R., D. Riehle, F. Buschmann, eds. (1997) Pattern Languages of Program Design 3. n Fowler (1997). Analysis Patterns.

57 PLoP Conferences PLoP = Pattern Languages of Programming Twice a year, since 1994: n PLoP USA n EuroPLoP Purpose: Promote good pattern papers Products: PLoPD books Pattern

58 Web Sites n Patterns home page:

59 Articles n Beck & Johnson Patterns Generate Architecture. ECOOP 94 n Schmidt D. Overview of Patterns and Pattern Languages. CACM, Vol. 39, No. 10, October 96. n Vlissides J. To Kill a Singleton. C++ Report, June 1996.

60 Discussion Groups n Discussion groups: – Tel Aviv – New York – Washington – Florida – Stockholm