COMP280:Introduction to Software Development, Week 9, Lecture 27

Slides:



Advertisements
Similar presentations
Model Driven Generative Programming Reza Azimi February 6, 2003 ECE1770: Trends in Middleware Systems.
Advertisements

Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Design Patterns for Object Oriented systems CSC 515 Ashwin Dandwate.
Design Patterns Yes, they are important Robert Cotton April 23, 2009.
Patterns Reusable solutions to common object-oriented programming problems When given a programming problem, re-use an existing solution. Gang of Four.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
Design Patterns CS is not simply about programming
Design Patterns Daniel McClain. Background What are they?  Way of recording solutions to recurring design problems History  “A Pattern Language: Towns,
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Visual Basic: An Object Oriented Approach 11 – Patterns in object oriented programming.
Design Patterns William A. Hoffman NYU OOP Class.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Software Design Patterns Anton Danshin Moscow Institute of Physics and Technology Dolgoprudny
Object-Oriented Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Patterns Standardized Recurring model Fits in many location Opposite of customization Fundamental types of pattern Choose and use as desired and.
樣式導向設計 (Pattern-Oriented Design) 課程簡介 Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
CSSE 374: Introduction to Gang of Four Design Patterns
1 SAD2 - UML 4 th Lecture Class Diagram in Construction Phase Patterns Case Study Lecturer: Dr Dimitrios Makris
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
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.
Design Patterns in Java Chapter 1 Introduction Summary prepared by Kirk Scott 1.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Testing Extensible Design Patterns in OO Frameworks through Scenario Templates D.S. Sanders Software Verification & Validation.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
1 Design Patterns Object-Oriented Design. 2 Design Patterns 4Reuse of design knowledge and experience 4Common in many engineering disciplines 4Avoids.
Creational Patterns
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Introduction to Patterns. Introduction to Patterns Pattern: Webster definition of Pattern: Something regarded as a normative example to be copied.
CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
1 Design Patterns prepared for COMP314, Bernhard Pfahringer see links on the web page as well!
Design Patterns CSCE 315 – Programming Studio Spring 2013.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
The Object-Oriented Thought Process Chapter 15
樣式導向設計 (Pattern-Oriented Design) 課程簡介
GoF Patterns (GoF) popo.
MPCS – Advanced java Programming
Introduction to Design Patterns
Design Patterns Lecture part 2.
Introduction to Design Patterns
Design Patterns Introduction
COMP280:Introduction to Software Development Week 12, Lecture 34
COMP280:Introduction to Software Development Week 10, Lecture 28
Structure We saw early on the importance of algorithms, but what about structuring our code? What's the best way to structure a larger project made of.
Chapter 8, Design Patterns Bridge
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Advanced Programming Behnam Hatami Fall 2017.
Object Oriented Design Patterns - Creational Patterns
CSE 403 Software Design.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
DESIGN PATTERNS : Introduction
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Chapter 8, Design Patterns Singleton
Structure We saw early on the importance of algorithms, but what about structuring our code? What's the best way to structure a larger project made of.
Chapter 8, DesignPatterns Facade
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Presentation transcript:

COMP280:Introduction to Software Development, Week 9, Lecture 27 Bernhard Pfahringer

How about a general solution to caching? def memo(f): memo = {} def helper(x): if x not in memo: memo[x] = f(x) return memo[x] return helper @memo def fib(n): return n if n < 2 else fib(n-1) + fib(n-2)

An example of the Decorator pattern @memo def fib(n): … Is equivalent to the following: fib = memo(fib) @memo can be used/wrapped around any function More on “Patterns” later

What else is wrong with fib(n) ? >>> fib( -10 ) -10 ??? >>> fib( 10.2 ) 72.79999999999993

Checking input validity def fib(n): assert type(n) is int assert n >= 0 return n if n < 2 else fib(n-1) + fib(n-2) if type(n) is not int: raise TypeError( "fib: n must be an int: ” + str(n)) if n < 0: raise ValueError( "fib: must be non-negative: %d” % n) More on “defensive programming” later

What is still wrong with fib(n) ? >>> help( fib ) ??? def fib(n): """Compute Fibonacci numbers""" return n if n < 2 else fib(n-1) + fib(n-2) >>> help(fib) fib(n) Compute Fibonacci numbers More on ”documenting code” later

What else is missing: Testing def fib(n): """Compute Fibonacci numbers >>> fib(0) 0 >>> fib(30) 832040 """ return n if n < 2 else fib(n-1) + fib(n-2) if __name__ == '__main__': import doctest doctest.testmod()

Putting it all together def fib(n): """Compute Fibonacci numbers >>> fib(0) 0 >>> fib(30) 832042 """ if type(n) is not int: raise TypeError( "fib: n must be an int: {}”.format(n)) if n < 0: raise ValueError( "fib: n must be non-negative: {}”.format(n)) if n < 2: return n else: a, b = 0, 1 for i in range(n-1): a, b = b, a+b return b

Design Patterns 1994 book by GoF “Gang of Four” Gamma, Helm, Johnson, Vlissides Template solution for common problem Make up for a deficiency/shortcoming of the language lack of library Also help documenting/understanding systems Generally: Patterns use classes and instances to explicitly represent actions

Groups of patterns Creational, e.g. Structural, e.g. Behavioural, e.g. Prototype Abstract Factory Builder Structural, e.g. Adapter Decorator Proxy Behavioural, e.g. Iterator Command [Concurrency] [Architecture]

Prototype [ plus shallow and deep copying ] Creating an instance using another as a template In Java or C/C++: copy-constructor Python: only one __init__ (more or less) but copy(), copy.deepcopy(), __copy__, __deepcopy__ a = [ [ 0 ] ] b = a.copy() a[0][0] = 1 b ? b[0] = 2 a ? a = [ [ 0 ], 1] a[1] = a[0] a ? a[0][0] = 1 b = copy.deepcopy(a) b[0][0] = 2 b ?