Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Factory Method mode in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use the Factory Method pattern in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Q: when reading the article "Polymorphism in its purest form", I came across an unfamiliar term "Factory method". Can you explain what Factory method is and how to use it?

A: Factory method (factory method) is simply the name of a method that instantiates an object. Like a factory, the task of Factory method is to create-or manufacture-objects.

Let's look at an example.

Every program should have a way to report an error. Look at the following interfaces:

Code 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 two implementations are written. One implementation (listing 3) writes the information to the command line, and the other (listing 2) writes to a file.

Code 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 two classes, do this:

Listing 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 large, and this modification requires a lot of work. Also, you must want to avoid making as many changes to your classes as possible.

Listing 5

Public class TraceFactory {

Public static Trace getTrace () {

Return new SystemTrace ()

}

}

GetTrace () is a Factory method. So, whenever you want to get a reference to Trace, simply call TraceFactory.getTrace ():

Code listing 6

/ /... Some code...

Trace log = new TraceFactory.getTrace ()

/ /... Code...

Log.debug ("entering loog")

/ /... Etc...

Using Factory method to get an instance can save a lot of work in the future. In the above code, TraceFactory returns an instance of SystemTrace. Assuming that the requirements have changed, the information needs to be written to a file. If you are using Factory method to obtain an instance, you only need to modify it once in a class to meet the new requirements. You don't have to make changes in every class that uses Trace. That is, simply redefine getTrace ():

Code 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

}

}

}

Factory method is useful when you are not sure what concrete implementation of a class will be instantiated. You can leave those details to Factory method.

In the above example, your program does not know whether to create FileTrace or SystemTrace. Therefore, you can just use Trace to deal with objects, leaving the instantiation of the concrete implementation to Factory method.

Thank you for reading! This is the end of this article on "how to use Factory Method mode in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report