Java Design pattern decoration mode (decorator mode) Introduction _java

Source: Internet
Author: User
Tags inheritance

Decorator is often translated into "decoration", I think the translation of "painter" more image point, painter (decorator) is used to brush paint, then the object of painting paint we call decoratee. Both of these entities are required in decorator mode.

Decorator definition: Dynamically adds some extra responsibilities to an object, like painting on a wall. It is more flexible to use the decorator mode to achieve functional expansion than to use the generative subclass method.

Why use Decorator

We can usually use inheritance to extend the functionality, if these need to expand the variety of functions, it is bound to generate a lot of subclasses, increase the complexity of the system, at the same time, the use of inheritance to achieve functional expansion, we must be able to foresee these expansion functions, these functions are compiled when it is determined, is static.

The rationale for using decorator is that these features require the user to dynamically decide how and when to join. Decorator provides a "Plug and Play" method that determines when to add functionality during run time.

How to use decorative mode

For the piling example in adapter, there are two kinds in adapter: Square pile Round Pile, adapter mode shows how to use these two classes synthetically, in the decorator mode, we have to add some extra functions when piling, for example, digging the pit on the pile and so on, Don't care about using two unrelated classes.

We first set up an interface:

Copy Code code as follows:

public interface work{
public void Insert ();
}

Interface work has a specific implementation: insert a square pile or a round pile, the two differences on the decorator is indifferent. We take the insert square pile as an example:
Copy Code code as follows:

public class Squarepeg implements work{
public void Insert () {
System.out.println ("Square pile insertion");
}
}

Now there is an application: you need to dig in before the pile, after the break, in the pile on the plank, these additional functions are dynamic, may be arbitrarily increased adjustment changes, for example, may need to be piling up after the rack (just metaphor).

So we use the decorator mode, where the square pile squarepeg is decoratee (the painter), we need to brush some "paint" on decoratee, these are the extra features.

Copy Code code as follows:

public class decorator implements work{
Private Work Work;
Additional features are packaged in this list
Private ArrayList others = new ArrayList ();
The work object is introduced in the constructor using the combination new method.
Public decorator (Work Work) {
This.work=work;
Others.add ("Digging pit");
Others.add ("nail plank");
}
public void Insert () {
Newmethod ();
}
In the new method, we add additional methods before the insert, where the user is flexibly assigned
public void Newmethod () {
Othermethod ();
Work.insert ();
}
public void Othermethod () {
Listiterator listiterator = Others.listiterator ();
while (Listiterator.hasnext ()) {
System.out.println (((String) (Listiterator.next ()) + "in Progress");
}
}
}

In the example above, we put the digging and the plank in front of the piling insert, just to illustrate that the extra function order can be arranged arbitrarily.

Well, decorator mode comes out and we see how to call:

Copy Code code as follows:

Work squarepeg = new Squarepeg ();
Work decorator = new Decorator (squarepeg);
Decorator.insert ();

The decorator mode is complete.

If you are careful, you will notice that the call is similar to the one we read when reading the file:

Copy Code code as follows:

FileReader FR = new FileReader (filename);
BufferedReader br = new BufferedReader (FR);

In fact, Java's I/O API is implemented using decorator, I/O variant many, if all adopt inheritance method, will produce many subclasses, obviously quite cumbersome.

The realization of decorator in Jive

In the forum system, some special words can not appear in the forum such as "Down with XXX", we need to filter these "reactionary" font. Do not let them appear or display high brightness.

In an article devoted to jive in the IBM Java column, it was mentioned that the decorator model was used in Forummessagefilter.java in Jive, in fact, the program did not really use decorator, Instead, the tip says: You can design additional filtering capabilities for special forums, so you can reorganize forummessagefilter as a decorator mode.

So, as we distinguish between the real decorator pattern and the actual use of the decorator pattern, we must grasp the definition of the decorator pattern and the role it participates in (Decoratee and decorator).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.