First, let’s introduce what a listener is:
Listener - is an ordinary Java program that implements the pending interface. This program is specifically used to listen for method calls of another class.
This is using observer mode.
What is Observer Mode:
Defines a one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified to update automatically.
Example:
The addXxxxListener in GUI programming is all observer mode.
For example, adding listening events to button clicks, adding listening events to keyboard, etc.
Three important categories of observer pattern:
The source of the event being listened to, that is, the object we are using.
The registered listener is specifically used to listen to the currently used objects.
The event object Event is the object being listened to!
Let's first look at a simple version of the monitor written by yourself.
Simple version:
There are event sources, listeners, and test classes.
Event will be implemented in the next full version.
Development steps:
Step 1: Implement a class Person that needs to be listened to.
Step 2: Implement a listening interface IPersonRunListener.
Step 3: In Person class, provide one method (or multiple, I provide 2 methods here) for registering IPersonRunListener classes, i.e. addBefore and addAfter
Step 4: The instance of the IPersonRunListener class must be maintained in the Person class.
Step 5: When calling the person.run method, determine whether the IPersonRunListener is null, and if it is not null, call its fighting method.
Step 6: In the Demo class, instantiate 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+"Run..."); 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 person = new Person("Zhang San"); IPersonRunListener listener = new IPersonRunListener() { @Override public void fighting() { // There are many things you can do here, not only output // However, since you haven't written the Event object yet, you can't get the System.out.println("Be prepared for it 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 only output // However, since you haven't written the Event object yet, you can't get the System.out.println called by whom it was ("Run, rest..."); } }Output:
Add event source in the full version:
Here, an Event-event object has been added to the previous one. It is considered the full version.
Development steps:
Step 1: Continue to add a PersonEvent class based on the previous page (note that I said it is a class, not an interface), representing the event object.
Step 2: Add a Person attribute to the PersonEvent object to identify the event source object.
Step 3: Modify the fighting method of the PersonListener interface and let it receive a PersonEvent parameter.
Step 4: In the Person class run method, if it is determined that the PersonListener property is not empty, the fighting method is called, the PersonEvent is instantiated and the fighting method is passed to the fighting method.
Step 5: In the main method, test whether it is the same object through the getSource method of PersonEvent.
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+"Start 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// It writes our common template. If we inherit this class, we can stop writing it. //If there are different functions, we will write it ourselves to override this class class DefaultCatListener implements IPersonRunListener { @Override public void fighting(PersonEvent 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("Zhang San"); Person p2 = new Person("Jack"); IPersonRunListener listener = new IPersonRunListener() { @Override public void fighting(PersonEvent pe) { System.out.println(pe.getSource()+"It has been run..."); if(pe.getName().equals("Zhang San")) { System.out.println(pe.getName()+"Run to the first place..."); } } } }; p1.addPersonListener(listener); p2.addPersonListener(listener); p1.run(); p2.run(); Person p3 = new Person("Li Si"); p3.addPersonListener(new DefaultCatListener()); p3.run(); }}Demo results:
These are the basic principles. If you change the output of the event in it to the action you need, you can realize the function you want. Add a listener and you can call the method you want to call before or after the run method and do the action you want to do!
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.