avatar
Design Pattern Question# JobHunting - 待字闺中
i*b
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
avatar
Z*Z
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);

avatar
J*n
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);

avatar
i*b
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。