Java Event handling mode

Source: Internet
Author: User
Tags config

The Java event pattern is the important foundation of the dynamic response system, the event pattern in the graphical interface field has been introduced many articles, but in the server side we will encounter more event pattern, here I try to sum up:

Event Direct Drive Mode

The first requirement of the event pattern is performance requirements, direct and fast, command mode must be used frequently, mainly suitable for handling the command of the front desk quickly, and command mode is often the important part of system architecture and the main mode of Process control.

Command mode is often used in conjunction with Java's reflect, because the system's event-handling systems are dynamically changing, and as functional requirements expand, there may be dynamic event-handling response systems, as in struts, for example, we know that One of the main structs profiles is struts-config.xml as follows:

<struts-config>
<action-mappings>
<action path="/login" type="com.javapro.struts.LoginAction"/>
<action path="/logout" type="com.javapro.struts.LogoutAction"/>
</action-mappings>
</struts-config>

It is actually a command and event mapping relationship, through this configuration file, runtime dynamic load corresponding action, complete command mode, we check loginaction code, we can see the basic characteristics of command mode:

public final class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
.................
}
}

Obviously, a typical command pattern requires an interface. There is a unified approach in the interface, where the unified method is execute;

For example, we have a real-time system, customer segments to the server issued a different code code, meaning that different requests, different requests have different handler for processing, handler interface is:

public class Handler{
public byte[] handleRequest();
}

Processes of different natures inherit this handler interface, such as the process of entering the system

public class EnterHandler implements Handler{
public byte[] handleRequest(){
//具体业务处理
......
}
}

When handler is invoked:

//从cache中获取这个requestId对应的Handler
Handler handler = (Handler)cache.get(new Integer(reqId));
//调用handler的统一方法handleRequest()
byte[] outInf = handler.handleRequest();

The above is a commonly used event-driven pattern. It is characterized by an event directly starting the corresponding event handler.

Chain of the responsibility responsibility chain pattern should also belong to this category, when the event arrives, let this event in the batch of processors we provide to pick and choose the appropriate processor for processing, the disadvantage is obvious, the loss of performance in a selection, generally not recommended, This pattern is suitable when we can not predict the occurrence of the event content to use, because we do not know the specific circumstances of the event, we can not run the program before the operation of the corresponding processor, can only rely on the runtime, the incident itself to explore "hit luck."

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.