Javaweb Listener Listener Instance parsing _java

Source: Internet
Author: User

Let's start by describing what a listener is:

Listener-is a common Java program that implements a pending interface that is designed to listen to method calls from another class.
This is the use of observer mode.

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 a listener event, for the keyboard to add listening and so on ...

Three important classes of observer patterns:

The source of the event being monitored, which is the object we are using.
The registered listener is designed to listen for objects that are currently in use.
Event object is the object being monitored!

Let's take a look at a simple version of the listener that you wrote.

Simple version:

There are event sources, and listeners, test classes.
Event and the next full version is implemented.
Development Steps:
First step: Implement a class person that needs to be monitored.
Step two: Implement a listener interface Ipersonrunlistener.
Step three: In the person class, provide a method (or more, I've provided 2 methods here) for registering Ipersonrunlistener classes, i.e. Addbefore and Addafter
Step Fourth: You must maintain an instance of the Ipersonrunlistener class in the person class.
Step Fifth: When calling the Person.run method, determine whether the Ipersonrunlistener is null and 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 class Person {
 private String name;
 Private Ipersonrunlistener Listener1;
 Private Ipersonrunlistener 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;
 }

}

Interface ipersonrunlistener{public
 void Fighting ();
}

Demo

Package cn.hncu.designPattern1;

public class Demo {public

 static void Main (string[] args) {person is
  = new Person ("John");
  Ipersonrunlistener listener = new Ipersonrunlistener () {
   @Override public
   void Fighting () {
    //There's a lot to do here, Not only can output oh
    //But since the event object has not been written, it is not possible to get the System.out.println who called
    ("do the preparatory work first ...");
  Person.addbefore (listener);

  A A = new A ();

  Person.addafter (a);

  Person.run ();
 }

Class A implements ipersonrunlistener{
 @Override public
 void Fighting () {//
  There are many things you can do here, not just output oh
  / However, since the event object has not been written, it is not System.out.println who called
  ("run over, rest ...");
 } 


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:
Step one: Continue adding a personevent class on the basis of the previous page (note that I'm saying that class is not an interface), representing event pairs.
Step two: Give Personevent the image, add a person attribute to identify the event source to the image.
Step three: Modify the fighting method of the Personlistener interface so that it receives a personevent parameter.
Step Fourth: In the Person class run method, if you judge that the Personlistener property is not empty, the fighting method is invoked, the personevent is instantiated, and the fighting method is passed.
Fifth step: In the Main method, test whether it is the same pair of image by Personevent GetSource method.

Person.java

Package cn.hncu.designPattern2;
 public class Person {private String name;


 Private Ipersonrunlistener listener;
  Public person (String name) {super ();
 THIS.name = name;
  public void Run () {System.out.println (name+ "started running ...");
  if (listener!=null) {listener.fighting (new personevent (this));
 } public void Addpersonlistener (Ipersonrunlistener listener) {This.listener=listener;
 Public String GetName () {return name;
 @Override public String toString () {return ' person [name= ' + name +], listener= "+ listener +]";

} interface Ipersonrunlistener {public void fighting (Personevent pe);}
 Class personevent{person p = null;
 Public personevent (person p) {this.p = P;
 Public String GetName () {return p.getname ();
 Public Object GetSource () {return p;
//We can also write a basic class that helps us implement the interface//write our generic template, if we inherit this class, we can not write it. There is a different function of the place, we write on our own, covering this class method class Defaultcatlistener implements Ipersonrunlistener {@Override public void fightingSonevent pe) {System.out.println ("Default action ...");

 }
}

Demo.java

Package cn.hncu.designPattern2;

public class Demo {public

 static void Main (string[] args) {person
  p1 = new Person ("John");
  person P2 = new Person ("Jack");
  Ipersonrunlistener listener = new Ipersonrunlistener () {
   @Override public
   void Fighting (personevent pe) {
    System.out.println (Pe.getsource () + "has run out ...");
    if (Pe.getname (). Equals ("John")) {
     System.out.println (pe.getname () + "ran to first place ...");
  P1.addpersonlistener (listener);
  P2.addpersonlistener (listener);
  P1.run ();
  P2.run ();


  Person P3 = new person ("Dick");
  P3.addpersonlistener (New Defaultcatlistener ());
  P3.run ();
 }


Demo results:

Basically the principle is that, the output of the event you can change the action you need to achieve the function you want, add a monitor, you can call in the Run method before or after you want to call the method, do what you want to do!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.