Step one: Define a base class, where you need to decorate the method, the base class can be a class, can also be an interface
Public interface sourceable {public
void method ();
}
Step two: Define a decorated class, inherit (implement) the base class
The public class Source implements sourceable{@Override the public
void Method () {
System.out.println ("This is Method one");
}
}
Step three: Define an adorner, (directly or indirectly inheriting the base class) where the base class is implemented directly, or the direct subclass of the base class can be inherited.
To save an instance of a base class
public class decorator implements sourceable{
private sourceable source;
Public decorator (sourceable source) {
super ();
This.source=source;
}
@Override public
Void Method () {
System.out.println ("Decorate in front");
Source.method ();
System.out.println ("Decorate the Back");
public void Method2 () {
System.out.println ("Decorate in front");
Source.method ();
System.out.println ("Decorate the Back");
}
Step Four: Test (note the way of the call, to the decorated person instance into the adorner)
public class Test {public
static void Main (string[] args) {
sourceable source=new source ();
sourceable obj=new Decorator (source);
Obj.method ();
}
InputStream and OutputStream in IO use the decorator mode, where we use InputStream to simply record.
The decorated person is FilterInputStream
--------------------------------------------------------------------------------------------------------------- ----------------
Decorated by Bufferedinputstream
--------------------------------------------------------------------------------------------------------------- -------------