A good memory is better than a bad pen. Listener in 34-java application Listener (1)

Source: Internet
Author: User
Tags event listener

A listener is an object that listens to and handles events or state changes that occur on other objects and takes action immediately when the object being monitored occurs. A listener is actually a generic Java program that implements a particular interface, which is specifically designed to listen to a method call or property change of another Java object, which is immediately executed by a listener when the event has occurred.
In many places, a lot of behavior, all need to listen. For example, we have a car, after the ignition of the car, we have to check the status of the car, the car after the start, to do some processing, which can be achieved through monitoring.
If we want to design a car, so that the various states of the car can be monitored by other systems and what to do, you can follow the strict event processing model to design an object, the object can be monitored by other objects, the event processing model involves three components: Event source, event object, Event listener.
1, Java implementation of event listeners (analog monitoring of the car part of the behavior)
In the bandwidth is not enough, compression is a good way to solve things, the current bandwidth is getting bigger, but the file is getting bigger, compression or there is a hero. Baidu and other Web sites are also using compressed data transfer methods.
An event listener consists of three blocks: the event source, the event object, the event listener, and an additional listener (the listening test Class). The following simulates a simple monitoring of the car, a total of four classes.

 PackageCom.listener;/** * A Simple object that can be monitored, such as a car, ignition, operation and other actions need to be monitored * @author Fan Fangming */ Public  class easycar {    PrivateEasycarlistener Listener;//Car ignition     Public void Start() {if(Listener! =NULL) {Listener.dostart (NewEasycarevent ( This)); }    }//The car starts to run up     Public void Run() {if(Listener! =NULL) {Listener.dorun (NewEasycarevent ( This)); }    }//This method is used to register listeners listening for the behavior of the person class object     Public void Registerlistener(Easycarlistener Listener) { This. Listener = Listener; }} PackageCom.listener;/** * A simple (event source) Listener interface * @author Fan Fangming */Interface Easycarlistener {voidDostart (Easycarevent e);voidDorun (Easycarevent e);} PackageCom.listener;/** * A simple event object that encapsulates the object to which the event source can be monitored * @author Fan Fangming */Class Easycarevent {PrivateEasycar Source; Public easycarevent() {    } Public easycarevent(Easycar Source) { This. Source = Source; } PublicEasycarGetSource() {returnSource } Public void SetSource(Easycar Source) { This. Source = Source; }} PackageCom.listener;/** * A Simple object that can be monitored * @author Fan Fangming * * Public  class easycartest {     Public Static void Main(string[] args) {Easycar p =NewEasycar ();//Register listener for monitoring p object behaviorP.registerlistener (NewEasycarlistener () {//Monitor the car ignition behavior             Public void Dostart(Easycarevent e)                {Easycar p = e.getsource (); System.out.println ("The car has been ignited, is automatically detected the various properties of the rice classmate car ..."); System.out.println ("The tire pressure of a classmate's car is normal. "); System.out.println ("The power system of a classmate's car is normal. "); System.out.println ("The safety system of a classmate's car is normal. "); System.out.println ("..."); }//Monitor the car starts to run up             Public void Dorun(Easycarevent e)                 {Easycar p = e.getsource (); System.out.println ("Ah Rice classmate car start running ..."); System.out.println ("Automatically closes the door. "); System.out.println ("Automatically shuts off the tank. "); System.out.println ("Automatically turns on music. "); System.out.println ("...");        }        }); System.out.println ("After listening, the car's behavior is monitored. ");        P.start ();    P.run (); }}

2. Operation result
Completed the monitoring, the following began to monitor the behavior of the car.
The car has been ignition, is automatic detection of the various properties of rice classmate car ...
The tire pressure of a classmate's car is normal.
The power system of a classmate's car is normal.
The safety system of a classmate's car is normal.
...
A rice classmate car starts to run ...
Automatically closes the door.
Automatically closes the tank.
Automatically turn on music.
...

A good memory is better than a bad pen. Listener in 34-java application Listener (1)

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.