Intro to Design Patterns By Taylor Helsper Outline

Intro to Design Patterns By: Taylor Helsper

Outline � Introduction � Design Patterns Basics � Design Patterns ◦ Adapters ◦ Class Factories ◦ Locks � Design Patterns for Communication ◦ Publisher-Subscriber � My Suggestions ◦ Object-Manager Pattern � Conclusion

Introduction � What is this lecture? ◦ Part of a CSE 4000 Independent Study Course ◦ “Practical Issues in Software Engineering” � What’s the point? ◦ To provide practical information to students in Software Engineering topics

Design Pattern Basics � What � “A are they? general reusable solution to a commonly occurring problem in software design” [1]

Design Pattern Basics � When are they used? ◦ Every time you design software ◦ Your design should have a ‘theme’ based on the �Language �Hardware �Interfaces �Security �… ◦ These all influence the patterns you choose

Design Pattern Basics � Why are they important? Design Patterns => Design Code Formatting + => Code Coding Standards

Design Patterns � Adaptors � Factories � Locks � Publisher-Subscriber � Object-Manager (Communication) Pattern

Adaptors http: //www. dhgate. com/usb-universal-cell-phone-charger-adaptor/r-ff 8080812 d 0 e 017 c 012 d 11 bdfa 9324 c 4. html

Adaptors � Create a simple, usable interface for interaction between classes � Performs similar functionality as a wrapper

Adaptors - Example

Design Patterns � Adaptors � Factories � Locks � Publisher-Subscriber � Object-Manager (Communication) Pattern

Factories http: //buy-chrome-wheels. com/mustang-factory-oem-wheels. html http: //www. mustangmonthly. com/thehistoryof/mump_0409_ford_mus tang_dearborn_assembly_plant/photo_01. html

Factories � Class Factories dynamically create a class at runtime � Similar to a constructor, but offers advantages in some situations ◦ Creation decisions must be made based on external factors

Factories – Java Example Public override Db. Command Create. Command() { return new Sql. Command(); }

Factories – Objective C Example (i. Phone Programming) CGRect rect 1 = CGRect. Make(100, 100); CGRect rect 2 = CGRect. Make(190, 100, 100); if (CGRect. Intersects. Rect(rect 1, rect 2) == 1) NSLog(@"The rectangles intersect"); Else NSLog(@"The rectangles do not intersect"); http: //iphonedevelopertips. com/c/cgrect-cgsize-and-cgpoint-functions. html

Factories � Abstract Factories ◦ Create a specific type of object based on certain factors ◦ Ex. A Java class ‘GUIFactory’ would return an appropriately themed GUI class based on the current platform: Linux, OS X, or Windows

Design Patterns � Adaptors � Factories � Locks � Publisher-Subscriber � Object-Manager (Communication) Pattern

Locks http: //i. ehow. com/images/a 04/m 5/ne/change-master-lock-combination-200 X 200. jpg

Locks � Prevent simultaneous access to a resource by multiple threads or processes. � Idea is simple � Implementation � Many Types is difficult ◦ Semaphores, Mutexes…

Locks � Simple Code While (1) { my. Data = get. Latest. Data(); } lock = get. Lock(); if ( lock == my. Process. ID) { update. File(my. Data); release. Lock(); }

Locks - Example � Simple Example ◦ A multiplayer game needs access to update a high score list. ◦ Situation: Two players get new high scores at the same time. Current High Scores 350 335 310 280 240 P 1 Score: 360 P 2 Score: 365

Locks - Example � Simple Example (Bad Design) P 1 Score: 360 P 2 Score: 365 New High Scores 365 or 360 350 335 310 280

Locks - Example � Simple Example (Good Design) P 1 Score: 360 P 2 Score: 365 New High Scores 365 360 350 335 310

Design Patterns � Adaptors � Factories � Locks � Publisher-Subscriber � Object-Manager (Communication) Pattern

Publisher-Subscriber http: //sonicko. com/online-marketing/the-value-of-social-media-marketing/

Publisher-Subscriber � Model is very popular for online information sharing � Subscribers � Publishers ask for data they need/want send information to all subscribers that want the data

Publisher-Subscriber subscriptions Publisher 1 Publisher 2 Publisher 3 Consumer 1 Consumer 2 Consumer 3 Consumer 4 Consumer 5

Publisher-Subscriber Example � Twitter ◦ Each user is both a publisher and a subscriber ◦ Every “Follow” is a subscription ◦ Each tweet is one bit of data that all the followers/subscribers can see � This model is very common in social media

Design Patterns � Adaptors � Factories � Locks � Publisher-Subscriber � Object-Manager (Communication) Pattern

Object-Manager � Compilation � Useful of models for web projects � Classes represent data stored in the database as well as the interactions between the data

Object-Manager � Has three objects ◦ Pages (ex. PHP) ◦ Objects ◦ Managers Pages Managers Database Objects

Object-Manager: Pages � Pages are the PHP pages that are normally generated within a PHP project. � Pages can use the Objects and Managers � Same as using Classes in a C++ program

Object-Manager: Objects � Objects are classes that represent necessary system data � Object � The data is stored in the database object constructor handles retrieving data about the specific object from the database

Object-Manager: Objects � Example ◦ ◦ ◦ Classes for an Online Book Store User Book Sale Comment Rating � The ‘Book’ Object would have the same variables as the columns in the database

Object-Manager: Objects � Example for Book -Book. ID -Title -Author +Book( book. ID ) +Get. ID() +Get. Title() +Get. Author() +Set. Title(title) +Set. Author(author)

Object-Manager: Manager � Managers are static classes that contain functions for modifying the database � Every � Ex. object has an associated manager class Book Object -> Book. Manager

Object-Manager: Manager � Example Book. Manager +Add. Book(book) +Update. Book(book) +Delete. Book(book) +Get. All. Books() +Get. Num. Books() …

Object-Manager: Code � Code for adding/updating/deleting a book ◦ This would appear on a PHP page // Add a Book $b = new Book(-1); $b->set. Title(“Awesome Book”); $b->set. Author(“Person”); Book. Manager: : Add. Book($b); // Update Book $b = new Book(1); $b->set. Title(“Awesome Book 2”); $b->set. Author(“Person 2”); Book. Manager: : Update. Book($b); // Delete Book $b = new Book(1); Book. Manager: : Delete. Book($b);

Question #1 � What type of pattern was used to create the ‘? ? ? ’ object? Old Database ? ? ? Data Collector

Question #1 � What type of pattern was used to create the ‘? ? ? ’ object? Old Database ? ? ? � Answer: Adaptor Data Collector

Question #2 � What pattern do RSS News Feeds follow?

Question #2 � What pattern do RSS News Feeds follow? � Answer: Publisher-Subscriber

Question #3 � What do Managers do in the Object-Manager pattern?

Question #3 � What do Managers do in the Object-Manager pattern? � Answer: Managers update/insert/delete the databased on data in the given object. ◦ Ex. Update. Book (book) will update the database with the data in the book object.

Question #4 � What is the following pattern missing? Data Updater 1 Updater 2 Updater 3

Question #4 � What is the following pattern missing? Data Updater 1 Updater 2 Updater 3 Answer: A lock to prevent the ‘updaters’ from overwriting each others data

Question #5 � What is the name of the following pattern? Box 1= Box. Maker(10, 0, 10, 5, “red”) Box 2= Box. Maker(10, 5, “white”) Box 3= Box. Maker(0, 0, 10, “blue”)

Question #5 � What is the name of the following pattern? Box 1= Box. Maker(10, 0, 10, 5, “red”) Box 2= Box. Maker(10, 5, “white”) Box 3= Box. Maker(0, 0, 10, “blue”) � Answer: Class Factory
![References [1] Martin, Robert C. . "Design Principles and Design Patterns". Retrieved 2000. http: References [1] Martin, Robert C. . "Design Principles and Design Patterns". Retrieved 2000. http:](http://slidetodoc.com/presentation_image_h2/ad00e5f774226b48511a63c8f343df85/image-49.jpg)
References [1] Martin, Robert C. . "Design Principles and Design Patterns". Retrieved 2000. http: //www. objectmentor. com/resources/articles/Principles_and_Pat terns. pdf.

Questions?
- Slides: 50