⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap11.htm

📁 java书籍《thinking in java》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</TR>
<A NAME="_Toc375545386"></A><A NAME="_Toc481064741"></A></TABLE></P></DIV>
<A NAME="Heading361"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Adding attributes <BR>and useful interfaces</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The use of layered objects to dynamically
and transparently add responsibilities to individual objects is referred to as
the <A NAME="Index1203"></A><A NAME="Index1204"></A><I>Decorator</I> pattern.
(Patterns</FONT><A NAME="fnB57" HREF="#fn57">[57]</A><FONT FACE="Georgia"> are
the subject of <I>Thinking in Patterns with Java</I>, downloadable at
<I>www.BruceEckel.com</I>.) The decorator pattern specifies that all objects
that wrap around your initial object have the same interface. This makes the
basic use of the decorators transparent&#8212;you send the same message to an
object whether it&#8217;s been decorated or not. This is the reason for the
existence of the &#8220;filter&#8221; classes in the Java I/O library: the
abstract &#8220;filter&#8221; class is the base class for all the decorators. (A
decorator must have the same interface as the object it decorates, but the
decorator can also extend the interface, which occurs in several of the
&#8220;filter&#8221; classes).

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I25' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I26>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Decorators are often used when simple
subclassing results in a large number of subclasses in order to satisfy every
possible combination that is needed&#8212;so many subclasses that it becomes
impractical. The Java I/O library requires many different combinations of
features, which is why the decorator pattern is used. There is a drawback to the
decorator pattern, however. Decorators give you much more flexibility while
you&#8217;re writing a program (since you can easily mix and match attributes),
but they add complexity to your code. The reason that the Java I/O library is
awkward to use is that you must create many classes&#8212;the &#8220;core&#8221;
I/O type plus all the decorators&#8212;in order to get the single I/O object
that you want.  
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I26' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I27>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The classes that provide the decorator
interface to control a particular <B>InputStream</B> or <B>OutputStream</B> are
the <B>FilterInputStream</B> and <B>FilterOutputStream</B>&#8212;which
don&#8217;t have very intuitive names. <B>FilterInputStream</B> and
<B>FilterOutputStream</B> are abstract classes that are derived from the base
classes of the I/O library, <B>InputStream</B> and <B>OutputStream</B>, which is
the key requirement of the decorator (so that it provides the common interface
to all the objects that are being decorated).

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I27' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I28>
</FONT><A NAME="_Toc375545387"></A><A NAME="_Toc481064742"></A><BR></P></DIV>
<A NAME="Heading362"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Reading from an InputStream <BR>with FilterInputStream</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>FilterInputStream</B> classes
accomplish two significantly different things. <B>DataInputStream</B> allows you
to read different types of primitive data as well as <B>String</B> objects. (All
the methods start with &#8220;read,&#8221; such as <B>readByte(&#160;)</B>,
<B>readFloat(&#160;)</B>, etc.) This, along with its companion
<B>DataOutputStream</B>, allows you to move primitive data from one place to
another via a stream. These &#8220;places&#8221; are determined by the classes
in Table 11-1.  
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I28' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I29>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The remaining classes modify the way an
<B>InputStream</B> behaves internally: whether it&#8217;s buffered or
unbuffered, if it keeps track of the lines it&#8217;s reading (allowing you to
ask for line numbers or set the line number), and whether you can push back a
single character. The last two classes look a lot like support for building a
compiler (that is, they were added to support the construction of the Java
compiler), so you probably won&#8217;t use them in general programming. 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I29' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I30>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You&#8217;ll probably need to buffer your
input almost every time, regardless of the I/O device you&#8217;re connecting
to, so it would have made more sense for the I/O library to make a special case
(or simply a method call) for unbuffered input rather than buffered input.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I30' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I31>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Table 11-3. Types of
FilterInputStream<A NAME="Index1205"></A><A NAME="Index1206"></A><A NAME="Index1207"></A><A NAME="Index1208"></A><A NAME="Index1209"></A><A NAME="Index1210"></A><A NAME="Index1211"></A><A NAME="Index1212"></A></B></FONT><BR></P></DIV>
<DIV ALIGN="CENTER"><TABLE BORDER>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Function</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Constructor
Arguments</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>How to use it</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Data-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Used in concert with
<B>DataOutputStream</B>, so you can read primitives (<B>int</B>, <B>char</B>,
<B>long</B>, etc.) from a stream in a portable fashion.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Contains a full interface to allow you to
read primitive types.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Buffered-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Use this to prevent a physical read every
time you want more data. You&#8217;re saying &#8220;Use a
buffer.&#8221;</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B>, with optional buffer
size.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This doesn&#8217;t provide an interface
<I>per se</I>, just a requirement that a buffer be used. Attach an interface
object.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>LineNumber-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Keeps track of line numbers in the input
stream; you can call <B>getLineNumber(&#160;)</B> and
<B>setLineNumber(</B></FONT><BR><FONT FACE="Georgia"><B>int)</B>.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This just adds line numbering, so
you&#8217;ll probably attach an interface object.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Pushback-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Has a one byte push-back buffer so that
you can push back the last character read.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Generally used in the scanner for a
compiler and probably included because the Java compiler needed it. You probably
won&#8217;t use this.</FONT><BR></P></DIV>
</TD>
</TR>
<A NAME="_Toc375545388"></A><A NAME="_Toc481064743"></A></TABLE></P></DIV>
<A NAME="Heading363"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Writing to an OutputStream <BR>with FilterOutputStream</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The complement to <B>DataInputStream</B>
is <B>DataOutputStream</B>, which formats each of the primitive types and
<B>String</B> objects onto a stream in such a way that any
<B>DataInputStream</B>, on any machine, can read them. All the methods start
with &#8220;write,&#8221; such as <B>writeByte(&#160;)</B>,
<B>writeFloat(&#160;)</B>, etc.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I31' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I32>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The original intent of <B>PrintStream</B>
was to print all of the primitive data types and <B>String</B> objects in a
viewable format. This is different from <B>DataOutputStream</B>, whose goal is
to put data elements on a stream in a way that <B>DataInputStream</B> can
portably reconstruct them. 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I32' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I33>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The two important methods in
<B>PrintStream</B> are <B>print(&#160;)</B> and <B>println(&#160;)</B>, which
are overloaded to print all the various types. The difference between
<B>print(&#160;)</B> and <B>println(&#160;)</B> is that the latter adds a
newline when it&#8217;s done.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I33' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I34>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>PrintStream</B> can be problematic
because it traps all <B>IOException</B>s (You must explicitly test the error
status with <B>checkError(&#160;)</B>, which returns <B>true</B> if an error has
occurred). Also, <B>PrintStream</B> doesn&#8217;t internationalize properly and
doesn&#8217;t handle line breaks in a platform independent way (these problems
are solved with <B>PrintWriter</B>).

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I34' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I35>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>BufferedOutputStream</B> is a modifier
and tells the stream to use buffering so you don&#8217;t get a physical write
every time you write to the stream. You&#8217;ll probably always want to use
this with files, and possibly console I/O.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I35' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER11_I36>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Table 11-4. Types of
FilterOutputStream<A NAME="Index1213"></A><A NAME="Index1214"></A><A NAME="Index1215"></A><A NAME="Index1216"></A><A NAME="Index1217"></A><A NAME="Index1218"></A></B></FONT><BR></P></DIV>
<DIV ALIGN="CENTER"><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=99 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Class</B></FONT><BR></P></DIV>
</TH>
<TH WIDTH=130 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Function</B></FONT><BR></P></DIV>
</TH>
<TH WIDTH=121 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Constructor Arguments
</B></FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TH WIDTH=121 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>How to use it</B></FONT><BR></P></DIV>
</TH>
</TR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -