Intro to Design Patterns - Mississippi State University

By: Taylor Helsper



Introduction
Design Patterns Basics
Design Patterns
◦ Adapters
◦ Class Factories
◦ Locks

Design Patterns for Communication
◦ Publisher-Subscriber

My Suggestions

Conclusion
◦ Object-Manager Pattern

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


What are they?
“A general reusable solution to a commonly
occurring problem in software design” [1]

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

Why are they important?
Design Patterns => Design
Code Formatting
+
=> Code
Coding Standards

Adaptors

Factories

Locks

Publisher-Subscriber (Communication)

Object-Manager Pattern
http://www.dhgate.com/usb-universal-cell-phone-charger-adaptor/r-ff8080812d0e017c012d11bdfa9324c4.html


Create a simple, usable interface for
interaction between classes
Performs similar functionality as a wrapper

Adaptors

Factories

Locks

Publisher-Subscriber (Communication)

Object-Manager Pattern
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


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
Public override DbCommand CreateCommand()
{
return new SqlCommand();
}
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

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

Adaptors

Factories

Locks

Publisher-Subscriber (Communication)

Object-Manager Pattern
http://i.ehow.com/images/a04/m5/ne/change-master-lock-combination-200X200.jpg

Prevent simultaneous access to a resource by
multiple threads or processes.

Idea is simple

Implementation is difficult

Many Types
◦ Semaphores, Mutexes…

Simple Code
While (1)
{
myData = getLatestData();
}
lock = getLock();
if ( lock == myProcessID)
{
updateFile(myData);
releaseLock();
}

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
P1 Score: 360
P2 Score: 365

Simple Example (Bad Design)
P1 Score: 360
P2 Score: 365
New High Scores
365 or 360
350
335
310
280

Simple Example (Good Design)
P1 Score: 360
P2 Score: 365
New High Scores
365
360
350
335
310

Adaptors

Factories

Locks

Publisher-Subscriber (Communication)

Object-Manager Pattern
http://sonicko.com/online-marketing/the-value-of-social-media-marketing/



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
subscriptions
Consumer 1
Publisher 1
Consumer 2
Publisher 2
Consumer 3
Consumer 4
Publisher 3
Consumer 5

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

Adaptors

Factories

Locks

Publisher-Subscriber (Communication)

Object-Manager Pattern

Compilation of models

Useful for web projects

Classes represent data stored in the database
as well as the interactions between the data

Has three objects
◦ Pages (ex. PHP)
◦ Objects
◦ Managers
Pages
Managers
Database
Objects

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



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

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

Example for Book
Book
-BookID
-Title
-Author
+Book( bookID )
+GetID()
+GetTitle()
+GetAuthor()
+SetTitle(title)
+SetAuthor(author)

Managers are static classes that contain
functions for modifying the database

Every object has an associated manager class

Ex. Book Object -> BookManager

Example BookManager
BookManager
+AddBook(book)
+UpdateBook(book)
+DeleteBook(book)
+GetAllBooks()
+GetNumBooks()
…

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);

What type of pattern was used to create the
‘???’ object?
Old
Database
Database
???
Data
Collector

What type of pattern was used to create the
‘???’ object?
Old
Database
Database
???

Answer: Adaptor
Data
Collector

What pattern do RSS News Feeds follow?

What pattern do RSS News Feeds follow?

Answer: Publisher-Subscriber

What do Managers do in the Object-Manager
pattern?


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.

What is the following pattern missing?
Data
Updater 1
Updater 2
Updater 3

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

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”)

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”)

Answer: Class Factory
[1] Martin, Robert C.. "Design Principles and Design Patterns".
Retrieved 2000.
http://www.objectmentor.com/resources/articles/Principles_and_Pa
tterns.pdf.