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 object of the brush paint we call Decorator. These two entities are in Decorator mode It is necessary.
Decorator definition: Dynamically add some additional responsibilities to an object, just brush paint on the wall. It is more flexible to use the Decorator mode to achieve a function with a 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 build an interface first:
Public interface work {public void insert ();
}
Interface Work has a concrete implementation: inserting a square pile or circular pile, these two differences do not matter to Decorator. We use the insertion square pile as an example:
Public class squarepeg import ({system.out.println ("square pile insertion");
}
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 increasing features Packed in this list price arraylist = new arraylist ();
// Use a combination new method in the constructor to introduce a Work object; public decorator (Work Work) {this.work = work; er.add ("digging");
Others.Add ("Nail Machine");
Public void insert () {
Newmethod ();
/ / In a new method, we add additional methods before INSERT. Here, the order is successfully specified by the user flexible public void newmethod (); work.insert ();
Public void othermethod () {listiterator Listiterator = odser.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 above call is called similar to what we read:
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 the discorator 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).