Re: input stream 101



In article <1122981887.841573.185760@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
hawat.thufir@xxxxxxxxx <hawat.thufir@xxxxxxxxx> wrote:
>blmblm@xxxxxxxxxxxxx wrote:
>...
>> Hm. Why do you want this interface BasicTidy? Maybe you shouldn't
>> bother, but just define a class that provides only the functionality
>> you want. If it's an interface, it won't be useful unless you
>> have at least one class that implements it, and unless you think
>> that eventually you'll want more than one class that implements it,
>> what's the point of having a separately-defined interface? (Maybe
>> someone else will have a better answer for that.)
>
>Part of the justification for BasicTidy is for the sake of having an
>interface and using it. BasicTidy does serve a practical purpose,
>hopefully, in that down the line as I use more and more of the methods
>in BasicTidy I won't have to look in Test16 to see the method
>signatures but can just refer to BasicTidy which is much simpler.
>Hopefully, BasicTidy is largely self-documenting. I should've been
>doing documentation all along, of course. Mea culpa.

So, the reason for having a separate interface definition is to have
something short that just lists method signatures? Well, okay.
I still think it's a bit redundant to have both an interface and
a class, though perhaps there's an additional benefit I'm not
thinking of.

With regard to documentation, do you know about the "javadoc"
tool? (I think you're using CLI tools; if you're using an IDE,
such as Eclipse, there's probably equivalent functionality in
it somewhere.) This is something that takes your code (and its
comments) and produces HTML-form documentation similar to the Java
documentation you find at Sun's Web site and elsewhere. In fact,
if you take completely uncommented code and just run it through
javadoc (e.g., "javadoc MyProgram.java"), you'll get *something*,
and it's even something that serves pretty much your purpose for
having an interface, I think. Well worth knowing about if you're
going to do much Java-writing. (I know -- so much that's "well
worth knowing about", so little time .... )

>Possibly the code changed a bit since you last read it. Currently
>BasicTidy implements Runnable and Test16 implements BasicTidy. I'm not
>totally convinced that this is good, but it does mean that ControlTidy
>doesn't have to deal with an object of type Test16, only the interface
>of BasicTidy.

But the methods defined in Test16 are exactly the same as those in
BasicTidy, except that Test16 also has a constructor. How is the
interface simpler? Why not just have a class BasicTidy? (You might
still be right that you want the separate interface; I'm just trying
to point out alternatives.)

>This was one the chief reasons for ControlTidy's
>existence, to create the simplest possible program for my needs. If
>necesarry I can create an IntermediateTidy for Test16 to implement, but
>I'm not sure what that would consist of.
>
>>
>> >(I'm not sure how to say that last one. There's the type (interface)
>> >of an object, and then its type (instance of).)
>>
>> I'm not sure either, but I think you have the ideas straight anyway:
>>
>> For example, if class ControlTidy extends Tidy and implements Runnable
>> (just an example, don't think this is how you should do things), when
>> you say
>>
>> ControlTidy c = new ControlTidy();
>
>Yes, this is a step I'm considering. For instance, there might a
>GUItidy which sends url's to ControlTidy, or it might send a path to a
>local file, too.

Sure .... I'm not sure I'm 100% clear where you're going with this
(you may not be either), but the idea of separating the creating
and manipulating of a Tidy object from the user interface seems like
a fine idea, since then it's easy to use the same back end with
different front ends (CLI, GUI, etc.).

>> you an object that's "really" a ControlTidy, but it could be used as
>> a ControlTidy, a Tidy, or a Runnable. Or, of course, an Object.
>>
>> So c points to an object that's an instance of class ControlTidy,
>> and yet all of the following should evaluate to true:
>>
>> c instanceof ControlTidy
>>
>> c instanceof Tidy
>>
>> c instanceof Runnable
>
>Hmm, I never though of that, but that's interesting. For any object
>foo, instanceof Object will evaluate to true.

Right.

>I'm not sure of the
>application, but it's interesting. An object can have many "types".

Yes, and this is billed as Java's approach to "multiple inheritance"
(don't worry if you don't know that term), and it's useful in a lot
of ways that might not be clear until you've worked with the language
a little longer.

For example, in your code below, you have a variable "foo" of type
BasicTidy. Since BasicTidy extends Runnable, foo is of type Runnable,
which is what makes the line

Thread fooThread = new Thread(foo);

work -- Thread has a constructor that takes a parameter of type
Runnable. You could get the same effect if BasicTidy didn't
extend Runnable, but Test16 implemented Runnable, and foo were
a Test16 rather than a BasicTidy.

(By the way, I'm not sure that if I were designing this stuff from
the ground up I'd bother with using threads here. I'm guessing you
do because you adapted the code from something that uses threads.
Not a problem, just something to keep in mind if you continue
refining the program. Also, if BasicTidy extends Runnable -- which
I'm not sure makes object-oriented-design sense, but whatever -- then
there's no need for it to explicitly include a signature for run().
I'm not really sure whether the duplication could be harmful.
Maybe someone else is following this and can help with that.)

>> Anyway to get back to your question -- if you define an interface
>> BasicTidy, then for an object to count as a BasicTidy, it has to
>> be an instance of a class that implements the BasicTidy interface.
>...
>
>At one point ControlTidy instatiated an object which implemented
>BasicTidy, then I took that out. Now, it's back. ControlTidy
>instatiates:
>
>BasicTidy basicTidy = new Test16();
>
>Except that the name (reference?) for the object above is "foo" in the
>actual code, I'm not sure why. I should change the name to basicTidy,
>probably.

Something else borrowed from the initial code, maybe? yeah, a different
name might be clearer.

>here's the full, current code:

Left in for others responding.

Somewhat off-topic, but I'm curious: What's your programming
background? I think Java must not be your first language; what others
do you know? That might help us in answering questions ....

(And -- entertaining names you use, for machines and a username.
Takes me back to a time when I was reading a lot more SF than I do
now.)

>[thufir@arrakis tidyXhtml]$ date
>Tue Aug 2 12:03:05 IST 2005
>[thufir@arrakis tidyXhtml]$ pwd
>/home/thufir/java/src/atreides/tidyXhtml
>[thufir@arrakis tidyXhtml]$ ll
>total 24
>-rw-rw-r-- 1 thufir thufir 284 Aug 2 00:36 BasicTidy.java
>-rw-rw-r-- 1 thufir thufir 520 Aug 2 00:41 ControlTidy.java
>-rw-rw-r-- 1 thufir thufir 1492 Aug 2 00:36 Test16.java
>[thufir@arrakis tidyXhtml]$ cat BasicTidy.java -n
> 1 package atreides.tidyXhtml;
> 2
> 3 public interface BasicTidy extends Runnable{
> 4 public void setUrl(String url);
> 5 public void setOutFileName(String outFileName);
> 6 public void setErrOutFileName(String errOutFileName);
> 7 public void setXmlOut(boolean xmlOut);
> 8 public void run();
> 9 }//BasicTidy
>[thufir@arrakis tidyXhtml]$ cat Test16.java -n
> 1 package atreides.tidyXhtml;
> 2
> 3 import java.io.IOException;
> 4 import java.net.URL;
> 5 import java.io.BufferedInputStream;
> 6 import java.io.FileOutputStream;
> 7 import java.io.PrintWriter;
> 8 import java.io.FileWriter;
> 9 import org.w3c.tidy.Tidy;
> 10
> 11
> 12 public class Test16 implements BasicTidy{
> 13
> 14 private String url;
> 15 private String outFileName;
> 16 private String errOutFileName;
> 17 private boolean xmlOut;
> 18
> 19 public Test16(){
> 20 this.url = "http://www.google.com/";;
> 21 this.outFileName = "out.txt";
> 22 this.errOutFileName = "err.txt";
> 23 this.xmlOut = true;
> 24 }//Test16
> 25
> 26 public void run() {
> 27 URL u;
> 28 BufferedInputStream in;
> 29 FileOutputStream out;
> 30 Tidy tidy = new Tidy();
> 31
> 32 tidy.setXmlOut(xmlOut);
> 33 try {
> 34 tidy.setErrout(new PrintWriter(new
>FileWriter(errOutFileName), true));
> 35 u = new URL(url);
> 36 in = new
>BufferedInputStream(u.openStream());
> 37 out = new
>FileOutputStream(outFileName);
> 38 tidy.parse(in, out);
> 39 }//try
> 40 catch ( IOException e ) {
> 41 System.out.println( this.toString() +
>e.toString() );
> 42 }//catch
> 43 }//run
> 44
> 45 public void setUrl (String url){
> 46 this.url = url;
> 47 }//setUrl
> 48
> 49 public void setOutFileName (String outFileName){
> 50 this.outFileName = outFileName;
> 51 }//setOutFileName
> 52
> 53 public void setErrOutFileName (String errOutFileName){
> 54 this.errOutFileName = errOutFileName;
> 55 }//setErrOutFileName
> 56
> 57 public void setXmlOut (boolean xmlOut) {
> 58 this.xmlOut = xmlOut;
> 59 }//setXmlOut
> 60
> 61 public static void main( String[] args ) {
> 62 }//main
> 63 }//Test16
>[thufir@arrakis tidyXhtml]$ cat ControlTidy.java -n
> 1 package atreides.tidyXhtml;
> 2
> 3 public class ControlTidy{
> 4
> 5 public static void main (String[] args) {
> 6 String yahooUrl =
>"http://www.yahoo.com/";;
> 7 String googleUrl =
>"http://www.google.com/";;
> 8 String out = "out.txt";
> 9 String err = "err.txt";
> 10 boolean xml = true;
> 11 boolean xhtml = false;
> 12 BasicTidy foo = new Test16();
> 13
> 14 foo.setUrl (yahooUrl);
> 15 foo.setOutFileName (out);
> 16 foo.setErrOutFileName (err);
> 17 foo.setXmlOut(xml);
> 18
> 19 Thread fooThread = new Thread(foo);
> 20 fooThread.start();
> 21 }//main
> 22 }//ControlTidy
>[thufir@arrakis tidyXhtml]$
>
>
>
>-Thufir
>


--
| B. L. Massingill
| ObDisclaimer: I don't speak for my employers; they return the favor.
.



Relevant Pages

  • Re: input stream 101
    ... >I do want to create my own interface, BasicTidy ... >I do want ControlTidy to call methods on an object which is of type ... Why do you want this interface BasicTidy? ... if class ControlTidy extends Tidy and implements Runnable ...
    (comp.lang.java.help)
  • to thread or not
    ... Test16 implements BasicTidy ... ControlTidy instantiates aTidy, which is a BasicTidy ... public class Test16 implements Runnable, ...
    (comp.lang.java.help)
  • Re: input stream 101
    ... Why do you want this interface BasicTidy? ... if class ControlTidy extends Tidy and implements Runnable ... [thufir@arrakis tidyXhtml]$ date ...
    (comp.lang.java.help)
  • Re: input stream 101
    ... If you want something with a GUI for those ... I do want to create my own interface, BasicTidy ... I do want ControlTidy to call methods on an object which is of type ...
    (comp.lang.java.help)
  • Re: input stream 101
    ... the reason for having a separate interface definition is to have ... except that Test16 also has a constructor. ... Why not just have a class BasicTidy? ... All the HTML files are the same pattern. ...
    (comp.lang.java.help)