6. Design Patterns
In software engineering, a design pattern is a general
reusable solution to a commonly occurring problem within a
given context in software design.
A design pattern is not a finished design that can be
transformed directly into source or machine code. It is a
description or template for how to solve a problem that can
be used in many different situations.
Patterns are formalized best practices that the
programmer must implement themselves in the application.
7. Before Inserting Data ...
H2 db = null;
dbUrl = “db://database/location”;
dbUser = “username”;
dbPass = “password”;
// Initialize Database Object
db = new H2(dbUrl, dbUser, dbPass);
db.openConnection();
// Insert Data, Query Data, Do Stuff
db.closeConnection();
8. Every time we want to use the database
● Get database credentials
● Instantiate a new database object
● Open and close database connection
9. Looping
DirectoryScanner dirScan = new DirectoryScanner();
List<File> files = dirScan.getList(dir);
for (int i = 0; i < files.size(); i++) {
// Instantiate Database
// Open Connection
// Close Connection
}
10. Looping Overhead
● Every iteration requires:
– Access to database credentials
– New object with every iteration
– Open & Close the resource
11. Looping Overhead Example
SuperD (File Duplication Checker)
● 52,000+ Database Connections
● 8GB of RAM used
● System Locked
● Application Crashed
12. Looping Overhead – Solved!
DirectoryScanner dirScan = new DirectoryScanner();
List<File> files = dirScan.getList(dir);
// Instantiate Database
// Open Connection
for (int i = 0; i < files.size(); i++) {
// Insert & Query Stuff
}
// Close Connection
13. What about multiple files?
Public Class UserFiles
{
// Database Credentials
// Instantiate Database
// Open Connection
// Close Connection
}
Public class DupeChecker
{
// Database Credentials
// Instantiate Database
// Open Connection
// Close Connection
}
14. What about multiple files?
● Duplicate Code (load credentials every time)
● Multiple instances of same object
– Instantiate an open resource multiple times is
resource intensive
● Could inject into objects
– Creates object dependencies AKA coupling
– Adds to object complexity
● Contributors (are drunk, stressed, naïve,
resentful or otherwise unaware and)
instantiate Database objects within the loops.
19. The Singleton Pattern
Ensures a class has only one instance, and
provides a global point of access to it.
All further references to objects of the singleton
class refer to the same underlying instance.
20. The Singleton Pattern
Without Pattern:
H2 db = null;
dbUrl = “db://database/location”;
dbUser = “username”;
dbPass = “password”;
// Initialize Database Object
db = new H2(dbUrl, dbUser, dbPass);
db.openConnection();
// Insert Data, Query Data, Do Stuff
db.closeConnection();
With Pattern:
Database.getInstance().execute(sql);
Database.getInstance().closeConnection();
21. How does it work?
● Private Constructor
● Static Method
● Static Property
23. Private Constructor
● Object cannot be instantiated outside itself
● Weird, right?
● Can never, ever have more than one instance
● What about the first instance?
25. Static Method
● Public - can be accessed anywhere
● Static - can be accessed w/out instantiation
● Checks if instance of object exists
– If true : return instance
– If false: instantiate, assign, and return
27. Static Property
● Private - can only be set within the class
● Static - static methods cannot access
instance variables. They can, however, access
class variables aka static properties.
28. The Singleton
Public Class Database
{
private static H2 uniqueInstance;
public static H2 getInstance() {
if (uniqueInstance == null) {
this.uniqueInstance = new Database();
this.uniqueInstance.openConncetion();
}
return this.uniqueInstance;
}
private Database() {}
}
30. The Singleton
● Only one instance can ever be created
● Global access point, static method()
● Contributors can safely use in or out of loop
Gotchas:
● Multi-Threading, tweak it slightly :)