500 likes | 742 Views
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?
E N D
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 are they? • “A 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 + Coding Standards => Code
Design Patterns • Adaptors • Factories • Locks • Publisher-Subscriber (Communication) • Object-Manager Pattern
Adaptors http://www.dhgate.com/usb-universal-cell-phone-charger-adaptor/r-ff8080812d0e017c012d11bdfa9324c4.html
Adaptors • Create a simple, usable interface for interaction between classes • Performs similar functionality as a wrapper
Design Patterns • Adaptors • Factories • Locks • Publisher-Subscriber (Communication) • Object-Manager Pattern
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
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 overrideDbCommandCreateCommand() { return new SqlCommand(); }
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
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 (Communication) • Object-Manager Pattern
Locks http://i.ehow.com/images/a04/m5/ne/change-master-lock-combination-200X200.jpg
Locks • Prevent simultaneous access to a resource by multiple threads or processes. • Idea is simple • Implementation is difficult • Many Types • Semaphores, Mutexes…
Locks • Simple Code While (1) { myData = getLatestData(); lock = getLock(); if ( lock == myProcessID) { updateFile(myData); releaseLock(); } }
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
Locks - Example • Simple Example (Bad Design) P1 Score: 360 P2 Score: 365
Locks - Example • Simple Example (Good Design) P1 Score: 360 P2 Score: 365
Design Patterns • Adaptors • Factories • Locks • Publisher-Subscriber (Communication) • Object-Manager 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 ask for data they need/want • Publishers send information to all subscribers that want the data
Publisher-Subscriber subscriptions Consumer 1 Publisher 1 Consumer 2 Publisher 2 Consumer 3 Consumer 4 Publisher 3 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 (Communication) • Object-Manager Pattern
Object-Manager • Compilation of models • Useful 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 data is stored in the database • The object constructor handles retrieving data about the specific object from the database
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
Object-Manager: Objects • Example for Book Book -BookID -Title -Author +Book( bookID ) +GetID() +GetTitle() +GetAuthor() +SetTitle(title) +SetAuthor(author)
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
Object-Manager: Manager • Example BookManager BookManager +AddBook(book) +UpdateBook(book) +DeleteBook(book) +GetAllBooks() +GetNumBooks() …
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);
Question #1 • What type of pattern was used to create the ‘???’ object? Old Database Database ??? Data Collector
Question #1 • What type of pattern was used to create the ‘???’ object? • Answer: Adaptor Old Database Database ??? 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 database based on data in the given object. • Ex. UpdateBook (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?Answer: A lock to prevent the ‘updaters’ from overwriting each others data Data Updater 1 Updater 2 Updater 3
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”)
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”)
References [1] Martin, Robert C.. "Design Principles and Design Patterns". Retrieved 2000. http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf.