ContactUs :

Please send your Questions & Answers or Feedback to "mohan@javabook.org"

What is Factory method ?

Factory method is used to create different object from factory often refereed as Item and it encapsulate the creation code. So instead of having object creation code on client side we encapsulate inside Factory method in Java

 

Define an interface for creating an object, but let subclasses

decide which class to instantiate subclasses

 

Best Example of Factory method design pattern is valueOf() method which is there in String and wrapper classes

valueOf() method which returns object created by factory equivalent to value of parameter passed.

 

valueOf(int i): This returns an Integer object holding the value of the specified primitive.

valueOf(String s): This returns an Integer object holding the value of the specified string representation.

valueOf(String s, int radix): This returns an Integer object holding the integer value of the specified string representation, parsed with the value of radix.

 

 

                  Integer x =Integer.valueOf(9);

      Double c = Double.valueOf(5);

      Float a = Float.valueOf("80");

 

 

Same for Float and Double,String

 

 

 

getInstance() method which creates instance of Singleton class.

Calendar date = Calendar.getInstance();

newInstance() method which is used to create and return new instance from factory method every time called. throws InstantiationException,IllegalAccessException

        Date d = new Date();

        Class cls = d.getClass();

                                Object obj = cls.newInstance();

JDBC is a good example for this pattern; application code doesn't need to know what database it will be used with, so it doesn't know what database-specific driver classes it should use. Instead, it uses factory methods to get Connections, Statements, and other objects to work with.

Map synchronizedMap = Collections.synchronizedMap(new HashMap());

 

code example of Factory pattern in Java:

interface Currency {

       String getSymbol();

}

// Concrete Rupee Class code

class Rupee implements Currency {

       @Override

       public String getSymbol() {

              return "Rs";

       }

}

 

// Concrete SGD class Code

class SGDDollar implements Currency {

       @Override

       public String getSymbol() {

              return "SGD";

       }

}

 

// Concrete US Dollar code

class USDollar implements Currency {

       @Override

       public String getSymbol() {

              return "USD";

       }

}

 

// Factroy Class code

class CurrencyFactory {

 

       public static Currency createCurrency (String country) {

       if (country. equalsIgnoreCase ("India")){

              return new Rupee();

       }else if(country. equalsIgnoreCase ("Singapore")){

              return new SGDDollar();

       }else if(country. equalsIgnoreCase ("US")){

              return new USDollar();

        }

       throw new IllegalArgumentException("No such currency");

       }

}

 

// Factory client code

public class Factory {

       public static void main(String args[]) {

              String country = args[0];

              Currency rupee = CurrencyFactory.createCurrency(country);

              System.out.println(rupee.getSymbol());

       }

}

Related Posts Plugin for WordPress, Blogger...
Flag Counter