In Java IO is the use of decorative mode design. One layer of decorative layer
such as: DataOutputStream dos = new DataOutputStream (new Bufferedoutputstream (New FileOutputStream ("Data.txt"));
Composition of the decoration mode: 1, abstract component role: OutputStream or InputStream in fairly java.io
2, the specific component role: equivalent to java.io in the FileInputStream or FileOutputStream
3. Abstract decorative Role: holds a reference to a component (Component) object, and define an interface that is consistent with the abstract component interface (that is, the role inherits or implements the abstract component role) equivalent to FilterInputStream or Filteroutputstream in java.io.
4, the specific decorative role: responsible for adding additional functions to the component objects. Equivalent to java.io bufferedinputstream or Bufferedoutputstream.
The code is implemented as follows:
/** * defines the abstract component role and defines a method */ public interface Component { public void DoSomething ();}
/** * Define specific component roles and implement methods */ Public class Implements Component { @Override publicvoid dosomething () { System.out.println ("FileInputStream");} }
/*** Define abstract decorative roles, hold references to artifact objects, and define an interface that is consistent with the abstract component interface (that is, the role inherits or implements the abstract component role) *@authorAcer **/ Public classDerectorImplementscomponent{PrivateComponent Component; Publicderector (Component Component) { This. comonent =component; } @Override Public voiddosomething () {component.dosomething (); }}
/*** Define specific decorative roles and add additional features *@authorAcer **/ Public classConcreteDerector1extendsDerector { PublicConcreteDerector1 (compenent component) {Super(component); } @Override Public voiddosomething () {Super. dosomething (); Doanything (); } Public voiddoanything () {System.out.println ("DataInputStream"); }}
/** * decoration mode, client Test * * @author Acer * */ public class Client { public static void main (string[] args) {Component Component1 = new Concretecomponent (); Component component2 = new ConcreteDerector1 (component1); // build datainputstream component2.d Osomething (); }}
Output Result:
FileInputStream
DataInputStream
Java decoration mode