This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

design a POJO

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to design a POJO as follow

It should start itself when a particular event occurs. event may be changing in value of parameters of classes. POJO should listen the context for any event related to it, those event already defined in the POJO.

Ex:
Class A {
int j;

}

Class B
{
Events :If any changes in value of J in the class A then below operation should fire

Viod df()
{
Do some operation
}

}
 
Reinhard Horn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jacob,

you could use B as a listener class and register it in class A. So whenever the j-value
in A changes, A fires B's event Method doDf().

public interface AListener {

public void doDf(int j);

}

package javaapplication1;


public class B implements AListener {

public void doDf(int j) {
System.out.println(j);
}



}


package javaapplication1;



import java.util.*;

public class A {



private int j;

private List<AListener> alisteners = new Vector<AListener>();

public List<AListener> getAlisteners() {
return alisteners;
}

public void addAlistener(AListener alistener) {
this.alisteners.add(alistener);
}

public int getJ() {
return j;
}

public void setJ(int j) {
this.j = j;
this.fireJChangedEvent();
}

private void fireJChangedEvent() {
for (AListener alistener : this.getAlisteners()) {
alistener.doDf(this.getJ());
}
}


}


package javaapplication1;

public class TestA {


public static void main(String[] args) {

B b1 = new B();
B b2 = new B();
A a1 = new A();

a1.addAlistener(b1);
a1.addAlistener(b2);
a1.setJ(12);
}

}

Regards
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look to the java.util.Observer / java.util.Observable pair for a kind of reference implementation of the Observer pattern. You could either use them directly or take a look at their API's for some inspiration, if you want to implement your own solution.
[ November 29, 2008: Message edited by: Jelle Klap ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic