i**********b 发帖数: 77 | 1 anyone familiar with Decorator design pattern?
Java I/O is using this pattern. BufferedInputStream and DataInputStream
are all subclasses of FilterInputStream.
they are supposed to work fine when used in any order on an InputStream
This (1):
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
And this (2):
fis = new FileInputStream(file);
dis = new DataInputStream(bis);
bis = new Buffered |
Z*****Z 发帖数: 723 | 2 They both have the same interface for inputting dada. They both need to perf
orm decoration for the input stream. Since they are both subclasses of Filte
rInputStream, they can be chained together. Their neighbor is specified in t
he constructor. So each of them need to get data from the underlying (or nei
ghbor) input stream and do the specific decoration. Then wait the data to be
read by the next neighbor.
anyone familiar with Decorator design pattern?
Java I/O is using this pattern. Buffered
【在 i**********b 的大作中提到】 : anyone familiar with Decorator design pattern? : Java I/O is using this pattern. BufferedInputStream and DataInputStream : are all subclasses of FilterInputStream. : they are supposed to work fine when used in any order on an InputStream : This (1): : fis = new FileInputStream(file); : bis = new BufferedInputStream(fis); : dis = new DataInputStream(bis); : And this (2): : fis = new FileInputStream(file);
|
J*******n 发帖数: 2901 | 3 FileInputStream is the component that's being decorated. It gives a base
component
from which to read bytes.
BufferedInputStream is a concrete decorator. It adds behavior / decorates in
two
ways. It buffers input to improve performance, and also augments the
interface
with a new method readLine() for reading character-based input, a line at a
time.
DataInputStream is also a concrete decorator. It adds the ability to let an
application read primitive java data types from an underlying input strea
【在 i**********b 的大作中提到】 : anyone familiar with Decorator design pattern? : Java I/O is using this pattern. BufferedInputStream and DataInputStream : are all subclasses of FilterInputStream. : they are supposed to work fine when used in any order on an InputStream : This (1): : fis = new FileInputStream(file); : bis = new BufferedInputStream(fis); : dis = new DataInputStream(bis); : And this (2): : fis = new FileInputStream(file);
|
i**********b 发帖数: 77 | 4 Thank you. Still I don't understand how this can be implemented. I had
understood how it's used though.
A typical Decorator design pattern is implemented using composition. The
Decorator and the Component (to be decorated) shall have the same interfaces
. The decorated component can be treated as an original component.
However, in this case, BufferedInputStream and DataInputStream both added
additional functions to FilterInputStream. If a FilterInputStream is
decorated by BufferedInputStream fir
【在 Z*****Z 的大作中提到】 : They both have the same interface for inputting dada. They both need to perf : orm decoration for the input stream. Since they are both subclasses of Filte : rInputStream, they can be chained together. Their neighbor is specified in t : he constructor. So each of them need to get data from the underlying (or nei : ghbor) input stream and do the specific decoration. Then wait the data to be : read by the next neighbor. : : anyone familiar with Decorator design pattern? : Java I/O is using this pattern. Buffered
|