• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

design a POJO

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}

}
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Trust God, but always tether your camel... to this tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic