Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java design patterns


Published on

  • Be the first to comment

Java design patterns

  1. 1. Design Patterns in JAVA
  2. 2. Design Patterns in JAVABrian Zitzow, @bzitzowWeb Developer, Student, FatherMichael Kirby
  3. 3. Sasiwipa NakdeeTwitter: @lilwipa● Girlfriend● Puts up with me● Feeds me after class● Thank You Sasi!
  4. 4. Design PatternsHuh?
  5. 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. 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. 7. Every time we want to use the database● Get database credentials● Instantiate a new database object● Open and close database connection
  8. 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. 9. Looping Overhead● Every iteration requires:– Access to database credentials– New object with every iteration– Open & Close the resource
  10. 10. Looping Overhead ExampleSuperD (File Duplication Checker)● 52,000+ Database Connections● 8GB of RAM used● System Locked● Application Crashed
  11. 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. 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. 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. 14. Solution?
  15. 15. The Singleton Pattern
  16. 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. 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. 18. How does it work?● Private Constructor● Static Method● Static Property
  19. 19. Private ConstructorPublic Class Database{private Database() {}}
  20. 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. 21. Static MethodPublic Class Database{Public static H2 getInstance() {}}
  22. 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. 23. Static PropertyPublic Class Database{private static H2 uniqueInstance;}
  24. 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. 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. 26. The SingletonDatabase.getInstance().execute(sql);Database.getInstance().closeConnection();
  27. 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. 28. THANK YOUThank YouThank You