Java and mode Learning Series-responsibility chain mode

Source: Internet
Author: User

 

I, Chain
Responsibility) Schema

In the responsibility chain mode, many objects are linked by each object's reference to its next home to form a chain. Requests are transmitted on this chain until an object on the chain decides to process this request. The client that sends this request does not know which object on the chain will eventually process this request, which allows the system to dynamically reorganize the chain and assign responsibility without affecting the client.

As shown in:

The Code is as follows:

// Handler class

Public
Abstract class handler {

 

Protected handler successor;

Public abstract void handlerequest ();

Public void setsuccessor (Handler
Successor ){

This. Successor = successor;

}

Public handler getsuccessor (){

Return successor;

}

}

// Concretehandler class

Public class
Concretehandler extends handler {

@ Override

Public void handlerequest (){

If (getsuccessor ()! = NULL ){

System. Out. println ("
Request is passed to "+ getsuccessor ());

Getsuccessor (). handlerequest ();

} Else {

System. Out. println ("
Request is handler here ");

}

}

}

// Client

Public class client {

Static
Private handler handler1, handler2;

Public
Static void main (string [] ARGs ){

Handler1
= New concretehandler ();

Handler2
= New concretehandler ();

Handler1.setsuccessor (handler2 );

Handler1.handlerequest ();

}

}

II, Application of the responsibility chain model in Java

Java1.0Version AWTEvent Processing Mechanism

The AWT library in Java uses the responsibility chain mode and command mode to process GUI events. Because a Windows component is often located in a container component, when an event occurs on a component, the event processor of this component can process the event, then decide whether to spread the event to the container component at the upper level. After receiving the event, the upper-level container component can handle the event here and decide whether to re-transmit the event to the upper-level container component until the event reaches the top-level component.

Disadvantages:

(1) AWT 1.0 event processing model is based on inheritance. To enable a program to capture a GUI event and process the event, it must inherit the component and assign the event processor to its subclass, that is, replace the action () method or handleevent () method. This is not a practice that should be promoted: In an object-oriented system, delegated relationships are often used, and inheritance relationships are not abused.

(2) In a complex GUI system, providing subclasses for all event components will lead to many subclasses and increase maintenance costs.

(3) because each event will spread upwards along the component tree structure, the event floating mechanism will make the event processing slow, which is also one of the disadvantages.

DHTMLEvent Processing in

Event processing in the browser's Dom model adopts the responsibility chain mode.

Netscape event model: the event processing mechanism of Netscape is called "event capturing )". In the event capture mechanism, an event is propagated down from the top layer of Dom, that is, the Windows Object is first received to the event, and then the document object, so that the event goes down. Therefore, the object generated by the event is the last one connected to the event.

Internet Explorer event model: when an event occurs on the Internet
When explorer browses a webpage, Internet Explorer uses DHTML's "event bubbing", that is, the event floating mechanism handles this event. The Dom model of Internet Explorer is an HTML object hierarchical structure and event processing mechanism. In the Dom, each HTML identifier is a DOM object, and each DOM object can generate one (or several) of the several events defined in advance ). Such an event first occurs on the object to which the event belongs, then it is propagated up and uploaded to the container object to which the object belongs. Therefore, the event floating mechanism is the opposite of the event capturing mechanism.

Timer

The Java. util. Timer class encapsulates the thread functions required by the timer. There is also a timertask class that encapsulates the thread functions required when scheduled tasks need to be repeated. As shown in:

Timer is a subclass of timerthread, while timerthread is the implementation of thread, so timer is the implementation of thread. At the same time, timertask is the implementation of runnable and thus a thread. Example:

Public class reminder {

Timer timer;

Public
Reminder (INT seconds ){

Timer = new timer ();

Timer. Schedule (New
Remindtask (), seconds * 1000 );

}

Class remindtask extends timertask {

Public void run (){

System. Out. println ("Time's
Up ");

// End the timer thread

Timer. Cancel ();

}

}

}

Behind every timer object, there is a background thread used to execute the timer tasks in sequence. These tasks should end quickly, otherwise they will occupy the thread used by timer to execute tasks, this delays subsequent tasks. After all the tasks are completed, the timer task execution thread will end smoothly and be collected by the Java garbage collector, but the garbage collection time cannot be predicted. If the program wants to quickly end the timer task execution thread, call the timer cancel () method to call the method. The timer class is thread-safe. In other words, multiple threads can share the same timer object without synchronization. The timer class is actually scheduled using the object. Wait (long) method.

 

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.