Javaweb-listener Listener parsing and instance

Source: Internet
Author: User

Let's start by introducing what a listener is:

Listener-is A generic Java program that implements a pending interface that is specifically designed to listen for method calls from another class.
This is using the Observer Pattern.

What is the Observer pattern:
Defines a one-to-many dependency between objects, and when an Object's state changes, all objects that depend on it are notified of automatic Updates.
Example:
The Addxxxxlistener in GUI programming are all observer patterns.
For example, click on the button to add listening events, for the keyboard to add monitoring and so on ...

Three important classes of the observer pattern:

The source of the event being monitored, which is the object we are Using.
The registered listener is dedicated to listening to the objects currently in Use.
Event object is the object that is being monitored!

Let's start by looking at a simple version of the listener that we Wrote.

Simple version:

There are event sources, and listeners, to test the class.
The next full version of the event is Implemented.
Development Steps:
Step One: implement a class person that needs to be monitored.
Step two: implement a listening interface ipersonrunlistener.
Step three: in the person class, provide a method (or more, I have provided 2 methods Here) for registering the Ipersonrunlistener class, namely Addbefore and Addafter
Fourth Step: You must maintain an instance of the Ipersonrunlistener class in the person class.
Fifth step: when calling the Person.run method, determine whether Ipersonrunlistener is null, or call its fighting method if it is not null.
Sixth step: in the demo class, instantiate the person and register a listener.

Person:
Package cn.hncu.designPattern1; public classperson {PrivateString name;PrivateIpersonrunlistener listener1;PrivateIpersonrunlistener listener2; public  person(String Name) {super (); this. Name = name; } public void Run(){if(listener1!=NULL) {listener1.fighting (); } System. out. println (name+"running ...");if(listener2!=NULL) {listener2.fighting (); }    } public void Addbefore(ipersonrunlistener Listener) { this. listener1=listener; } public void Addafter(ipersonrunlistener Listener) { this. listener2=listener; }}Interfaceipersonrunlistener{ public void Fighting();}
Demo
Package cn.hncu.designPattern1; public  class Demo {     public Static voidMain (string[] Args) {person person =NewPerson ("zhang san"); Ipersonrunlistener listener =NewIpersonrunlistener () {@Override public voidFighting () {//here can do a lot of things, not only output oh                //however, Since the event object has not been written, you cannot get theSystem.out.println ("get ready for work first ...");        }        };        Person.addbefore (listener); A A =NewA ();        Person.addafter (a);    Person.run (); }} class A implements Ipersonrunlistener{@Override public voidFighting () {//here can do a lot of things, not only output oh        //however, Since the event object has not been written, you cannot get theSystem.out.println ("the Rest of the Day ..."); } }
Output:

Full Version – Add Event Source:

A Event-event object is added here relative to the previous one. it's a full Version.

Development Steps:
The first step: continue to add a personevent class on top of the previous page (note that I say the class is not an interface), which represents the event Pair.
Step two: to personevent, add a person property to identify the event source Pair.
Step Three: Modify the fighting method of the Personlistener interface so that it receives a personevent parameter.
Fourth step: in the person class run method, if you determine that the Personlistener property is not empty, call the fighting method, instantiate the personevent and pass it to the fighting method.
Fifth step: in the main method, the Personevent GetSource method is used to test whether the same pair of Images.

Person.java
Package cn.hncu.designPattern2; public classperson {PrivateString name;PrivateIpersonrunlistener listener; public  person(String Name) {super (); this. Name = name; } public void Run() {System. out. println (name+"started running.");if(listener!=NULL) {listener.fighting (NewPersonevent ( this)); }    } public void Addpersonlistener(ipersonrunlistener Listener) { this. listener=listener; } publicStringGetName(){returnName } @Override publicStringtoString() {return "person [name="+ name +", listener="+ Listener +"]"; }}InterfaceIpersonrunlistener { public void Fighting(personevent pe);} Class Personevent{person P =NULL; public personevent(person P) { this. P = p; } publicStringGetName(){returnP.getname (); } publicObjectGetSource(){returnP }}//we can also write a basic class that helps us implement the interface//inside Write our generic template, if we inherit this class, we can not Write. //have A different function, we write ourselves, covering this class of methodsClass Defaultcatlistener implements Ipersonrunlistener {@Override public void Fighting(personevent Pe) {System. out. println ("default Action ..."); }}
Demo.java
Package cn.hncu.designPattern2; public classDemo { public Static void Main(string[] Args) {person P1 =NewPerson ("zhang san"); person P2 =NewPerson ("Jack"); Ipersonrunlistener listener =NewIpersonrunlistener () {@Override public void Fighting(personevent Pe) {System. out. println (pe.getsource () +"it's finished ...");if(pe.getname (). equals ("zhang san")) {System. out. println (pe.getname () +"ran to the first place ...");        }            }        };        P1.addpersonlistener (listener);        P2.addpersonlistener (listener);        P1.run ();        P2.run (); Person P3 =NewPerson ("john doe"); P3.addpersonlistener (NewDefaultcatlistener ());    P3.run (); }}
Demo Result:

Basically the principle is that, the output of the event you need to change the action you want to achieve the function, add a listener, You can call the method before or after the run method, do the action you want to do!

Reprint please attach the original blog link:

http://blog.csdn.net/qq_26525215

Javaweb-listener Listener parsing and instance

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.