1 / 50

Intro to Design Patterns

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?

gerek
Download Presentation

Intro to 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. Intro to Design Patterns By: Taylor Helsper

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

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

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

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

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

  7. Design Patterns • Adaptors • Factories • Locks • Publisher-Subscriber (Communication) • Object-Manager Pattern

  8. Adaptors http://www.dhgate.com/usb-universal-cell-phone-charger-adaptor/r-ff8080812d0e017c012d11bdfa9324c4.html

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

  10. Adaptors - Example

  11. Design Patterns • Adaptors • Factories • Locks • Publisher-Subscriber (Communication) • Object-Manager Pattern

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

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

  14. Factories – Java Example Public overrideDbCommandCreateCommand() { return new SqlCommand(); }

  15. Factories – Objective C Example(iPhone Programming) CGRect rect1 = CGRectMake(100, 100, 100, 100); CGRect rect2 = CGRectMake(190, 190, 100, 100); if(CGRectIntersectsRect(rect1, rect2)==1) NSLog(@"The rectangles intersect"); Else NSLog(@"The rectangles do not intersect"); http://iphonedevelopertips.com/c/cgrect-cgsize-and-cgpoint-functions.html

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

  17. Design Patterns • Adaptors • Factories • Locks • Publisher-Subscriber (Communication) • Object-Manager Pattern

  18. Locks http://i.ehow.com/images/a04/m5/ne/change-master-lock-combination-200X200.jpg

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

  20. Locks • Simple Code While (1) { myData = getLatestData(); lock = getLock(); if ( lock == myProcessID) { updateFile(myData); releaseLock(); } }

  21. 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. P1 Score: 360 P2 Score: 365

  22. Locks - Example • Simple Example (Bad Design) P1 Score: 360 P2 Score: 365

  23. Locks - Example • Simple Example (Good Design) P1 Score: 360 P2 Score: 365

  24. Design Patterns • Adaptors • Factories • Locks • Publisher-Subscriber (Communication) • Object-Manager Pattern

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

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

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

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

  29. Design Patterns • Adaptors • Factories • Locks • Publisher-Subscriber (Communication) • Object-Manager Pattern

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

  31. Object-Manager • Has three objects • Pages (ex. PHP) • Objects • Managers Pages Managers Database Objects

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

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

  34. Object-Manager: Objects • Example Classesfor an Online Book Store • User • Book • Sale • Comment • Rating • The ‘Book’ Object would have the same variables as the columns in the database

  35. Object-Manager: Objects • Example for Book Book -BookID -Title -Author +Book( bookID ) +GetID() +GetTitle() +GetAuthor() +SetTitle(title) +SetAuthor(author)

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

  37. Object-Manager: Manager • Example BookManager BookManager +AddBook(book) +UpdateBook(book) +DeleteBook(book) +GetAllBooks() +GetNumBooks() …

  38. 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->setTitle(“Awesome Book”); $b->setAuthor(“Person”); BookManager::AddBook($b); // Update Book $b = new Book(1); $b->setTitle(“Awesome Book 2”); $b->setAuthor(“Person 2”); BookManager::UpdateBook($b); // Delete Book $b = new Book(1); BookManager::DeleteBook($b);

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

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

  41. Question #2 • What pattern do RSS News Feeds follow?

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

  43. Question #3 • What do Managers do in the Object-Manager pattern?

  44. Question #3 • What do Managers do in the Object-Manager pattern? • Answer: Managers update/insert/delete the database based on data in the given object. • Ex. UpdateBook (book) will update the database with the data in the book object.

  45. Question #4 • What is the following pattern missing? Data Updater 1 Updater 2 Updater 3

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

  47. Question #5 • What is the name of the following pattern? Box1= BoxMaker(10,0,10,5, “red”) Box2= BoxMaker(10,5,10,5, “white”) Box3= BoxMaker(0,0,10,10, “blue”)

  48. Question #5 • What is the name of the following pattern? • Answer: Class Factory Box1= BoxMaker(10,0,10,5, “red”) Box2= BoxMaker(10,5,10,5, “white”) Box3= BoxMaker(0,0,10,10, “blue”)

  49. References [1] Martin, Robert C.. "Design Principles and Design Patterns". Retrieved 2000. http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf.

  50. Questions?

More Related