KEMBAR78
Java design patterns | PDF
Design Patterns in JAVA
Design Patterns in JAVA
Brian Zitzow, @bzitzow
Web Developer, Student, Father
Michael Kirby
Sasiwipa Nakdee
Twitter: @lilwipa
● Girlfriend
● Puts up with me
● Feeds me after class
● Thank You Sasi!
Design Patterns
Huh?
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.
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();
Every time we want to use the database
● Get database credentials
● Instantiate a new database object
● Open and close database connection
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
}
Looping Overhead
● Every iteration requires:
– Access to database credentials
– New object with every iteration
– Open & Close the resource
Looping Overhead Example
SuperD (File Duplication Checker)
● 52,000+ Database Connections
● 8GB of RAM used
● System Locked
● Application Crashed
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
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
}
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.
Solution?
The Singleton Pattern
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.
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();
How does it work?
● Private Constructor
● Static Method
● Static Property
Private Constructor
Public Class Database
{
private Database() {}
}
Private Constructor
● Object cannot be instantiated outside itself
● Weird, right?
● Can never, ever have more than one instance
● What about the first instance?
Static Method
Public Class Database
{
Public static H2 getInstance() {
}
}
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
Static Property
Public Class Database
{
private static H2 uniqueInstance;
}
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.
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() {}
}
The Singleton
Database.getInstance().execute(sql);
Database.getInstance().closeConnection();
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 :)
THANK YOU
Thank YouThank You

Java design patterns

  • 2.
  • 3.
    Design Patterns inJAVA Brian Zitzow, @bzitzow Web Developer, Student, Father Michael Kirby
  • 4.
    Sasiwipa Nakdee Twitter: @lilwipa ●Girlfriend ● Puts up with me ● Feeds me after class ● Thank You Sasi!
  • 5.
  • 6.
    Design Patterns In softwareengineering, 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 wewant 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 ● Everyiteration 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 multiplefiles? 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 multiplefiles? ● 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.
  • 15.
  • 18.
  • 19.
    The Singleton Pattern Ensuresa 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 WithoutPattern: 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 itwork? ● Private Constructor ● Static Method ● Static Property
  • 22.
    Private Constructor Public ClassDatabase { private Database() {} }
  • 23.
    Private Constructor ● Objectcannot be instantiated outside itself ● Weird, right? ● Can never, ever have more than one instance ● What about the first instance?
  • 24.
    Static Method Public ClassDatabase { Public static H2 getInstance() { } }
  • 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
  • 26.
    Static Property Public ClassDatabase { private static H2 uniqueInstance; }
  • 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 ClassDatabase { private static H2 uniqueInstance; public static H2 getInstance() { if (uniqueInstance == null) { this.uniqueInstance = new Database(); this.uniqueInstance.openConncetion(); } return this.uniqueInstance; } private Database() {} }
  • 29.
  • 30.
    The Singleton ● Onlyone 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 :)
  • 31.