Design Pattern pocket edition continuous reprinting-decorator)

Source: Internet
Author: User
Tags types of functions

Original: fanix

Decorator is often translated into "decoration". I think decorator is used to paint a painter, so the object to be painted is called decoratee. these two entities are required in the decorator mode.

Decorator definition:
Dynamically add some additional responsibilities to an object, just like painting on the wall. Using the decorator mode is more flexible than using the subclass method to Expand functions.

Why decorator?
We can usually use inheritance to Expand functions. If there are many types of functions to be extended, many sub-classes will be generated to increase the complexity of the system. At the same time, when using inheritance to implement function expansion, we must be able to foresee these extended functions. These functions are determined at compilation and static.

The reason for using decorator is: These functions need to be dynamically determined by the user to join the method and timing. decorator provides the "Plug and Play" method, during which to decide when to add the function.

How to use it?
As an example of piling in the adapter, there are two types in the adapter: Square Pile round pile. The adapter mode shows how to use these two classes comprehensively. In the decorator mode, we want to add some additional features when piling, such as digging holes and dingtalk boards on the piles. We don't care how to use two unrelated classes.

First, create an interface:
Program code:

Public interface work
{
Public void insert ();

}

 

Interface work has a specific implementation: Insert a square pile or a circular pile, the two differences do not matter to the decorator. We take inserting a Square Pile as an example:
Program code:

Public class squarepeg implements work {
Public void insert (){
System. Out. println ("Square Pile insertion ");
}
}

 

Now there is an application: It is necessary to dig holes before piling into the pile. after hitting the pile, dingtalk boards on the pile. These additional functions are dynamic and may be adjusted and modified at will, for example, you may need to pin the shelf after piling (just a metaphor ).

In decorator mode, squarepeg is decoratee (painted). We need to paint some "paint" on decoratee, which is additional.
Program code:

Public class decorator implements work {

Private work;
// Additional functions are packaged in this list.
Private arraylist others = new arraylist ();

// Use the new combination method in the constructor to introduce the work object;
Public decorator (Work work)
{
This. Work = work;

Others. Add ("pitfalls ");

Others. Add ("dingtalk Board ");
}

Public void insert (){

Newmethod ();
}

  
// In the new method, we add other methods before insert. Here, the order is specified by the user flexibly.
Public void newmethod ()
{
Othermethod ();
Work. insert ();

}

Public void othermethod ()
{
Listiterator = others. listiterator ();
While (listiterator. hasnext ())
{
System. Out. println (string) (listiterator. Next () + "in progress ");
}

}

}

 

In the above example, we put both the pitfall and dingtalk board in front of the piling insert. Here we just give an example to illustrate that the order of extra functions can be arranged at will.

Now the decorator mode is available. Let's look at how to call it:
Program code:

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

The decorator mode is now complete.

If you are careful, you will find that the above call is similar to the call when we read the file:
Program code:

Filereader Fr = new filereader (filename );
Bufferedreader BR = new bufferedreader (FR );

In fact, Java I/O APIs are implemented using decorator, and there are many I/O variants. If the inheritance method is adopted, many sub-classes will be generated, which is obviously quite tedious.

Decorator implementation in jive
In the forum system, some special words cannot appear in the forum, such as "down xxx", we need to filter these "reactionary" fonts, so that they do not appear or High Brightness Display.

In the IBM Java column, I talked about the forummessagefilter in jive. java uses the decorator mode. In fact, this program does not actually use decorator, but prompts that additional filtering functions can be designed for special forums, then we can reorganize the forummessagefilter as the decorator mode.

Therefore, when determining whether the decorator mode is actually used and whether the decorator mode is used, we must grasp the definition of the decorator mode and the involved roles (decoratee and decorator ).

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.