In Java, you can combine two similar functions, where is JspWriter and another PrintWriter used?

I have the following class, which, as you will see, has a rather redundant formatNameAndAddress method:

package hu.flux.helper; import java.io.PrintWriter; import javax.servlet.jsp.JspWriter; // A holder for formatting data public class NameAndAddress { public String firstName; public String middleName; public String lastName; public String address1; public String address2; public String city; public String state; public String zip; // Print out the name and address. public void formatNameAndAddress(JspWriter out) throws java.io.IOException { out.println("<PRE>"); out.print(firstName); // Print the middle name only if it contains data. if ((middleName != null) && (middleName.length() > 0)) {out.print(" " + middleName);} out.println(" " + lastName); out.println(" " + address1); if ((address2 != null) && (address2.length() > 0)) out.println(" " + address2); out.println(city + ", " + state + " " + zip); out.println("</PRE>"); } public void formatName(PrintWriter out) { out.println("<PRE>"); out.print(firstName); // Print the middle name only if it contains data. if ((middleName != null) && (middleName.length() > 0)) {out.print(" " + middleName);} out.println(" " + lastName); out.println(" " + address1); if ((address2 != null) && (address2.length() > 0)) out.println(" " + address2); out.println(city + ", " + state + " " + zip); out.println("</PRE>"); } } 

I would like to rewrite the class to use a generic method, for example:

  // Print out the name and address. private void genericFormatNameAndAddress(Object out) { out.println("<PRE>"); out.print(firstName); // Print the middle name only if it contains data. if ((middleName != null) && (middleName.length() > 0)) {out.print(" " + middleName);} out.println(" " + lastName); out.println(" " + address1); if ((address2 != null) && (address2.length() > 0)) out.println(" " + address2); out.println(city + ", " + state + " " + zip); out.println("</PRE>"); } 

But I can’t do it exactly like that, because Object does not have print () and println () methods. If I pass the output to JspWriter or PrintWriter, sometimes I get it wrong.

I guess what I need to do is somehow pass the type of the object as a variable, and then use the variable to determine how to distinguish it. Is it possible? If so, how? If not, what would be a good solution?

+6
java polymorphism casting jsp printwriter
source share
4 answers

This will probably work:

 public void formatNameAndAddress(JspWriter out) throws java.io.IOException { formatNameAndAddress(new PrintWriter(out)); } 
+5
source share

You kind of confuse two different tasks with these methods and violate the principle of OO specialization. That is, you have methods that are responsible for formatting one of the two types of strings ... and are responsible for sending them to one of the two types of target results.

A better approach might be to make your methods more specialized. That is, they need ONLY to be responsible for building the "Name" string or the "Name and address" string ... and return String as the return type of methods.

At the point in the code where you call these methods, you obviously already have a JspWriter or PrintWriter ... because right now you are passing it as an argument to the method. Therefore, it would be easier to just leave this object where it is in the code and print it String , which will be returned by your specialized output agent-agnostic.

+1
source share

If you want to pass the object to the correct Writer, you can try something like:

 private void genericFormatNameAndAddress(Object out){ if (obj instanceof Printwriter){ //cast to printwriter } else { //cast to JspWriter } 

Barkers solution seems more appropriate because Printwriter can be built from JspWriter.

0
source share

Both JspWriter and PrintWriter are subclasses of java.io.Writer . Since you are not using any functionality specific to either of the two, you can declare the method as taking java.io.Writer :

 public void formatNameAndAddress(Writer out) throws java.io.IOException { [...] 

Then pass in JspWriter or PrintWriter if necessary.

Edit:. This will not work without changing the code, because, as others have noted, Writer does not have print and println methods, while JspWriter and PrintWriter both provide them.

0
source share

All Articles