Ads Sponsored By Google :)

Tuesday 29 January 2013

factory method Static and|vs Instance|design pattern in java with example abstract difference between|what are|is advantages|disadvantages


Factory method in java factory method design pattern in java factory method pattern in java static factory method in java factory method design pattern in java example abstract factory method design pattern in java Factory method in java example Factory method in java design pattern factory method pattern in java example what is factory method in java what is factory method in java with example what is factory method in java example what is static factory method in java with example what is factory method pattern in java with example what is static factory method in java what is factory method pattern in java what are static factory methods in java  what are static factory methods in java example static factory method in java  example advantages of static facatory method instance factory method in java instance factory method in java with example static vs instance factory methods in java with example static vs instance factory methods in java  static factory method vs instance factory method in java  difference between static and instance factory  methods in java


          Factory Method: 

       Definition:
          A factory method is one whose return type is similar to class name in which class name it 
        presents.

      The purpose of Factory method to create an object without using new operator.

      Rules for developing Factory Method:

1)   The return type of the factory method must be always similar to name of the class. In which class it presents.
2)   The nature of factory method must be always static.
3)   Access Specifier of factory method be Public.

     For Example:
     Real Time Example in JDK API

            1)   GraphicsEnvironment

Public  static   GraphicsEnvironment
                         Rule 3  Rule 2          Rule1
                                                          getLocalGraphicsEnvironment
                                                                          Method
                From above example we can understand that under GrapshicsEnvironment class 
    getLocalGraphicsEnvironment method return type is class name (i.e: GraphicsEvironment) 
    of nature static.

             2)   String
                   Public static   String       valueOf(Xxx)
                Rule 3  Rule 2   Rule1         Method

            3)   Class
              Public static   Class
             Rule 3  Rule 2  Rule1
          
              forName(String) throw ClassNotFoundException
                 Method

      The Following Example illustrate the concept of Factory Method:

        //The Following User defined Example for Factory Method.    
       // FMDemo.java
          public Class FMDemo{
               public static FMDemo fun1(){
                       FMDemo fmd=new FMDemo();
                       Return fmd;
                  }
               }

   What are different types of factory method s in java ?

     There are two types of factory methods in java. They are
1)      Static factory method.
2)      Instance factory method.

Static Factory Method:

     Factory method produces an object and produced object may be of same class or may be different class.
     In real scenarios we observe can Calendar, thread , InetAddress, Class …..etc
       classes are comes under static Factory methods

Examples of static factory methods:
a)      Class c=Class.forName(“java.lang.String”):
b)      Calendar c=Calendar.getInstance();
c)       Thread t=Thread.CurrentThread();

         Instance Factory Method:
              
              Instance factory method in java we can create instance factory methods also generally  
        factory methods also generally we use instance factory methods to produce one class  
       object from another class.

       For Example:

       Public class A{
          ----------// Block of statements
          ----------
        }

      Public class B{
             Public A getInstance() {
               return new A();
          }
       }

      getInstance method is producing an object of class A and it is instance method not a static 
       method. So this getInstance method is called Instance Factory method.


Advantages:
1.  Static factory methods is that, not like constructors, they have names.
2.  Static factory methods is that, not like constructors, they are not required to create a new object each time they’re invoked.
3.  Static factory methods is that, not like constructors, they can return an object of any sub type of their return type.
4.  Static factory methods is that they reduce the verbosity of creating parameterized type instances.
Disadvantages:
1. Only static factory methods is that classes without public or protected constructors cannot into sub class.
2.  static factory methods is that they are not readily to  differ  from other static methods.



No comments:

Post a Comment

LinkWithin