Using listeners

Listeners are referred in many places of this documentation probably already giving an idea about how and why they are used. Anyway the listener concept should be explained in more detail here.

Example: Font manipulation

In stage 3 of SimplyHTML many classes dealing with font manipulation had been added. These classes mostly are GUI elements or are related to GUI elements in some way. The interaction between objects during font manipulation demonstrates the importance of proper design in handling such a rather complex topic.

In class FontPanel for instance several objects allow changes to font attributes while all attributes are reflected in another object, the sample view.

Listener interface instead of hard coding object relations

Instead of hard coding a relationship between the font attribute selectors and the sample view component, each attribute selector defines a listener interface. Whenever an attribute is changed, a change event is fired in the format that interface defines.

The sample view component in turn implements the interface by having a method valueChanged which is defined in the listener interface of the font selectors. The sample view component is then registered as a listener with the component defining the interface.

Having functions to handle calls to method valueChanged, the functions need to be coded only once and only at the place they belong to - the sample view component in our case.

Conclusion

The listnener concept is an elegant way of letting an arbitrary number of objects dynamically interact without having to hard code relationships between objects. By defining interfaces and listening to events a clear separation according to object boundaries is accomplished.

By implementing code reacting on events, redundancies are avoided and objects do not need to 'know' about how other objects have to be changed by own actions.