Question:
what is factory method in java ?
sundharscjp
2006-05-03 20:58:37 UTC
what is factory method in java ?
Four answers:
thanassis
2006-05-10 18:45:39 UTC
The term "factory method" refers to a method that is part

of the factory design pattern. The factory design pattern

suggests that when you want to decouple the instantiations

of objects from their type you can defer the instantiations

to a class method (static method).



i.e. Rather than instantiating,



Fruit myFruit = new Apple();



you call a method:



Fruit myFruit = FruitFactory.getApple();



That of course presumes writing the FruitFactory class with

all of its methods.





Initially it may seem like a simple transformation but it

has huge implications considering scaling.



If before you had a system with 1000s of places where you

instantiated an appleFruit and you needed to make a change

to the instantiation parameters, then you would also had to

make changes to all these 1000s places. But if you had

chosen to use a "factory method" instead of direct

instantiations, you would had only needed to go to the

factory method's implementation and make a single change.



For more information on the factory method have a look at the wickipedia page.

http://en.wikipedia.org/wiki/Factory_design_pattern



For java code on a further variation of the idea look at

http://www.thanassis.com/java/Java-advanced-day1/sld120.htm
programmer
2006-05-04 00:10:26 UTC
Factory method is just a fancy name for a method that instantiates objects. Like a factory, the job of the factory method is to create -- or manufacture -- objects.



Let's consider an example.



Every program needs a way to report errors. Consider the following interface:



Listing 1







public interface Trace {



// turn on and off debugging

public void setDebug( boolean debug );



// write out a debug message

public void debug( String message );



// write out an error message

public void error( String message );



}





Suppose that you've written two implementations. One implementation (Listing 2) writes the messages out to the command line, while another (Listing 3) writes them to a file.



Listing 2







public class FileTrace implements Trace {



private java.io.PrintWriter pw;

private boolean debug;



public FileTrace() throws java.io.IOException {

// a real FileTrace would need to obtain the filename somewhere

// for the example I'll hardcode it

pw = new java.io.PrintWriter( new java.io.FileWriter( "c:\trace.log" ) );

}



public void setDebug( boolean debug ) {

this.debug = debug;

}



public void debug( String message ) {

if( debug ) { // only print if debug is true

pw.println( "DEBUG: " + message );

pw.flush();

}

}

public void error( String message ) {

// always print out errors

pw.println( "ERROR: " + message );

pw.flush();

}



}





Listing 3







public class SystemTrace implements Trace {



private boolean debug;



public void setDebug( boolean debug ) {

this.debug = debug;

}



public void debug( String message ) {

if( debug ) { // only print if debug is true

System.out.println( "DEBUG: " + message );

}

}

public void error( String message ) {

// always print out errors

System.out.println( "ERROR: " + message );

}



}





To use either of these classes, you would need to do the following:



Listing 4







//... some code ...

SystemTrace log = new SystemTrace();

//... code ...

log.debug( "entering loog" );

// ... etc ...





Now if you want to change the Trace implementation that your program uses, you'll need to edit each class that instantiates a Trace implementation. Depending upon the number of classes that use Trace, it might take a lot of work for you to make the change. Plus, you want to avoid altering your classes as much as possible.



A factory method lets us be a lot smarter about how our classes obtain Trace implementation instances:



Listing 5







public class TraceFactory {

public static Trace getTrace() {

return new SystemTrace();

}

}





getTrace() is a factory method. Now, whenever you want to obtain a reference to a Trace, you can simply call TraceFactory.getTrace():



Listing 6







//... some code ...

Trace log = new TraceFactory.getTrace();

//... code ...

log.debug( "entering loog" );

// ... etc ...





Using a factory method to obtain an instance can save you a lot of work later. In the code above, TraceFactory returns SystemTrace instances. Imagine again that your requirements change and that you need to write your messages out to a file. However, if you use a factory method to obtain your instance, you need to make only one change in one class in order to meet the new requirements. You do not need to make changes in every class that uses Trace. Instead you can simply redefine getTrace():



Listing 7







public class TraceFactory {

public static Trace getTrace() {

try {

return new FileTrace();

} catch ( java.io.IOException ex ) {

Trace t = new SystemTrace();

t.error( "could not instantiate FileTrace: " + ex.getMessage() );

return t;

}

}

}





Further, factory methods prove useful when you're not sure what concrete implementation of a class to instantiate. Instead, you can leave those details to the factory method.



In the above examples your program didn't know whether to create FileTrace or SystemTrace instances. Instead, you can program your objects to simply use Trace and leave the instantiation of the concrete implementation to a factory method.
?
2014-03-11 02:54:02 UTC
http://java-latte.blogspot.in/2014/02/factory-method-design-pattern-in-java.html
M
2006-05-03 23:22:24 UTC
I am not sure. What do you mean by factory? DomFactory? I think it is used to build a DOM.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...