Open main menu

CDOT Wiki β

Changes

Decorator

376 bytes added, 13:53, 1 March 2007
Example
----
Abstract Component class
public abstract class InputStream {
abstract int read();
}
Concrete Component class
class FileInputStream extends InputStream
{
public int read();
}
Decorator class
public
class FilterInputStream extends InputStream {
{
//<span style="color:red">Local variable for storing the reference of the concrete component class</span>
protected volatile InputStream in;
protected FilterInputStream(InputStream in) { this.in = in; }
public int read() {
return in.read();
}
}
Concrete Decorator class
public
class PushbackInputStream extends FilterInputStream {
public PushbackInputStream(InputStream in, int size) {
super(in);//<span style="color:red">Calls the FileInputStream class and stores the reference of InputStream</span>
if (size <= 0) {
throw new IllegalArgumentException("size <= 0");
1
edit