Search

Showing posts with label Design Pattern. Show all posts
Showing posts with label Design Pattern. Show all posts

Java Design Patterns

                                                DESIGN PATTERNS
-Design patterns are the best solution in the form of set of rules for reoccurring problems of application development.
-Design patterns are the best practices to use software technologies effectively and efficiently.
-Design patterns are there in every software technology but we generally implement them in large scale application development.
-Senior programmers give set of best solutions for reoccurring problems based on their experience later these will be documented as design patterns.
-The worst solution for reoccurring problems is called anti pattern. The organization ISO will maintain the info about both anti patterns and design patterns.
-There are 500+ design patterns in java/j2ee technologies out of them the industry uses 20+ design patterns regularly almost in every project.
CORE JAVA DESIGN PATTERNS:
1.      Factory method
2.      Singleton
3.      Factory
4.      Abstract factory
5.      Template method
6.      IOC/ Dependency Injection
7.      Command line view
8.      Value object/ Data Transfer Object(V.O/D.T.O)
9.      Adaptor
10.  Fast line Reader
WEB DESIGN PATTERNS:
1.      MVC
2.      View Helper (JSP)
3.      Composite view
4.      Web Context
5.      Front Controller
6.      Intercepting Filter
7.      Application Controller
INTEGRATION TIER:
1.      Service Locator
2.      Business Delegate
3.      EJB Home Factory
BUSINESS TIER:
1.      Business Object
2.      Session Façade
3.      Message Façade
4.      DAO

CORE JAVA DESIGN PATTERNS:
Factory Method:
-A method in java class that is capable of constructing its own java class object is called as factory method.
-Factory method is useful to create object of a class outside the class when class is having only private constructors.
Syntax:
public static className method-name(parameters){
return obj;
}
Rules:
1.      Return type of method must be its own class name.
2.      Method must be taken as static method.
3.      Method must have either public or default access specifier.
Ex:
Thread t=Thread.currentThread();
String s=String.valueOf(10);
Class c=Class.forName(------);
class Test{                                                                    class Test{
public static Test xyz(){ //factory method        public static ABC xyz(){
-          -                                                                                               - -
-          -                                                                                               - -
return new Test();                                                 return new ABC();
}                                                                                             }
}                                                                                            }
SINGLETON DESING PATTERN:
-          The java class which allows to create only one object per JVM is called singleton java class.
-          If you open multiple command prompt to execute multiple java applications there will be multiple JVM’s.
-          Instead of creating multiple objects for a class having same data it is recommended to create only one object and use it for multiple number of times. So that objects creation and destruction time will not be wasted.
-          Most of the jdbc driver classes are designed as singleton java classes.
Rules:
1.      Class should contain only private constructors.
2.      Class should contain factory method having singleton logic.
Q: why there should be only private constructors in singleton java class?
A: To close all the doors of creating object outside and to allow only through factory method which contains singleton logic.
Ex:
// singleton test
public class SingletonTest{
private static SingletonTest st=null;
private SingletonTest(){
System.out.println(“constructor”);
}
//factory method
Public static SingletonTest create(){
If(st==null)
st=new SingletonTest();
return st;
}
public static void main(String []args){
create();
create();
create();
}
}
-In order to allow multiple applications using single object we start multiple Threads on the object. This process may generate multi threading issues. To overcome this problem we go for synchronized singleton java class.
Factory Design pattern:
-The java class or object that is capable of constructing other class objects is called factory class or factory object.
-In EJB architecture HomeOject is called as Factory Object because it is capable of creating EJB Objects dynamically.
-In the below example Test class is called Factory class because it is capable of creating ABC, MNO class objects.
Ex:
 class Test{
public static object xyz(){
-          -
-          -                                                           ABC abc=(ABC)Test.xyz();
Return obj;                              MNO mno=(MNO)Test.xyz();
}
}
Note:
While working with Factory Design pattern we must know class names of objects that are created by Factory class.
ABSTRACT FACTORY DESIGN PATTERN:
interface xyz{     class ABC implements XYZ{   class MNO implements xyZ
a();                                           a(){                                          {
}                                                  - -                                                      a(){ - - }
                                                    }                                           }
                                          }

class Test{
public  static xyz method(- -){
-          - // Based on parameter value it creates either ABC obj or MNO obj
-          -
}           xyz x1=Test.methd(--);
}         xyz x1=Test.method(--);

When a method in a java class returns multiple other class objects(only one class object at a time). While designing method instead of taking specific class name as return type it is recommended to make all other classes implementing common interfaces and take that interface name as return type as shown for method() in the above give code. So that while calling that method we can capture the return value without knowing the class names by just using interface reference.