java design patterns

Post on 29-Nov-2014

250 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Design Patterns in JAVA

Design Patterns in JAVA

Brian Zitzow, @bzitzowWeb 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 Objectdb = 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 Objectdb = 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 YOUThank YouThank You

top related