Java Q & A: Using Factory Method mode

zhaozj2021-02-11  207

Java Q & A: Using Factory Method mode

Q: When I read the article "Polymorphism in its purest form", I saw an unfamiliar term "Factory Method". Can you explain what is Factory Method and explains how to use it?

A: Factory Method is just a name of a method of instantiating an object. Like the factory, Factory Method's task is to create - or manufactured - objects.

Let us look at an example.

Each program has an error in an error. Take a look at the interface below:

Code List 1PUBLIC 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);

}

Assume that two implementations have been written. A implementation (Code List 3) Write the information to the command line, another (code list 2) is written to the file.

Code List 2Public 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 ();

}

Code List 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 any of these two classes, you need this:

Code list 4 // ... Some code ... systemtrace log = new systemtrace (); // ... code ... log.debug ("entering loog"); // ... etc ...

Now, if you want to change the "Trace Implementation" used in the program, you need to modify each class that instantiates the "Trace Implementation". The number of classes that use TRACE may be a lot, which requires a lot of work. Moreover, you must also want to avoid a lot of modifying your class as much as possible.

Code list 5PUBLIC CLASS TRACTORY {public static trace gettrace () {return new systemtrace ();}}

GetTrace () is a Factory Method. In this way, whenever you want to get a TRACE reference, just simply call tracefactory.gettrace ():

Code List 6 // ... Some Code ... trace log = new tracefactory.gettrace (); // ... code ... log.debug ("Entering loog"); // ... etc .. .

Using Factory Method to get an instance to save a lot of work later. In the code above, TraceFactory returns the SystemTrace instance. Suppose changes have changed, and the information needs to be written to the file. If you use Factory Method to get an instance, you can meet your new needs only in a class. You don't have to modify it in each class that uses TRACE. That is, only simply redefine GetTrace ():

Listing 7public 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 ()); returnit t;}}}}

Factory Method will be useful when it is not possible to determine what specific implementation is to be instantiated. You can leave those details to Factory Method.

转载请注明原文地址:https://www.9cbs.com/read-4433.html

New Post(0)