Examples (D. Schmidt et al)

Slides:



Advertisements
Similar presentations
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Advertisements

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
Design Patterns CS is not simply about programming
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
© Spiros Mancoridis 27/09/ Software Design Topics in Object-Oriented Design Patterns Material drawn from [Gamma95] and [Coplien95] Revised and augmented.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 10 Use Case Design.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Structural Design Patterns
ECE450S – Software Engineering II
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns Introduction
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Sorts, CompareTo Method and Strings
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Strategy Design Pattern
5.13 Recursion Recursive functions Functions that call themselves
Chapter 10 Design Patterns.
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
SOFTWARE DESIGN AND ARCHITECTURE
Introduction to Design Patterns
Behavioral Design Patterns
Basic Input and Output Operations
Algorithm Analysis CSE 2011 Winter September 2018.
object oriented Principles of software design
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
week 1 - Introduction Goals
Presented by Igor Ivković
Object-Oriented Design
Design Patterns Satya Puvvada Satya Puvvada.
Software Engineering Lecture 7 - Design Patterns
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Quicksort and Mergesort
Object Oriented Design Patterns - Structural Patterns
Object-Oriented Design
Design Patterns Difficult to describe abstractly Elements:
Introduction to Data Structure
Programming Languages and Paradigms
Standard Template Library
Chapter 8, Design Patterns Introduction
Presented by Igor Ivković
Software Design Lecture : 39.
SPL – PS1 Introduction to C++.
Presentation transcript:

Examples (D. Schmidt et al) Design Patterns Examples (D. Schmidt et al)

Table of Contents Case Studies Using Patterns 1. System Sort - e.g., Facade, Adapter, Iterator, Singleton, Factory, Strategy, Bridge 2. Sort Verifier - e.g., Strategy, Factory Method, Facade, Iterator, Singleton 3. Expression trees - e.g., Bridge, Factory

Introduction The following slides describe several case studies using C++ features and OO techniques to build highly extensible software C++ features include classes, inheritance, dynamic binding, and templates The OO techniques include design patterns and frameworks

Case Study 1: System Sort Develop a general-purpose system sort that sorts lines of text from standard input and writes the result to standard output - e.g., the UNIX system sort In the following, we'll examine the primary forces that shape the design of this application For each force, we'll examine patterns that resolve it

External Behavior of System Sort A "line" is a sequence of characters terminated by a newline Default ordering is lexicographic by bytes in machine collating sequence The ordering is affected globally by the following options: Ignore case Sort numerically Sort in reverse Begin sorting at a specified field Program need not sort files larger than main memory

Forces Solution should be both time and space efficient e.g., must use appropriate algorithms and data structures Efficient I/O and memory management are particularly important This solution uses no dynamic binding (to avoid overhead) Solution should leverage reusable components e.g., iostreams, Array and Stack classes, etc. Solution should yield reusable components e.g., efficient input classes, generic sort routines, etc.

General Form of Solution Note the use of existing C++ mechanisms like I/O streams template <class ARRAY> void sort (ARRAY &a); int main (int argc, char *argv[]) { parse.args (argc, argv); Input.Array input; cin >> input; sort (input); cout << input; }

General OOD Solution Approach Identify the "objects" in the problem and solution space e.g., stack, array, input class, options, access table, sorts, etc. Recognize and apply common design patterns e.g., Singleton, Factory, Adapter, Iterator Implement a framework to coordinate components e.g., use C++ classes and parameterized types

C++ Class Model System Sort Access Table Sort Array

C++ Class Components Strategic components System Sort Access Table Integrates everything… Access Table Used to store and sort input Options . Manages globally visible options - Sort e.g., both quicksort and insertion sort

C++ Class Components ffl Tactical components Input Efficiently reads arbitrary sized input using only dynamic allocation Stack Used by non-recursive quick sort Array Used by Access Table to store line and field pointers

Detailed Format for Solution Note the separation of concerns // Prototypes template <class ARRAY> sort (ARRAY &a); void operator >> (istream &, Access.Table &); void operator << (ostream &, const Access.Table &); int main (int argc, char *argv[]) { Options::instance ()->parse.args (argc, argv); cin >> System.Sort::instance ()->access.table (); sort (System.Sort::instance ()->access.table ()); cout << System.Sort::instance ()->access.table (); }

Reading Input Efficiently Problem: The input to the system sort can be arbitrarily large (e.g., up to size of main memory) Forces To improve performance solution must minimize Data copying and data manipulation Dynamic memory allocation Solution Create an Input class that reads arbitrary input efficiently

The Input Class Efficiently reads arbitrary sized input using only 1 dynamic allocation class Input { public: // Reads from <input> up to <terminator>, // replacing <search> with <replace>. Returns // pointer to dynamically allocated buffer. char *read (istream input& int terminator = EOF, int search = ‘\n', int replace = ‘\0') // Number of bytes replaced. int replaced (void); // Size of buffer. size.t size (const) const; private: // Recursive helper method. char *recursive.read (void); // ... };

Design Patterns in System Sort Facade "Provide a unified interface to a set of interfaces in a subsystem" Facade defines a higher-level interface that makes the subsystem easier to use e.g., sort provides a facade for the complex internal details of efficient sorting Adapter "Convert the interface of a class into another interface client expects" Adapter lets classes work together that couldn't otherwise because of incompatible interfaces e.g., make Access Table conform to interfaces expected by sort and iostreams

Design Patterns in System Sort (cont'd) Factory "Centralize the assembly of resources necessary to create an object" e.g., decouple Access Table Line Pointers initialization from their subsequent use Bridge Decouple an abstraction from its implementation so that the two can vary independently e.g., comparing two lines to determine ordering Strategy Define a family of algorithms, encapsulate each one, and make them interchangeable e.g., allow flexible pivot selection

Design Patterns in System Sort (cont'd) Singleton "Ensure a class has only one instance, and provide a global point of access to it" e.g., provides a single point of access for system sort and for program options Iterator "Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation" e.g., provides a way to print out the sorted lines without exposing representation

Sort Algorithm For efficiency, two types of sorting algorithms are used: Quicksort Highly time and space efficient sorting arbitrary data O(n lg n) average-case time complexity O(n2) worst-case time complexity – O(lg n) space complexity Optimizations are used to avoid worst-case behavior Insertion sort Highly time and space efficient for sorting "almost ordered" data O(n2) average- and worst-case time complexity O(1) space complexity

Quicksort Optimizations Non-recursive Uses an explicit stack to reduce function call overhead Median of 3 pivot selection Reduces probability of worse-case time complexity Guaranteed (lg n) space complexity Always "pushes" larger partition Insertion sort for small partitions Insertion sort runs fast on almost sorted data

The Strategy Pattern Intent This pattern resolves the following force Define a family of algorithms, encapsulate each one, and make them interchangeable Strategy lets the algorithm vary independently from clients that use it This pattern resolves the following force How to extend the policies for selecting a pivot value without modifying the main quicksort algorithm

Structure of the Strategy Pattern