Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is what you have to do to understand the architecture. First the Design pattern used here is the Observer Pattern -</p> <blockquote> <p>Implementing the Publisher/Subscriber scenario. We need a mechanism which allows us to notify subscriber objects whenever the publisher object changes state.</p> </blockquote> <p>You can find more information about implementing it at <a href="http://www.codeproject.com/Articles/47948/The-Observer-Design-Pattern" rel="nofollow">here</a>. </p> <p>But if your objective is to make your own JButton the best approach is to Subclass JButton.</p> <pre><code>class MyCustomButton extends JButton{} </code></pre> <p>You asked about how <code>JFrame</code> can have <code>addMouseListener</code> - It's because <code>JFrame</code> extends <code>java.awt.Component</code>. Hope this helps.</p> <h2>EDIT</h2> <p>Observer doesn't do anything. Subject notify the Observers if anything change. This is the notify method.</p> <pre><code> public void notify() { for (int i=0;i &lt; observers.size();i++) { Observer ob = (Observer)observers.get(i); ob.update(newValue); } } </code></pre> <p>Back again to the Packet and the Bucket example - Packet - Observer Bucket - Subject</p> <p>Bucket wires the Packet that a new Packet has entered the Bucket. In classes Bucket class will call the notify method and all the registered Packets will be notified. If a Packet wishes to unsubscribe itself from the Bucket it just needs to call the Unsubscribe method and it will remove the Object from the ArrayList of the Bucket. </p>
 

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