Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, event handling is the key to your problem. The C# snippet you show also uses event handling to tell listeners that a property is changed. Java also has these PropertyChangeEvent's, which is used for most Beans. (For example: Swing components use them).</p> <p>You can, however, do this manually: (though this isn't as POJO anymore, but more a Java Bean)</p> <p><strong>EventBean.java</strong>:</p> <pre><code>import java.util.LinkedList; import java.util.List; public class EventBean { private final List&lt;Listener&gt; listeners = new LinkedList&lt;Listener&gt;(); protected final &lt;T&gt; void firePropertyChanged(final String property, final T oldValue, final T newValue) { assert(property != null); if((oldValue != null &amp;&amp; oldValue.equals(newValue)) || (oldValue == null &amp;&amp; newValue == null)) return; for(final Listener listener : this.listeners) { try { if(listener.getProperties().contains(property)) listener.propertyChanged(property, oldValue, newValue); } catch(Exception ex) { // log these, to help debugging ex.printStackTrace(); } } } public final boolean addListener(final Listener x) { if(x == null) return false; return this.listeners.add(x); } public final boolean removeListener(final Listener x) { return this.listeners.remove(x); } } </code></pre> <p><strong>Listener.java</strong>:</p> <pre><code>import java.util.Collections; import java.util.Set; import java.util.TreeSet; // Must be in same package as EventBean! public abstract class Listener { private final Set&lt;String&gt; properties; public Listener(String... properties) { Collections.addAll(this.properties = new TreeSet&lt;String&gt;(), properties); } protected final Set&lt;String&gt; getProperties() { return this.properties; } public abstract &lt;T&gt; void propertyChanged(final String property, final T oldValue, final T newValue); } </code></pre> <p>And implement your class like this:</p> <pre><code>public class MyBean extends EventBean { private boolean typedA; public void setTypedA(final boolean newValue) { // you can do validation on the newValue here final boolean oldValue = typedA; super.firePropertyChanged("typedA", oldValue, newValue); this.typedA = newValue; } public boolean getTypedA() { return this.typedA; } } </code></pre> <p>Which you can use as:</p> <pre><code>MyBean x = new MyBean(); x.addListener(new Listener("typedA") { public &lt;T&gt; void propertyChanged(final String p, final T oldValue, final T newValue) { System.out.println(p + " changed: " + oldValue + " to " + newValue); // TODO } }); x.setTypedA(true); x.setTypedA(false); x.setTypedA(true); x.setTypedA(true); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload