Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
WS Stack Comparison
If you don't know which WS stack tools you should use in java, this document will certainly help you.
Event-Listener Pattern
In lossy-couple environment, notification system is essential, because it allows two or more lossy-couple components to communicate and notify each other when an event is triggered. There are several patterns to implement notification system in Java and one of them is event-listener pattern.
In Java, building event-listener components is not easy as Flex or C#. It requires a lot of boilerplate code to implement it. In this article, I will talk about building simple event listener in Java. First I will demonstrate the general algorithm for event listeners then I will implement it in a simple, humble example.
Event-Listener Pattern:
Event-Listener pattern is a simple pattern that can be implemented easily in Java bean class. Actually, it is part of JavaBean conventions. It has been used mainly in J2SE platform, particularly in GUI libraries. This pattern consists of two main parts (Source and Listener):
What is share between these two parts is listener event message! The message is usually interface used to determine event data-type.

Sample:
Now, we are going to write a simple implementation for the pattern, so let's start with the source part.
First of all, we need a simple bean class. I will name it “SourceClass”:
Then we need a data structure to store the listeners (Remember, the small database). For simplicity, I will use Vector
For simplicity, a simple fire method will be added to notify all registered listeners
Now, let's define our event, which simply extends EventListener interface in the Java util package
Finally, we need a simple listener which listens to our TestEvent. Remember, listener can be any class you want. The only thing is required to become a listener is creating a SourceClass and adding the event (TestEvent).
when the source fires the TestEvent, listeners gets a notification and will execute their event handler code.
Conclusion:
Event-Listener Pattern provides simple notification system for Java components. The pattern has two main parts (Source and Listener). The source stores all listeners, and the listeners listen to particular event, which is simply the message or notification that transports from source to listeners.
Further Reading:
In Java, building event-listener components is not easy as Flex or C#. It requires a lot of boilerplate code to implement it. In this article, I will talk about building simple event listener in Java. First I will demonstrate the general algorithm for event listeners then I will implement it in a simple, humble example.
Event-Listener Pattern:
Event-Listener pattern is a simple pattern that can be implemented easily in Java bean class. Actually, it is part of JavaBean conventions. It has been used mainly in J2SE platform, particularly in GUI libraries. This pattern consists of two main parts (Source and Listener):
- Source: the source is the Java class for maintaining the event listeners which has previously been registered - It is like a small database . Therefore, the source class has method for registering (add) and unregistering (remove) listeners to the small database. The source also has ability to fire an event.
- Listener: As the name refers to, the listener is the Java component that interests in a specific event. When an event is triggered, all listeners interested in the event will get a notification and will execute their own event handler codes. Listeners register for events using the methods defined by the sources of those events.
What is share between these two parts is listener event message! The message is usually interface used to determine event data-type.

Sample:
Now, we are going to write a simple implementation for the pattern, so let's start with the source part.
First of all, we need a simple bean class. I will name it “SourceClass”:
class SourceClass {
}
Then we need a data structure to store the listeners (Remember, the small database). For simplicity, I will use Vector
private Vector listeners = new Vector();Now, we need two methods, one of them for registering and the other unregistering, they should have JavaBean conventional style. The method used to register a listener in the vector must use the prefix “add”, and the other one, must start with the prefix “remove”. Both of them should follow by the event type and must end with the postfix “Listener”. In addition, they should have an argument with event data-type
public void addTestEventListener(TestEvent l) {As the preceding example depicts, the two methods manage the listeners which interest in "TestEvent" Event.
listeners.addElement(l);
}
public void removeTestEventListener(TestEvent l) {
listeners.removeElement(l);
}
For simplicity, a simple fire method will be added to notify all registered listeners
public void fire() {
Vector temp = (Vector) listeners.clone();
for (int i=0; i > l.size(); i++) {
TestEventListener listeners = (TestEventListener) temp.elementAt(i);
listener.eventHandler ();
}
}
Now, let's define our event, which simply extends EventListener interface in the Java util package
public interface TestEvent extends java.util.EventListener {So as you see, the event is nothing than a simple, share interface between the source and listeners.
void eventHandler();
}
Finally, we need a simple listener which listens to our TestEvent. Remember, listener can be any class you want. The only thing is required to become a listener is creating a SourceClass and adding the event (TestEvent).
.As the codes shown above, SourceClass and TestEvent objects have been created. of course, we have to resolve all abstract methods in TestEventListener interface and the last not the least, we add the later object to the source.
.
.
SourceClass c = new SourceClass();
TestEvent e = new TestEvent(){
void eventHandler(){
//Do something
}
}
c.addTestEventListener(e);
c.fire();
when the source fires the TestEvent, listeners gets a notification and will execute their event handler code.
Conclusion:
Event-Listener Pattern provides simple notification system for Java components. The pattern has two main parts (Source and Listener). The source stores all listeners, and the listeners listen to particular event, which is simply the message or notification that transports from source to listeners.
Further Reading:
Add Java Magazines Widget to your website
Finally, I finished designing and building a new widget, Java Magazine. The widget has a very simple interactive interface which allows the end user to browse and navigate through the latest Java Magazine. Be free to embed this widget to your blog, web site, dashboard....etc.
the Widget's Code:
<script type="text/javascript" src="http://cdn.widgetserver.com/syndication/subscriber/InsertWidget.js"></script><script>if (WIDGETBOX) WIDGETBOX.renderWidget('6adc2f17-c036-4cac-bfb1-26a0e1a2d298');</script><noscript>Get the <a href="http://www.widgetbox.com/widget/3ba73d0f-c85d-4d73-8b61-34b5a8bc4078">Java Magazines</a> widget and many other <a href="http://www.widgetbox.com/">great free widgets</a> at <a href="http://www.widgetbox.com">Widgetbox</a>!</noscript>
Java is like Sex!!!
because:
- One mistake and you have to support it for the rest of your life. (Michael Sinz)
- Working with communities is more productive.
- you will get a headache if you don't use it for a day. (yakkoh)
- Once you get started, you’ll only stop because you’re exhausted.
- It takes another experienced person to really appreciate what you’re doing.
- Conversely, there’s some odd people who pride themselves on their lack of experience.
- You can do it for money or for fun.
- If you spend more time doing it than watching TV, people think you’re some kind of freak.
- It’s not really an appropriate topic for dinner conversation.
- There’s not enough taught about it in public school.
- It doesn’t make any sense at all if you try to explain it in strictly clinical terms.
- Some people are just naturally good.
- But some people will never realize how bad they are, and you’re wasting your time trying to tell them.
- There are a few weirdos with bizarre practices nobody really is comfortable with.
- One little thing going wrong can ruin everything.
- It’s a great way to spend a lunch break.
- Everyone acts like they’re the first person to come up with a new technique.
- Everyone who’s done it pokes fun at those who haven’t.
- Beginners do a lot of clumsy fumbling about.
- You’ll miss it if it’s been a while.
- There’s always someone willing to write about the only right way to do things.
- It doesn’t go so well when you’re drunk, but you’re more likely to do it.
- Sometimes it’s fun to use expensive toys.
- Other people just get in the way.