Using decorate mode to implement word processing in the message board

Source: Internet
Author: User
Tags filter define object getmessage implement inheritance interface string
The decorator pattern adds responsibility to the object dynamically in a transparent manner to the client. This pattern provides a more flexible alternative than inheriting the ability to extend the object, avoiding the proliferation of classes generated by the inheritance method, and making it easier to change the responsibility of the object.

We often have to add some new responsibilities to some individual objects, not all classes. For example, our system message feedback plate may need to filter some words in the user input messages (such as political sensitive words, pornographic words, etc.), but also may be some of the user input message modification (such as the user entered the URL automatically add hyperlinks, the user entered the UBB code to convert), It is also possible to send user-entered content in the mailbox of the network administrator and so on. If you are designing by using class inheritance, we may want to design an interface

bodycontentfilterintf, and then in the bodycontentfilterintf derived from Sensitivewordcontentfilter, Htmlcontentfilter, Sendemailcontentfilter and other classes. However, if you also want to be able to filter sensitive words and can be modified, or filter sensitive words after the user input messages sent to the network management mailbox, etc., this will increase the sensitivewordhtmlcontentfilter, Sensitivewordsendemaillcontentfilter and other classes, this way led to the formation of the sub-type waterfall hair.

A flexible approach is to embed the filter in another filter, which is responsible for invoking the method that is embedded in the filter and executing its own filter method. We call this embedded filter to be decorated (decorator). This decoration is consistent with the filter interface. Decoration moves the request forward to another filter, and may be able to forward and execute some additional actions (such as retouching, sending mail), transparency allows you to nest multiple decorations recursively, from the face can add any number of features.

In fact, the filter pattern in Java is very much used, typically the IO stream operation. In IO processing, Java abstracts data into streams (stream). In the IO Library, the most basic is the InputStream and outputstream two processing output and input objects respectively, but in InputStream and OutputStream provides the simplest flow processing method, can only read/write characters, no buffer processing, Unable to process files, and so on.

Linenumberinputstream, Bufferinputstream, StringBufferInputStream and other classes that provide a variety of services can be combined to achieve many functions, as follows:

FilterInputStream mystream=new Linenumberinputstream
New Bufferinputstream (New StringBufferInputStream (Mystringbuffer));
Multiple decorator are stacked together, resulting in a powerful stream. Can be buffered, and can get the number of rows, this is the power of decorator!

Here is our class static diagram


We define an interface bodycontentfilterintf to define the methods to be implemented by all filters:

Public interface Bodycontentfilterintf {
public string Filtcontent (String acontent) throws contentfilterexception;
}
This interface has only one method Filtcontent, will filter the message to the acontent parameters, filtcontent to acontent some processing (such as decorative URL, UBB, etc.), and then the processed string as return value returned If the message is not filtered through (such as containing sensitive words, etc.), just throw the custom contentfilterexception exception.

The following is a possible filter (guaranteed to enter more than 50 words):

public class Lengthcontentfilter

Implements BODYCONTENTFILTERINTF {
Private bodycontentfilterintf bodycontentfilterintf = null;
Public Htmlcontentfilter (bodycontentfilterintf afilter)
{
bodycontentfilterintf = Afilter;
}

public string Filtcontent (String acontent) throws Contentfilterexception {
String l_content = acontent;
If (Bodycontentfilterintf!=null)
_content = bodycontentfilterintf. Filtcontent (l_content);
if (Acontent.length () <=50)
throw new Contentfilterexception ("Enter the number of words can not be less than 50!") ”);
return acontext;
}
}
This is another filter (pseudo code, which is used to send mail to the network management mailbox) public class Sendemailcontentfilter

Implements BODYCONTENTFILTERINTF {
Private bodycontentfilterintf bodycontentfilterintf = null;
Public Sendemailcontentfilter (bodycontentfilterintf afilter)
{
bodycontentfilterintf = Afilter;
}

public string Filtcontent (String acontent) throws Contentfilterexception {
String l_content = acontent;
if (bodycontentfilterintf!=null)
L_content = bodycontentfilterintf. Filtcontent (l_content);
SendEmail ("webmaster@SnailWeb.com", l_content)
return acontext;
}
}
Of course there are sensitivewordcontextfilter (filter sensitive words), Htmlcontentfilter (Modify the user input message in the hyperlink) and so on.

With these filters, we can easily add a variety of composite filters for the message board. For example, we want to hyperlink the input of the message to modify and filter the sensitive words, so we can only call as follows:

try {
L_content = new Htmlcontentfilter (new Sensitivewordcontextfilter (null)).
Filtcontent (Bodycontext);
}

catch (Contentfilterexception ex) {
Bbscommon.showmsginresponse (response, Ex.getmessage ());
Return
}
We can even add different filters dynamically, for example, for members we have to the input of a hyperlink to modify the message and send his message to the network management mailbox, and for Non-members we have to filter the sensitive words he entered and to ensure that the input of words not less than 50, we only need to call the following:

try {
bodycontentfilterintf bodycontentfilterintf = null;
bodycontentfilterintf = new Htmlcontentfilter (null);
if (ismember==true)
bodycontentfilterintf = new Sendemailcontentfilter (bodycontentfilterintf);
Else
bodycontentfilterintf = new Sensitivewordcontextfilter (bodycontentfilterintf);
L_content = Bodycontentfilterintf.filtcontent (Bodycontext);
}
catch (Contentfilterexception ex) {
Bbscommon.showmsginresponse (response, Ex.getmessage ());
Return
}

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.