Design Patterns
What is a Design Pattern? Generic pattern for solving a certain class of problem Learn to recognize the class of problem and apply a solution you already know A more structured/systematic way of thinking about program design
Common Patterns Functor Adapter Iterator Composite Observer
Functor A class with a single function and no data Often used to compare two objects Pass to a function objects to be compared and a functor object that can compare them Example: a different implementation of SortedList More generally: you may want to have a class that knows how to perform a function on given data, but does not store the data or results
Adapter/Wrapper Change or extend the interface of a class –May also wrap primitive types Example: miles to km
Iterator Goal: an interface that will allow the programmer to traverse through any collection Operations: bool hasNext() const Object & next() Example: iterate through SortedList of names
Iterator Example Code
Composite An object containing two other objects –Key, value pairs Idea: group together so that both can be returned from a function Example from Project 1?