Java implementation of observer pattern

Source: Internet
Author: User
Tags event listener function prototype naming convention

Java Event Model

In my first two introduction of the C # event and the delegation of the blog published, we responded particularly warmly, the rate is very high, it seems that the event/Commission mechanism is a lot of colleagues more vague place, take this Dongfeng, plus recently moved to Java, so decided to write this introduction of the Java Event Mechanism blog.

In fact, regardless of the language of the event mechanism, no exception to escape three points: Event source/sender, event receiver/processor/listener, and event source to the event recipient of event information. Corresponds in Java, event source, event listener, and event messages called EventObject. In C #, respectively, the sender (Sender), the processor (handler), the event message is the event argument (eventargument). Both Java and C # Use the same response pattern: publisher/Subscriber mode (Publisher/subscriber), specifically:

(1) The subscriber registers an event of interest to the publisher;

(2) When an event occurs, notifies the subscriber to respond to the event.

The simple word, is the sentence often said: "Don ' t call me,i ' ll called you." ”

Because of the previous discussion of C # 's event mechanism, I will focus on the Java event mechanism and the comparison of the two implementation mechanisms:

(i) Java Event Implementation mechanism

Here is an example of a custom Java event that, with this simple demo, you can see the Java Event Implementation mechanism. Here, this example is quoted from http://www.rainsts.net/article.asp?id=224, in order to illustrate the rationale of the original version of the anonymous method part, and because the site's code editor is lack of support for Java code, so the keyword , types, etc., are not formatted for display processing.

import java.util.*;


//Define a class similar to C # EventArgs to pass event state information.


//General requirements inherit from Java.util.EventObject, and end with Event.


class Clicktevent extends EventObject


{


public Demobean Source;


//constructor Parameters pass the event source that generated the event


public clickevent (Demobean source)


  {


super (source);


This.source = source;


  }


}


//interface to define the event response function prototype, just as the C # delegate defines the response function template,


//Don't forget, the interface is actually a kind of "contract", "contract", through the function signature in this interface to achieve the specification of the response function


//General requirements inherit from Java.util.EventListener, and end with Listener.


//Here with I start to define the Clicklistener interface, from. NET, does not conform to the Java EE naming convention


interface Iclicklistener extends EventListener


{


void Click (clicktevent e);


}





//Event listener, implement listener interface


public class Clicklistener implements Iclicklistener


{


public void Click (Clicktevent e) {


System.out.println ("The clicked event happened");


   }





 }


Defines the demo control class, which is the event source


class Demobean


{


//Use a Java.util.Vector object to store all event listener objects.


private vector clicks = new vector ();


//Add event subscriptions. Typically spelled in Add (listener) and add synchronized keywords.


public synchronized void Addexamplelistener (Iclicklistener listener)


 {


Clicks.add (listener);


 }


//Remove event subscriptions. Typically, you spell with the Remove (listener) and add the Synchronized keyword.


public synchronized void Removeexamplelistener (Iclicklistener listener)


 {


Clicks.remove (listener);


 }


//Trigger event.


protected void doclickevent ()


  {


//Lock to avoid any events being subscribed or removed during the triggering period.


synchronized (this)


   {


//Create event state object.


clicktevent ce = new clickevent (this);


//Loops trigger all event subscription methods.


for (int i = 0; i < clicks.size (); i++)


    {


Iclicklistener e = (iclicklistener) clicks.get (i);


E.click (CE);


    }


   }


 }


//Analog click operation.


public void Click ()


 {


doclickevent ();


 }


}





//test program


public class Program


{


public static void Main (string[] args)


 {


//Create control.


Demobean bean = new Demobean ();





//instantiation of an event listener


Clicklistner testlistner=new Clicklistner ();


//Add event subscriptions.


Bean.addexamplelistener (Testlistner);


//Simulate trigger click operation.


Bean. Click ();


 }


}

Related Article

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.