What is Design Pattern?
These are well proved solutions to any specific problem/task.
Eg.
Suppose you want to create a class for which only a single instance (or object) should be created and that single object can be used by all other classes.
Ans -Singleton Design Pattern
Advantages -
1.Reusable
2.Define System Architecture
3.Better System
4.Easy to scale and maintain app
When to use Design Pattern?
During the analysis and requirement phase of SDLC(Software Development Life Cycle).
Classification -
1.Core Java Design Pattern
2.J2EE Design Pattern
A.Core Java Design Patterns-
1.Creational Design Pattern -
Singleton Design Pattern -
It defines a class that has only one instance and provide global access to it.
It has two types -
1.Early Initialization - Instance creation when class is loaded
2.Lazy Initialization - Instance creation when required(on calling getInstance())
How to create?
Private & Static variable - To get memory once.
Private Constructor - To prevent instance creation from outside class
Static Factory Method - For global access to user to the Singleton Object
With Early Initialization
With lazy Initialization
Above eg. Singleton is A class
Significance Of Classloader in Singleton -
If singleton is loaded by two classloader then 2 instances of Singleton will be created.
Each singleton instance per each Classloader.
Significance Of Serialization in Singleton -
We can serialize and deserialize Singleton instance.
But on deserializing it will not return Singleton object.
Solution -
Override readResolve() to enforce to give Singleton Object.
Singleton Real Life Example from JDK -
1.Runtime (java.lang.Runtime). There is only one Runtime instance for Java Application.
2.Toolkit (java.awt.Toolkit)
3.Desktop (java.awt.Desktop)
Which class can be made Singleton ?
The class which can be available for whole application and only one instance is available throughout application.
Eg.
Real Life example could be GUI Popup.
We can make sure that at a time only one popup is available on the screen.
Factory Method Pattern -
It defines an Interface or Abstract class to create object but let sub-classes decide which class to be instantiated.
In short, subclasses are responsible for creation on object.
Advantages -
1.It let subclasses choose which object to create
2.It promotes loose-coupling by eliminating the need to bind classes into code.
It means code interacts with Interface or Abstract Classes.
Eg.Electricity Bill Calculation
http://www.javatpoint.com/factory-method-design-pattern
Prototype Design Pattern -
It helps in cloning of existing object instead of creating new one.
And new cloned object can be customized as per requirement.
When to use?
When price of creating object using new keyword is expensive.
Advantages -
1.Hides complexities for creating new object.
2.You can add or remove object at runtime.
Eg.Employee Record
http://www.javatpoint.com/prototype-design-pattern
Proxy Design Pattern -
Proxy means object representing another object.
It provides control over access of original object.
Advantage-
It protects original object from outside world.
When to use-
Protective proxy server
Eg.Proxy Internet Access
http://www.javatpoint.com/proxy-pattern
These are well proved solutions to any specific problem/task.
Eg.
Suppose you want to create a class for which only a single instance (or object) should be created and that single object can be used by all other classes.
Ans -Singleton Design Pattern
Advantages -
1.Reusable
2.Define System Architecture
3.Better System
4.Easy to scale and maintain app
When to use Design Pattern?
During the analysis and requirement phase of SDLC(Software Development Life Cycle).
Classification -
1.Core Java Design Pattern
2.J2EE Design Pattern
A.Core Java Design Patterns-
1.Creational Design Pattern -
Singleton Design Pattern -
It defines a class that has only one instance and provide global access to it.
It has two types -
1.Early Initialization - Instance creation when class is loaded
2.Lazy Initialization - Instance creation when required(on calling getInstance())
How to create?
Private & Static variable - To get memory once.
Private Constructor - To prevent instance creation from outside class
Static Factory Method - For global access to user to the Singleton Object
With Early Initialization
With lazy Initialization
Above eg. Singleton is A class
Significance Of Classloader in Singleton -
If singleton is loaded by two classloader then 2 instances of Singleton will be created.
Each singleton instance per each Classloader.
Significance Of Serialization in Singleton -
We can serialize and deserialize Singleton instance.
But on deserializing it will not return Singleton object.
Solution -
Override readResolve() to enforce to give Singleton Object.
Singleton Real Life Example from JDK -
1.Runtime (java.lang.Runtime). There is only one Runtime instance for Java Application.
2.Toolkit (java.awt.Toolkit)
3.Desktop (java.awt.Desktop)
Which class can be made Singleton ?
The class which can be available for whole application and only one instance is available throughout application.
Eg.
Real Life example could be GUI Popup.
We can make sure that at a time only one popup is available on the screen.
Factory Method Pattern -
It defines an Interface or Abstract class to create object but let sub-classes decide which class to be instantiated.
In short, subclasses are responsible for creation on object.
Advantages -
1.It let subclasses choose which object to create
2.It promotes loose-coupling by eliminating the need to bind classes into code.
It means code interacts with Interface or Abstract Classes.
Eg.Electricity Bill Calculation
http://www.javatpoint.com/factory-method-design-pattern
Prototype Design Pattern -
It helps in cloning of existing object instead of creating new one.
And new cloned object can be customized as per requirement.
When to use?
When price of creating object using new keyword is expensive.
Advantages -
1.Hides complexities for creating new object.
2.You can add or remove object at runtime.
Eg.Employee Record
http://www.javatpoint.com/prototype-design-pattern
Proxy Design Pattern -
Proxy means object representing another object.
It provides control over access of original object.
Advantage-
It protects original object from outside world.
When to use-
Protective proxy server
Eg.Proxy Internet Access
http://www.javatpoint.com/proxy-pattern
No comments:
Post a Comment