aspose file tools*
The moose likes Beginning Java and the fly likes design a POJO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "design a POJO" Watch "design a POJO" New topic
Author

design a POJO

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
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

Joined: Jun 04, 2007
Posts: 31
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


SCJP 5, SCWCD 1.4, SCBCD 5
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1773
    
    7

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 ]

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: design a POJO