Your SlideShare is downloading. ×
Java design patterns
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Java design patterns

148
views

Published on


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
148
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Design Patterns in JAVA
  • 2. Design Patterns in JAVABrian Zitzow, @bzitzowWeb Developer, Student, FatherMichael Kirby
  • 3. Sasiwipa NakdeeTwitter: @lilwipa● Girlfriend● Puts up with me● Feeds me after class● Thank You Sasi!
  • 4. Design PatternsHuh?
  • 5. Design PatternsIn software engineering, a design pattern is a generalreusable solution to a commonly occurring problem within agiven context in software design.A design pattern is not a finished design that can betransformed directly into source or machine code. It is adescription or template for how to solve a problem that canbe used in many different situations.Patterns are formalized best practices that theprogrammer must implement themselves in the application.
  • 6. 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 Stuffdb.closeConnection();
  • 7. Every time we want to use the database● Get database credentials● Instantiate a new database object● Open and close database connection
  • 8. LoopingDirectoryScanner dirScan = new DirectoryScanner();List<File> files = dirScan.getList(dir);for (int i = 0; i < files.size(); i++) {// Instantiate Database// Open Connection// Close Connection}
  • 9. Looping Overhead● Every iteration requires:– Access to database credentials– New object with every iteration– Open & Close the resource
  • 10. Looping Overhead ExampleSuperD (File Duplication Checker)● 52,000+ Database Connections● 8GB of RAM used● System Locked● Application Crashed
  • 11. Looping Overhead – Solved!DirectoryScanner dirScan = new DirectoryScanner();List<File> files = dirScan.getList(dir);// Instantiate Database// Open Connectionfor (int i = 0; i < files.size(); i++) {// Insert & Query Stuff}// Close Connection
  • 12. 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}
  • 13. What about multiple files?● Duplicate Code (load credentials every time)● Multiple instances of same object– Instantiate an open resource multiple times isresource 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.
  • 14. Solution?
  • 15. The Singleton Pattern
  • 16. The Singleton PatternEnsures a class has only one instance, andprovides a global point of access to it.All further references to objects of the singletonclass refer to the same underlying instance.
  • 17. The Singleton PatternWithout 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 Stuffdb.closeConnection();With Pattern:Database.getInstance().execute(sql);Database.getInstance().closeConnection();
  • 18. How does it work?● Private Constructor● Static Method● Static Property
  • 19. Private ConstructorPublic Class Database{private Database() {}}
  • 20. Private Constructor● Object cannot be instantiated outside itself● Weird, right?● Can never, ever have more than one instance● What about the first instance?
  • 21. Static MethodPublic Class Database{Public static H2 getInstance() {}}
  • 22. 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
  • 23. Static PropertyPublic Class Database{private static H2 uniqueInstance;}
  • 24. Static Property● Private - can only be set within the class● Static - static methods cannot accessinstance variables. They can, however, accessclass variables aka static properties.
  • 25. The SingletonPublic 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() {}}
  • 26. The SingletonDatabase.getInstance().execute(sql);Database.getInstance().closeConnection();
  • 27. The Singleton● Only one instance can ever be created● Global access point, static method()● Contributors can safely use in or out of loopGotchas:● Multi-Threading, tweak it slightly :)
  • 28. THANK YOUThank YouThank You