DESIGN PATTERNS
Kaushal Shah
WHAT ARE DESIGN PATTERNS
 Solutions to common application design problems
 Solve problems associated with Object creation and interaction
 Evolved over a long period of time and provide best solutions to recurring problems
 Provide boilerplates that can be applied to real life
TYPES OF DESIGN PATTERNS
 Creational Patterns
 Behavioral Patterns
 Structural Patterns
ENTERPRISE JAVA PATTERNS
 J2EE often contains excessive ‘plumbing’ code like JNDI lookup, XML configuration,
try catch blocks that acquire and release JDBC resources
 Maintaining such code was a drain of resources
 Enterprise Java Patterns solve these problems
 Some of these patterns evolved from the ‘classic’ patterns, others were new and
addressed the flaws of J2EE
DESIGN PATTERNS V/S ENTERPRISE PATTERNS
 Enterprise patterns target enterprise software and it’s problems which differ from
problems of desktop applications
 In Java EE 7 most design patterns are embedded in the platform, ready to be used
 Most of the patterns can be implemented via annotations without XML
 Enterprise software development requires discipline and coordination between
developers.
 Design patterns are a good way to approach this problem
IMPORTANCE OF DESIGN PATTERNS
 Gives the developers a common ‘vocabulary’
 Code becomes more readable and clean
 Refactoring the code is very simple
 Tested and sound solutions
Software can be made without design patterns, but it’s a lot harder.
Anti-Patterns
• Design patterns represent
collected wisdom
• Does not mean we need to
use them all the time
• Over using design patterns
tends to overcomplicate the
system
• May lead to poor
performance
MULTI TIER
ARCHITECTURE
MULTI TIER ARCHITECTURE
Java EE architecture is separated into tiers:
 Client/Presentation Tier: Usually browsers that connects to Java EE via HTTP
 EIS Tier: Databases or any resource that provides data
 Middle Tier: Consists of web container and EJB container
MIDDLE TIER
BUSINESS LAYER:
 Executes business logic
 Involves data retrieved from the EIS Tier
WEB LAYER:
 Manages interactions between Client and Business layer
 Receives and processes request from client for a resource
 Maintains user’s states in a session
CONTEXT AND DEPENDENCY INJECTION
 Decouples relation between a component and it’s dependencies
 Injects dependencies into an Object rather than object creating dependency by
using ‘new’ keyword
 Can easily swap out dependency for another compatible object
INTERCEPTORS
 Used in Java EE to address ‘cross cutting concerns’
 Common concerns like Logging, Security that are not related to the business logic
SINGLETON
PATTERN
WHAT IS A SINGLETON ?
 Ensures a class only has one instance
 Provides a global point of access to it
 Falls under Creational design patterns
 Spring uses singleton when creating beans
IMPLEMENTATIONS
THREAD SAFETY
PRE-LOADING THE CLASS
LAZY INITIALIZATION
DOUBLE CHECKING
ENUM IMPLEMENTATION
SINGLETON BEANS
SINGLETONS AT STARTUP
STARTUP ORDER
WHEN AND WHERE TO USE SINGLETON
 Access shared data across the whole application domain
 To load and cache expensive resources only once
 To create an application logger
 To manage objects within a class implementing Factory pattern
 To create a Façade object
Heavy use of Singleton sign of misuse
THANK YOU !!

Design Patterns

  • 1.
  • 2.
    WHAT ARE DESIGNPATTERNS  Solutions to common application design problems  Solve problems associated with Object creation and interaction  Evolved over a long period of time and provide best solutions to recurring problems  Provide boilerplates that can be applied to real life
  • 3.
    TYPES OF DESIGNPATTERNS  Creational Patterns  Behavioral Patterns  Structural Patterns
  • 4.
    ENTERPRISE JAVA PATTERNS J2EE often contains excessive ‘plumbing’ code like JNDI lookup, XML configuration, try catch blocks that acquire and release JDBC resources  Maintaining such code was a drain of resources  Enterprise Java Patterns solve these problems  Some of these patterns evolved from the ‘classic’ patterns, others were new and addressed the flaws of J2EE
  • 5.
    DESIGN PATTERNS V/SENTERPRISE PATTERNS  Enterprise patterns target enterprise software and it’s problems which differ from problems of desktop applications  In Java EE 7 most design patterns are embedded in the platform, ready to be used  Most of the patterns can be implemented via annotations without XML  Enterprise software development requires discipline and coordination between developers.  Design patterns are a good way to approach this problem
  • 6.
    IMPORTANCE OF DESIGNPATTERNS  Gives the developers a common ‘vocabulary’  Code becomes more readable and clean  Refactoring the code is very simple  Tested and sound solutions Software can be made without design patterns, but it’s a lot harder.
  • 7.
    Anti-Patterns • Design patternsrepresent collected wisdom • Does not mean we need to use them all the time • Over using design patterns tends to overcomplicate the system • May lead to poor performance
  • 8.
  • 9.
    MULTI TIER ARCHITECTURE JavaEE architecture is separated into tiers:  Client/Presentation Tier: Usually browsers that connects to Java EE via HTTP  EIS Tier: Databases or any resource that provides data  Middle Tier: Consists of web container and EJB container
  • 10.
    MIDDLE TIER BUSINESS LAYER: Executes business logic  Involves data retrieved from the EIS Tier WEB LAYER:  Manages interactions between Client and Business layer  Receives and processes request from client for a resource  Maintains user’s states in a session
  • 11.
    CONTEXT AND DEPENDENCYINJECTION  Decouples relation between a component and it’s dependencies  Injects dependencies into an Object rather than object creating dependency by using ‘new’ keyword  Can easily swap out dependency for another compatible object
  • 12.
    INTERCEPTORS  Used inJava EE to address ‘cross cutting concerns’  Common concerns like Logging, Security that are not related to the business logic
  • 13.
  • 14.
    WHAT IS ASINGLETON ?  Ensures a class only has one instance  Provides a global point of access to it  Falls under Creational design patterns  Spring uses singleton when creating beans
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
    WHEN AND WHERETO USE SINGLETON  Access shared data across the whole application domain  To load and cache expensive resources only once  To create an application logger  To manage objects within a class implementing Factory pattern  To create a Façade object Heavy use of Singleton sign of misuse
  • 25.

Editor's Notes

  • #8 Patterns work best when the conditions and the problems require their use.
  • #22 Initialized lazily by default