Java decoration mode (decorator mode)

Source: Internet
Author: User

Decorator often translated into "decoration", I think translated into "painter" more image point, painter (decorator) is used to brush paint, then painted objects we call decoratee. Both of these entities are required in the 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 the expansion of the function in the way of generating subclasses.

Why use Decorator

We can usually use inheritance to achieve the expansion of the function, if these need to expand the variety of functions, it is bound to generate a lot of subclasses, increase the complexity of the system, while the use of inheritance to achieve functional expansion, we must be able to foresee these expansion functions, these functions are compile-time determined, is static.

The reason for using decorator is that these features need to be dynamically decided by the user to join the way and timing. Decorator provides a "Plug and play" approach that determines when to add functionality during run time.

How to use decorative mode

Example of piling in adapter, there are two kinds in adapter: Square pile Round Pile, adapter mode shows how to use these two classes, in decorator mode, we want to add some additional functions in the piling, for example, digging a hole in the pile to nail the plank, Do not care how to use two unrelated classes.

Let's set up an interface first:

 Public Interface work{  publicvoid  Insert ();}

The interface work has a concrete implementation: inserting square piles or circular piles, these two differences are indifferent to decorator. Let's take the example of inserting a square pile:

 Public class Implements work{  publicvoid  Insert () {System.out.println ("square pile Insert");}}

Now there is an application: the need to dig in before the pile into the pit, after breaking into the pile on the board, these additional features are dynamic, may arbitrarily increase the adjustment changes, for example, may need to nail the shelves after piling (just analogy).


So we use the decorator mode, where the square pile squarepeg is decoratee (painted), we need to brush some "paint" on the decoratee, these paint is those additional functions.

 Public classDecoratorImplementswork{PrivateWork work ;//additional features are packaged in this list   PrivateArrayList others =NewArrayList ();//the work object is introduced in the constructor using the combination new method;    PublicDecorator (Work work) { This. work=Work ; Others.add ("Digging Pits"); Others.add ("Nail Board"); } Public voidInsert () {Newmethod ();}//In the new method, we add other methods before insert, where the order is the user's flexibility to specify    Public voidNewmethod () {Othermethod (); Work.insert (); } Public voidOthermethod () {listiterator listiterator=Others.listiterator ();  while(Listiterator.hasnext ()) {System.out.println ((String) (Listiterator.next () ))+ "In Progress"); }}}

In the example above, we put the pits and the nailed planks in front of the piling insert, just to illustrate that the additional function order can be arbitrarily arranged.

Well, decorator mode comes out and we look at how to invoke:

Newnew  Decorator (squarepeg);d Ecorator.insert ();

The decorator mode is now complete.

If you are careful, you will find that the call above is similar to the one we read from the file:

    New filereader (filename);     New BufferedReader (FR);

In fact, Java I/O API is implemented using decorator, I/O variants are many, if all take the inheritance method, will produce many subclasses, obviously quite cumbersome.

Implementation of decorator in Jive

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

In the IBM Java column devoted to Jive's article, there is talk about jive in Forummessagefilter.java used decorator mode, in fact, the program does not really use decorator, Instead, it suggests that additional filtering capabilities can be designed for special forums, so you can reorganize forummessagefilter as decorator mode.

So, we are in the decorator mode, and will really use the decorator model, we must grasp the definition of decorator pattern, and the role of participation (Decoratee and decorator).

Java decoration mode (decorator mode)

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.