Decorator is often translated into "decoration", I think it is more imageable to "painter", the painter (Decorator) is used to brush the paint, then the brush painted object we call Decorator mode. These two entities are in Decorator mode It is necessary.
Decorator definition: Dynamically add some additional responsibilities to an object, just on the wall brush paint. It is more flexible to use the Decorator mode to reach the functionality of the generated subclass.
Why use Decorator? We can usually use inheritance to implement functional expansion. If these needs to expand, it is necessary to generate a lot of subclasses, increase the complexity of the system, and use inheritance implementation, we must These expansion functions are foreseen, which is determined when compiling is static.
The reason for using Decorator is: These features need to be dynamically determined by the user and the timing. Decorator provides "Plug and Play" method to determine when to increase what function during operation.
How to use the piling example in Adapter, there are two classes in Adapter: square pile round pile, Adapter mode show how to use these two classes, in Decorator mode, we have to add some additional functions when piling. For example, digging pit is a wooden board on the pile, and does not care about how to use two unrelated classes.
Let's establish a interface:
Public interface work {public void insert ();
The interface Work has a specific implementation: inserting a square pile or circular pile, these two differences are uli. We use the inserted square pile as an example:
Public class squarepeg import () {public void insert () {system.out.println ("Square Pile Insert");}}
There is now an application: Before the pile is put into enter, dig the pit, after the pile is hined, these additional functions are dynamic, may increase the adjustment modification, for example, it may need to be piled after piling ( Just a metaphor).
Then we use Decorator mode, here is the square pile Squarepeg is Decoratee (brush paint), we need to brush some "paint" on Decoratee, these paints are those additional functions.
Public Class Decorator Implements Work {Private Work Work; // Extra Added Features Packed in this list Private ArrayList Others = New ArrayList (); // Use a combination new way in the constructor to introduce Work items; public Decorator (Work Work) {this.work = work; others.add ("digging"); others.add ("nail wooden board");} public void insert () {newMethod ();} // In a new method, we are INSERT increases other methods, here is the user's flexible designated public void newmethod () {iblic void ();} public void} () {listiterator listiterator = }.listiterator (); while (Listiterator. Hasnext ()) {system.out.println ((String)) "Out");}}}
In the above example, we ranked the pit and nail boards in front of the piling insert, which only illustrates that the additional function order can be arbitrarily arranged.
Ok, Decorator mode is coming out, we look at how to call:
Work Squarepeg = new Squarepeg (); Work Decorator = New Decorator (Squarepeg); Decorator.insert (); Decorator mode is complete.
If you are careful, you will find that the call is called similar to our reading of the file:
FileReader fr = new fileReader (FileName); BufferedReader Br = New BufferedReader (fr);
In fact, Java's I / O API is implemented using Decorator, and there are many types of I / O, and if they are taking inheritance methods, many subclasses will be generated, which is obviously quite cumbersome.
The Decorator in jive is implemented in the forum system, and some special words cannot appear in the forum, such as "knocking XXX", we need to filter these "reaction" fonts. Do not let them appear or high brightness display.
In the IBM Java column, talking about Jive's article, talking about the Decorator mode, in fact, the program does not really use Decorator, but a prompt: for special forums to design additional filtration Then you can reorganize the ForumMessageFilter as Decorator mode.
So, we are truly Decorator mode, and will truly use Decorator mode, you must grasp the definition of Decorator mode, and the roles (Decoratee and Decorator).