| -- What is the decoration mode.
Decoration mode: whether it is you or you. Please use the classic words of Lao Cui ).
It appears from inheritance, first inherited, and then decorated.
Recall inheritance
Benefits: Improves reusability and enables reuse. Later extensions can be used through method rewriting.
Disadvantage: because of the inheritance relationship between classes, their relationships are enhanced, and their coupling is high. Not in line with Java's purpose: low coupling and high cohesion.
Comparison:
A: There is a witer system.
| -- Txtwrite
| -- Mp3write
| -- Didwrite
The above is a common subclass implementation. The caller thinks the execution is too slow and needs to enhance the three (efficient | buffering ). Modify as follows:
Class mybufferewrite exetends write {
// Receiving subclass: Fu F = new Zi ();
Private write ziwrite;
Public bufferewrite (write ziwrite)
{
This. ziwrite = ziwrite;
}
Public void writeline (string line)
{
// Use ziwrite for cosmetic processing, and save a row of data to write.
}
Public void close ()
{
Ziwrite. clese (); // polymorphism (called or subclass )!
}
Public void flash (){
Ziwrite. Flash (); // polymorphism (called or subclass )!
}
... // Rewrite the abstract method of write. In mybufferewrite, the call in the method is to transfer ziwrite. Just calling it
I had some surgery on it before.
}
| -- Summary
1: for example, if you use the efficient write function, you can create mybufferewrite and pass your efficient objects, such as filewrite ();
2: The decoration mode reflects the enhancement (High Cohesion) of an object (function), and then the tightness is relatively weak (low coupling ).
3: JDK uses a lot of decorations, such as bufferedwriter (writer out), printstream (outputstream out), and objectinputstream (inputstream in.
| -- Sample Code
Simulate buffererereader decoration class
Package COM. decoration; import Java. io. ioexception; import Java. io. reader;/*** @ author hubiao * simulate buffererereader decoration class ** analysis: simulate a class, that is, to customize a class and implement its unique method: Readline (); * 1: simulation: Special Method * 2: What do you have: According to the decorative characteristics, a custom class inherits an abstract class, because the underlying layer of bufferereader's Readline inherits from Reader. * What we need to do is inherit reader and write a Readline method that is the same as bufferereader. */Public class mybuffere extends reader {/* reader is the parent class of an abstract class and implements all the classes that require efficiency. Is its subclass. */Private reader = NULL; Public mybuffere (Reader reader) {This. reader = reader;} public int read (char [] cbuf, int off, int Len) throws ioexception {return reader. read (cbuf, off, Len);} Public String Readline () throws ioexception {/*** 1: here you need to think about it. Use * A: Read (char [] cbuf, int off, int Len); read a buffer at a time * B: Read (); // read a string at a time * 2: Analysis * What is the purpose of simulating Readline? It is not an efficient method for reading a row at a time in readerline of bufferedreader. * It is not appropriate to use method A because it reads the length defined by a char capacity at a time, and the encapsulated code is difficult to know how long each row of Data Text the caller wants to read. * The advantage of using the B method is to read one character at a time. When \ r \ n is returned, it indicates that the reading of one row is complete, how can we store each character in a row of data? * It's terrible to use string, because we know that string is a constant pool in Java. 1. The value assignment is unchangeable. Therefore, the highly efficient stringbuilder * concatenates each character into the stringbuffer and returns it to the caller. * /// Single-character container stringbuffer buffer = new stringbuffer (); // each character int READ = 0; while (read = reader. Read ())! =-1) {char CHR = (char) read; If (CHR = '\ R') continue; If (CHR =' \ n') return buffer. tostring (); elsebuffer. append (CHR);} // prevents the last row from being read, and \ r \ n is not displayed. Then, check whether there is a value in the buffer zone? If yes, return again. If (buffer. Length ()> 0) return buffer. tostring (); elsereturn NULL;} public void close () throws ioexception {reader. Close ();}}
Simulate linenumberreader
/*** @ Author hubiao * simulates linenumberreader *. Similarly, the simulation is to define a class. The special method provided by the simulation requirement is to obtain each row and then read each row. */Public class mylinenumberreader extends reader {/* receives the passed subclass object, Fu F = new Zi (); // polymorphism */private reader; private int linenumber; // The row number is public mylinenumberreader (Reader reader) {This. reader = reader;} public int read (char [] cbuf, int off, int Len) throws ioexception {return reader. read (cbuf, off, Len);} Public String Readline () throws ioexception {// single character container stringbuffer buffer = new stringbuffer (); // each character int Read = 0; while (read = reader. Read ())! =-1) //-1 indicates reading to the end of the stream! {Char CHR = (char) read; If (CHR = '\ R') continue; If (CHR =' \ n') {linenumber ++; return buffer. tostring ();} elsebuffer. append (CHR);} // prevents the last row from being read, and \ r \ n is not displayed. Then, check whether there is a value in the buffer zone? If yes, return again. If (buffer. length ()> 0) {linenumber ++; return buffer. tostring ();} elsereturn NULL;} public void close () throws ioexception {reader. close ();} public int getlinenumber () {return linenumber;} public void setlinenumber (INT linenumber) {This. linenumber = linenumber ;}}
Decoration mode (old crush)