1 / 21

Design Patterns

Design Patterns. Examples (D. Schmidt et al). Table of Contents.

Download Presentation

Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Table of Contents • Case Studies Using Patterns1. System Sort- e.g., Facade, Adapter, Iterator, Singleton, Factory, Strategy, Bridge2. Sort Verifier- e.g., Strategy, Factory Method, Facade, Iterator, Singleton3. Expression trees- e.g., Bridge, Factory

  3. 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

  4. 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

  5. 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

  6. 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.

  7. 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; }

  8. 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

  9. Sort C++ Class Model System Sort Access Table Array

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

  11. 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

  12. 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 (); }

  13. 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

  14. 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);// ... };

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. The Strategy Pattern • Intent • 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

  21. Structure of the Strategy Pattern

More Related